热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

首先nugetOcelot版本5.6.0-unstable0028Consul版本0.7.2.6根目录新建一个 Ocelot.json{ReRoutes:[{Downstre

首先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()
             .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。达到负载均衡的效果


推荐阅读
author-avatar
手机用户2602915671
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有