码迷,mamicode.com
首页 > 其他好文 > 详细

第六篇 网关ocelot,搭配consul。达到负载均衡的效果

时间:2020-03-31 12:45:02      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:contain   his   ams   ide   youdao   ase   stc   host   netcore   

首先nuget 

Ocelot版本5.6.0-unstable0028

Consul版本0.7.2.6

 

 

根目录新建一个 Ocelot.json

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/{url}",//任意的url
      "DownstreamScheme": "http",//
      "UpstreamPathTemplate": "/VideoInspectionConfig/{url}",//可配置
      "UpstreamHttpMethod": [ "Get", "Post", "Delete", "Put" ],
      "ServiceName": "TestCoreWebAPI.VideoInspectionConfig",//跟前几篇中consul中注册的名字要一样
      "LoadBalancer": "RoundRobin",
      "UseServiceDiscovery": true,
      "ReRouteIsCaseSensitive": false      
    }
    
  ],
  "GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
      "Host": "114.115.215.189",//consul的服务器
      "Port": 8500, //consul端口,
"Token": "245d0a09-7139-bbea-aadc-ff170a0562b1" // consul acl里的toekn,如果没配置acl,这个节点不需要
} } }

在 Program.cs里注册Ocelot.json

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace WebAPIGateway
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
             .ConfigureAppConfiguration((context, builder) =>
             {
                 builder.AddJsonFile("Ocelot.json");
             });
    }
}

  在Startup.cs里配置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace WebAPIGateway
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddOcelot(Configuration);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();

            app.UseOcelot().Wait();
        }
    }
}

  现在就可以实现 ocelot和consul的 搭配了,以后有了新的服务。如果要玩转这套,请先看之前的文章把consul注册好才能通过ocelot的配置进行服务切换,ocelot里有很多功能,包括对consul的负载均衡等,详细的可自己查资料了解.

如果不懂,可以扫二维码交流

技术图片

 

 

第六篇 网关ocelot,搭配consul。达到负载均衡的效果

标签:contain   his   ams   ide   youdao   ase   stc   host   netcore   

原文地址:https://www.cnblogs.com/iversonlzy/p/12604120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!