Ocelot和Consul 实现网关API 服务注册 负载均衡

 

Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器与Service Fabric、Butterfly Tracing集成。这些功能只都只需要简单的配置即可完成

Consul 服务发现

在Ocelot已经支持简单的负载功能,也就是当下游服务存在多个结点的时候,Ocelot能够承担起负载均衡的作用。但是它不提供健康检查,服务的注册也只能通过手动在配置文件里面添加完成。这不够灵活并且在一定程度下会有风险。这个时候我们就可以用Consul来做服务发现,它能与Ocelot完美结合。

Consul是什么

Consul是一个服务网格(微服务间的 TCP/IP,负责服务之间的网络调用、限流、熔断和监控)解决方案,它是一个一个分布式的,高度可用的系统,而且开发使用都很简便。它提供了一个功能齐全的控制平面,主要特点是:服务发现、健康检查、键值存储、安全服务通信、多数据中心。

安装Consul:windos环境

https://www.consul.io/downloads.html 下载文件

放到D盘下

 

 

 

 

 打开powershell 执行 命令启动一个Agent的开发模式:

 

 

 启动完成:打开localhost:8500  默认端口8500 就可以看到一个可视化consul界面了,可以看到默认启动了一个consul 服务

 

下面,使用netcore通过Ocelot搭建API网关   服务注册   负载均衡

新建一个api1:nuget 添加一个consul 依赖

 startup 添加代码

复制代码
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime)
       {
           if (env.IsDevelopment())
           {
               app.UseDeveloperExceptionPage();
           }
 
 
           //注册项目启动的方法
           lifetime.ApplicationStarted.Register(OnStart);
           //注册项目关闭的方法
           lifetime.ApplicationStarted.Register(OnStopped);
 
           app.UseMvc();
       }
 
       //关闭的时候在consul中移除
       private void OnStopped()
       {
           var client = new ConsulClient();
           //根据ID在consul中移除当前服务
           client.Agent.ServiceDeregister("servicename:93");
       }
       private void OnStart()
       {
           var client = new ConsulClient();
           //健康检查
           var httpCheck = new AgentServiceCheck()
           {
               //服务出错一分钟后 会自动移除
               DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1),
               //每10秒发送一次请求到 下面的这个地址 这个地址就是当前API资源的地址
               Interval = TimeSpan.FromSeconds(10),
               HTTP = $"http://localhost:93/HealthCheck"
           };
 
           var agentReg = new AgentServiceRegistration()
           {
               //这台资源服务的唯一ID
               ID = "servicename:93",
               Check = httpCheck,
               Address = "localhsot",
               Name = "servicename",
               Port = 93
           };
           client.Agent.ServiceRegister(agentReg).ConfigureAwait(false);
       }
复制代码

 

添加一个健康检查接口

复制代码
[Route("HealthCheck")]
   [ApiController]
   public class HealthCheckController : ControllerBase
   {
       // GET: api/HealthCheck
       [HttpGet]
       [HttpHead]
       public IActionResult Ping()
       {
           return Ok();
       }
 
   }
复制代码

 

 修改ValuesController

复制代码
[Route("api/[controller]")]
   [ApiController]
   public class ValuesController : ControllerBase
   {
       // GET api/values
       [HttpGet]
       public string Get()
       {
           return "这是1资源服务器API";
       }
   }
复制代码

 

 
然后 copy 一份api1 为api2,,我们启用端口为92 ,细节........

添加项目  Api.Gateway(Ocelot网关服务器) 添加Ocelot包   添加Ocelot.Json配置  Ocelot服务器端口为91

Ocelot,Json

复制代码
{
  "ReRoutes": [
    {
      //暴露出去的地址
      "UpstreamPathTemplate": "/api/{controller}",
      "UpstreamHttpMethod": [ "Get" ],
 
      //转发到下面这个地址
      "DownstreamPathTemplate": "/api/{controller}",
      "DownstreamScheme": "http",
      //资源服务器列表
      "DownstreamHostAndPorts": [
        {
          "host": "localhost",
          "port": 92
        },
        {
          "host": "localhost",
          "port": 93
        }
      ],
      //决定负载均衡的算法
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      },
      "UseServiceDiscovery": true
    }
  ],
  //对外暴露的访问地址  也就是Ocelot所在的服务器地址
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:91"
  }
}
复制代码

 

Startup 修改

复制代码
public void ConfigureServices(IServiceCollection services)
  {
 
 
      services.AddOcelot();
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  }
 
  // 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.UseOcelot().Wait();
      app.UseMvc();
  }
复制代码

 

Program 修改

复制代码
public class Program
   {
       public static void Main(string[] args)
       {
           CreateWebHostBuilder(args).Build().Run();
       }
 
       public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
           WebHost.CreateDefaultBuilder(args)
                     .ConfigureAppConfiguration((hostingContext, builder) =>
                     {
                         builder
                         .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                         .AddJsonFile("Ocelot.json");
                     })
             .UseUrls("http://+:91")
             .UseStartup<Startup>();
   }
复制代码

 

启动 api3 和api2

 

 

再启动Api.Gateway

执行 http://localhost:91/api/values

 

 再执行

 

 我们这里负载均衡算法是:LeastConnection

当然还可以选择其他的:

  • RoundRobin - 轮询,挨着来
  • LeastConnection - 最小连接数,谁的任务最少谁来
  • NoLoadBalance - 不要负载均衡

 

如果api1 我们关掉 ,那么就只会请求到api2 ,api1的服务自动断开

posted @ 2020-08-09 11:12 tianlong88 阅读(283) 评论(0) 推荐(0)
摘要: 页面结构是这样的: 一个大的main父页面中嵌有子页面,点击菜单切换时,main页面不变,子页面切换。 a、b两个子页面中都有datepicker控件。 问题现象: 页面初始化后,在第一个子页面中初始化datepicker,正常。点击菜单切换子页面,其后的所有datepicker均不正常。 问题原因 阅读全文
posted @ 2020-07-28 11:13 tianlong88 阅读(466) 评论(0) 推荐(0)
摘要: 一、RabbitMQ概念 RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发。RabbitMQ据说具有良好的性能和时效性,同时还能够非常好的支持集群和负载部署,非常适合在较大规模 阅读全文
posted @ 2020-07-25 10:10 tianlong88 阅读(5351) 评论(0) 推荐(0)
摘要: 我们也可以手动下载URL Rewrite插件,这是官方地址:URL Rewrite下载, 安装完成后,webconfig添加如下配置。 <rewrite> <rules> <rule name="HostNameRule1"> <match url="(.*)" /> <!--匹配所有条件--> < 阅读全文
posted @ 2020-01-10 14:00 tianlong88 阅读(832) 评论(0) 推荐(0)
摘要: 1.表结构 2.根据传入id查询所有子节点及其的id 创建函数: 调用: 3.根据传入id查询所有父节点及其的id 创建函数: 调用: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办 阅读全文
posted @ 2019-12-19 11:44 tianlong88 阅读(2845) 评论(1) 推荐(0)
摘要: 在 hosts 文件里加入如下的配置: 注册码: 阅读全文
posted @ 2019-04-03 10:58 tianlong88 阅读(2155) 评论(1) 推荐(0)
摘要: 在spring boot 1.5.x中,resources/static目录下的静态资源可以直接访问,并且访问路径上不用带static,比如静态资源放置位置如下图所示: 那么访问静态资源的路径可以是: http://localhost:8080/views/demoindex.html http:/ 阅读全文
posted @ 2019-03-22 11:56 tianlong88 阅读(1068) 评论(0) 推荐(0)
摘要: 开发环境:IDEA, SprngBoot 2.0.4, Maven 2.19.1工程结构: 父工程father 子模块 dao (用于持久化数据跟数据库交互) 子模块 entity (实体类) 子模块 service (处理业务逻辑) 子模块 web (页面交互接收、传递数据,唯一有启动类的模块) 阅读全文
posted @ 2019-03-18 16:21 tianlong88 阅读(6607) 评论(0) 推荐(0)
摘要: 传参方式 下面通过几种不同传参方式来实现前文中实现的插入操作。 使用@Param 在之前的整合示例中我们已经使用了这种最简单的传参方式,如下: 这种方式很好理解,@Param中定义的name对应了SQL中的#{name},age对应了SQL中的#{age} 使用Map 如下代码,通过Map<Stri 阅读全文
posted @ 2019-03-08 17:08 tianlong88 阅读(541) 评论(0) 推荐(0)
摘要: 一.在asp.net core中使用多个环境 ASP.NET Core 配置是基于运行时环境, 使用环境变量。ASP.NET Core 在应用启动时读取环境变量ASPNETCORE_ENVIRONMENT,并将该值存储在 IHostingEnvironment.EnvironmentName 中。A 阅读全文
posted @ 2019-02-09 12:16 tianlong88 阅读(1223) 评论(0) 推荐(0)
点击右上角即可分享
微信分享提示