0
点赞
收藏
分享

微信扫一扫

asp.net+ocelot+consul,持续更新

魔都魅影梅杜萨 2022-04-24 阅读 45

文章目录

声明

我们是制造业企业,技术栈焊死在c#上,所以只能用c#搭建微服务应用。如果纯粹想用微服务,建议转spring cloud。

事前准备

  • 建一个业务web应用记为app。
  • 建一个最简单的web api应用记为 gateway。
  • 去官网下载consul 。

建立网关

  • gateway项目安装Ocelot。
  • 新建一个ocelot.json文件,与appsettings.json同级。内容参考官网,这里给一个范例:
{
  "Routes": [
    {
      //这段话的意思是用get方式通过/gateway/{urls}形式访问gateway程序的请求都将转发到http://localhost:5006/{urls}
      "DownstreamPathTemplate": "/{urls}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5006
        }
      ],
      "UpstreamPathTemplate": "/gateway/{urls}",
      "UpstreamHttpMethod": ["Get"]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5000"//gateway自身的根路径
  }
}

  • 将ocelot.json添加到系统内
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("ocelot.json");
  • 添加ocelot
builder.Services.AddOcelot();
  • 启用ocelot
app.UseOcelot().Wait();
  • 现在应该能实现访问跳转了,不过我们看到配置文件里是写死了跳转地址的,如果服务失效则没法自动切换,没法像Nacos+Feign那样做到无缝切换,所以得引入consul。

启动Consul

  • 将下载的压缩包解压,得出一个consul.exe文件。
  • 将consul.exe注册为环境变量(注册环境变量是个基础知识,自行百度)。
  • 在同级目录下,写一个server.json,官网范例就可以了。
  • 编写一个bat,内容是
consul agent -config-file=server.json -bind=服务器对外暴露的ip地址
  • 看到 agent: Consul agent running!就算成功了。
  • 访问 http://服务器地址:8500/ 可以看到consul的后台可视化页面

应用APP注册到consul上

  • 在app项目里安装consul
  • 在项目启动方法里添加注册代码,我用的是abp.vnext,所以是在OnApplicationInitialization方法里。
        var client = new ConsulClient();
        client.Config.Address = new Uri("http://consul服务器的ip地址:8500");
        var checker = new AgentServiceCheck()
        {
            DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(10),//失去响应后多久,踢出服务实例
            Interval = TimeSpan.FromSeconds(5),//多久响应一次
            HTTP = "http://本应用的baseurl/consul/healthCheck"//这个地址就是consul回调来检查服务实例是否存活的,要保证200
        };
        var register = new AgentServiceRegistration()
        {
            ID = Guid.NewGuid().ToString(),
            Check = checker,
            Address = "本应用所部属的服务器ip地址",
            Name = "agv",//服务实例分组
            Port = 10007//本服务所暴露的端口
        };
        client.Agent.ServiceRegister(register).ConfigureAwait(false);
  • 在项目里建一个api controller,来为上文consul的回调提供200接口,此处将该接口记为alive interface。
    public class ConsulController : AbpController
    {
        [HttpGet]
        public IActionResult HealthCheck()
        {
            return Ok();
        }
    }
  • 然后就可以把应用发布到服务上了。
  • 那么稍等片刻查看consul的后台就会出现一个新的服务组。一开始的状态可能是critical,但只要保证服务器的alive interface处于200状态,过会自然会好。
    在这里插入图片描述

gateway通过consul获取跳转地址

  • 在gateway项目里安装Ocelot.Provider.Consul
builder.Services.AddOcelot();

改为

builder.Services.AddOcelot().AddConsul();
  • 改ocelot.json,其中负责均衡模式参考官网文档
{
  "Routes": [
    {
      "DownstreamPathTemplate": "/{urls}",
      "DownstreamScheme": "https",

      "UpstreamPathTemplate": "/gateway/{urls}",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "agv", //之前app注册的服务组
      "LoadBalancerOptions": {
        "Type": "LeastConnection" //负载均衡模式
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5000",
    "ServiceDiscoveryProvider": {
      "Scheme": "http",
      "Host": "consul服务器对外暴露的ip地址",
      "Port": 8500,
      "Type": "Consul"
    }
  }
}
  • 此时应该能跳转过去了
举报

相关推荐

0 条评论