.NET Core Ocelot Consul

管理 NuGet 程序包
Ocelot
Consul
Ocelot.Provider.Consul

---------【ocelot + api】---------
优点:负载均衡
缺点:不能动态伸缩
1、如果某个业务站点挂了,站点不能正常访问了,ocelot网关还是会转发给挂了的站点;
2、如果新建了1个新的业务站点,ocelot网关本身是发现不了新建的业务站点的;
3、客户端始终访问的是网关站点【9500】,为了保证可用性,追求极致,搞了3次请求,2次成功,1次失败,这是不允许的,不达标的。

管理 NuGet 程序包
Ocelot

1、IIS部署Ocelot网关站点
localhost:9500

2、IIS部署3个业务站点
localhost:9526
localhost:9527
localhost:9528

3、Ocelot项目Startup.cs文件
读取 configuration.json 文件,添加Ocelot

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace OcelotProject
{
  public class Startup
  {
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddOcelot(new ConfigurationBuilder()
        .AddJsonFile("configuration.json")
        .Build());
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      app.UseOcelot();
    }
  }
}

4、配置Ocelot网关
添加configuration.json 配置文件

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/WeatherForecast",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 9526
        },
        {
          "Host": "localhost",
          "Port": 9527
        },
        {
          "Host": "localhost",
          "Port": 9528
        }
      ],
      "UpstreamPathTemplate": "/api/WeatherForecast",
      "UpstreamHttpMethod": [ "Get" ],
      "LoadBalancerOptions": { "Type": "RoundRobin" }
    }
  ]
}

---------【ocelot + consul + api】---------
优点:负载均衡,动态伸缩
1、使用Consul来完成服务注册,服务发现;
2、在业务系统中添加Consul功能;
3、客户端始终访问的是网关站点【9500】;

管理 NuGet 程序包
Ocelot
Consul
Ocelot.Provider.Consul

1、启动Consul服务
打开Consul目录,cmd
consul agent -dev
打开浏览器,地址栏输入http://127.0.0.1:8500

2、IIS部署Ocelot网关站点
localhost:9500

3、IIS部署3个业务站点
localhost:9526
localhost:9527
localhost:9528

4、Ocelot项目Startup.cs文件
读取 ocelotConsul.json 文件,添加Ocelot,Consul

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;

namespace OcelotProject
{
  public class Startup
  {
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddOcelot(new ConfigurationBuilder()
        .AddJsonFile("ocelotConsul.json")
        .Build())
        .AddConsul();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      app.UseOcelot();
    }
  }
}

5、配置Ocelot网关
添加ocelotConsul.json 配置文件

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/WeatherForecast",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/api/WeatherForecast",
      "UpstreamHttpMethod": [ "Get" ],
      "UseServiceDiscovery": true,
      "ServiceName": "AssetService", //Consul服务名称
      "LoadBalancerOptions": { "Type": "RoundRobin" }
    }
  ],
  "GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    }
  }
}

6、业务系统中添加Consul服务,查看.NET Core Consul文章
---------【将consul做成windwos服务】---------
在【运行】输入【services.msc】查看系统服务

sc create "ConsulService" binPath="D:\Consul\consul.exe agent -dev"

--启动Consul服务
sc start "ConsulService"

--删除Consul服务
sc delete "ConsulService"

--检查Consul服务
打开浏览器,地址栏输入http://127.0.0.1:8500

*
*
*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值