Ocelot + Consul

Ocelot allows you to specify a service discovery provider and will use this to find the host and port for the downstream service Ocelot is forwarding a request to. At the moment this is only supported in the GlobalConfiguration section which means the same service discovery provider will be used for all Routes you specify a ServiceName for at Route level.

Ocelot + Consul

 

 1. install package Ocelot.Provider.Consul and used in startup

       public void ConfigureServices(IServiceCollection services)
        {

            services.AddOcelot().AddConsul();
        }

2. adjust ocelot.json to add service discovery

{
  "Routes": [
    {
      "ServiceName": "message",
      "UseServiceDiscovery": true, //启用服务发现
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/Message/{url}",
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "LoadBalancerOptions": {
        "Type": "RoundRobin" 
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5000",
    "ServiceDiscoveryProvider": {
      "Host": "127.0.0.1",
      "Port": 8500,
      "Type": "Consul"
    }
  }
}

3. testing

Ocelot + Consul

 

上一篇:.Net Core微服务——Ocelot(3):超时、熔断、限流


下一篇:Ocelot+Consul 集群搭建实践