介绍

微服务中有关键的几项技术,其中网关和服务服务发现,服务注册相辅相成。

首先解释几个本次教程中需要的术语

网关 Gateway(API GW / API 网关),顾名思义,是企业 IT 在系统边界上提供给外部访问内部接口服务的统一入口,简化了外部由于多服务协同完成任务时的繁琐配置。网关组件有Kong,ocelot,

服务发现:通过网关访问内部各个微服务,网关要找到所需服务的过程称为服务发现

服务注册:既然有服务发现,前提是要把所需服务提前“录入”,这个录入的过程称为服务注册。服务注册可配置文件(人肉方式不推荐),也可用服务注册组件如Consul或者Eureka等等(推荐)

搭建Consul集群(Windows)

官网下载Consul程序,https://www.consul.io/downloads.html

下载下来就是一个可执行文件Consul.exe

Consul有两种代理模式,一种server,一种client,官方建议Server端达到3台以上才可高可用,但不要太多,太多会给集群间数据同步造成压力,client数量不限。

多个server端之间会选择出一个leader,当一个server的leader宕机则会从其他server端”投票“选择新的leader

实践

这里server我们用2台实验

192.168.74.55

192.168.74.54

1台Client

192.168.74.161

consul启动有两种方式一种是命令行,一种是配置文件的方式。

命令行方式启动一个consul的server端

consul agent -server -ui -bootstrap-expect 2 -data-dir opt/consul/data -node ServerMaster -bind 192.168.74.55 -client 192.168.74.55
关键参数说明
-server:server模式启动
-ui :开启ui界面(consul.exe内部带了GUI图形界面操作)
 -bootstrap-expect 2:server端到2个时集群生效
-data-dir:consul产生的文件路径(consul自己会产生一下数据存储的位置)
-node:此节点名称
-bind:集群内部通信地址,默认0.0.0.0
-client:此节点绑定的通讯地址
以上只是关键参数,以下是完整参数说明: 
 
 

但是命令启动很繁琐,所以推荐下面的配置文件的方式启动

在consul同文件夹下建立一个server.json的配置文件

  1. {
  2. "datacenter": "dc1",
  3. "data_dir": "opt/consul/data",
  4. "node_name": "consul-server01",
  5. "server": true,
  6. "bootstrap_expect": 2,
  7. "bind_addr": "192.168.74.55",
  8. "client_addr": "192.168.74.55",
  9. "ui":true
  10. }

为了快速启动,再建立一个bat批处理文件runconsul.bat

  1. consul agent -config-dir server.json
  2. pause

双击runconsul.bat启动consul

在192.168.74.54服务器开启一个server端继续以上操作。

命令方式启动

consul agent -server -ui -data-dir opt/consul/data -node Server01 -bind 192.168.74.54 -client 192.168.74.54 -join=192.168.74.55

-join将192.168.74.54加入到192.168.74.55服务器

配置文件方式:

  1. {
  2. "datacenter": "dc1",
  3. "data_dir": "opt/consul/data",
  4. "node_name": "consul-server2",
  5. "server": true,
  6. "bind_addr": "192.168.74.54",
  7. "client_addr": "192.168.74.54",
  8. "ui":true,
  9. "retry_join": ["192.168.74.55"],
  10. "retry_interval": "30s",
  11. "rejoin_after_leave": true,
  12. "start_join":["192.168.74.55"]
  13. }

在192.168.74.161服务器开启一个consul的client端

命令方式:

consul agent -ui -data-dir opt/consul/data -node ServerSlave  -bind 192.168.74.161 -client 192.168.74.161 -join 192.168.74.55

配置文件方式:

  1. {
  2. "datacenter": "dc1",
  3. "data_dir": "opt/consul/data",
  4. "node_name": "consul-client01",
  5. "server": false,
  6. "bind_addr": "192.168.74.161",
  7. "client_addr": "192.168.74.161",
  8. "ui":true,
  9. "retry_join": ["192.168.74.55"],
  10. "retry_interval": "30s",
  11. "rejoin_after_leave": true,
  12. "start_join":["192.168.74.55"]
  13. }

效果

简单Consul集群到这里就搭建成功,只要访问三台服务器任意一个都可数据同步,演示:

netcore集成Consul服务注册

首先新建一个ConsulClient的类库

ConsulRegister.csproj所需组件如下:

  1. <Project Sdk="Microsoft.NET.Sdk">
  2.  
  3. <PropertyGroup>
  4. <TargetFramework>netstandard2.0</TargetFramework>
  5. </PropertyGroup>
  6. <ItemGroup>
  7. <PackageReference Include="Consul" Version="0.7.2.6" />
  8. <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.0" />
  9. <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.0" />
  10. <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.1.0" />
  11. <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.0" />
  12. <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.0" />
  13. </ItemGroup>
  14.  
  15. </Project>
  1.  服务发现自动注册,无需手动绑定本机地址,会自动扫描本地ipv4地址和localhost地址,项目中无需再手动创建健康检查接口
  1. ServiceDiscoveryOptions.cs
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. namespace ConsulRegister
  8. {
  9. /// <summary>
  10. /// 服务治理第三方组件Consul相关配置参数
  11. /// </summary>
  12. public class ServiceDiscoveryOptions
  13. {
  14. public string ServiceName { get; set; }
  15.  
  16. public ConsulOptions Consul { get; set; }
  17. }
  18.  
  19. public class ConsulOptions
  20. {
  21. public string HttpEndPoint { get; set; }
  22. }
  23. }
  1. RegisterToConsulExtension.cs
  2.  
  3. using Consul;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Hosting.Server.Features;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.Http.Features;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Microsoft.Extensions.Options;
  12. using System;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Net.NetworkInformation;
  16. using System.Net.Sockets;
  17.  
  18. namespace ConsulRegister
  19. {
  20. public static class RegisterToConsulExtension
  21. {
  22. /// <summary>
  23. /// Add Consul
  24. /// 添加consul
  25. /// </summary>
  26. /// <param name="services"></param>
  27. /// <param name="configuration"></param>
  28. /// <returns></returns>
  29. public static IServiceCollection AddConsul(this IServiceCollection services, IConfiguration configuration)
  30. {
  31. // configuration Consul register address
  32. //配置consul注册地址
  33. services.Configure<ServiceDiscoveryOptions>(configuration.GetSection("ServiceDiscovery"));
  34.  
  35. //configuration Consul client
  36. //配置consul客户端
  37. services.AddSingleton<IConsulClient>(sp => new Consul.ConsulClient(config =>
  38. {
  39. var consulOptions = sp.GetRequiredService<IOptions<ServiceDiscoveryOptions>>().Value;
  40. if (!string.IsNullOrWhiteSpace(consulOptions.Consul.HttpEndPoint))
  41. {
  42. config.Address = new Uri(consulOptions.Consul.HttpEndPoint);
  43. }
  44. }));
  45.  
  46. return services;
  47. }
  48.  
  49. /// <summary>
  50. /// use Consul
  51. /// 使用consul
  52. /// The default health check interface format is http://host:port/HealthCheck
  53. /// 默认的健康检查接口格式是 http://host:port/HealthCheck
  54. /// </summary>
  55. /// <param name="app"></param>
  56. /// <returns></returns>
  57. public static IApplicationBuilder UseConsul(this IApplicationBuilder app)
  58. {
  59. IConsulClient consul = app.ApplicationServices.GetRequiredService<IConsulClient>();
  60. IApplicationLifetime appLife = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
  61. IOptions<ServiceDiscoveryOptions> serviceOptions = app.ApplicationServices.GetRequiredService<IOptions<ServiceDiscoveryOptions>>();
  62. var features = app.Properties["server.Features"] as FeatureCollection;
  63.  
  64. var port = new Uri(features.Get<IServerAddressesFeature>()
  65. .Addresses
  66. .FirstOrDefault()).Port;
  67. Console.ForegroundColor = ConsoleColor.Blue;
  68. Console.WriteLine($"application port is :{port}");
  69. var addressIpv4Hosts = NetworkInterface.GetAllNetworkInterfaces()
  70. .OrderByDescending(c => c.Speed)
  71. .Where(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up);
  72.  
  73. foreach (var item in addressIpv4Hosts)
  74. {
  75. var props = item.GetIPProperties();
  76. //this is ip for ipv4
  77. //这是ipv4的ip地址
  78. var firstIpV4Address = props.UnicastAddresses
  79. .Where(c => c.Address.AddressFamily == AddressFamily.InterNetwork)
  80. .Select(c => c.Address)
  81. .FirstOrDefault().ToString();
  82. var serviceId = $"{serviceOptions.Value.ServiceName}_{firstIpV4Address}:{port}";
  83.  
  84. var httpCheck = new AgentServiceCheck()
  85. {
  86. DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(),
  87. Interval = TimeSpan.FromSeconds(),
  88. //this is default health check interface
  89. //这个是默认健康检查接口
  90. HTTP = $"{Uri.UriSchemeHttp}://{firstIpV4Address}:{port}/HealthCheck",
  91. };
  92.  
  93. var registration = new AgentServiceRegistration()
  94. {
  95. Checks = new[] { httpCheck },
  96. Address = firstIpV4Address.ToString(),
  97. ID = serviceId,
  98. Name = serviceOptions.Value.ServiceName,
  99. Port = port
  100. };
  101.  
  102. consul.Agent.ServiceRegister(registration).GetAwaiter().GetResult();
  103.  
  104. //send consul request after service stop
  105. //当服务停止后想consul发送的请求
  106. appLife.ApplicationStopping.Register(() =>
  107. {
  108. consul.Agent.ServiceDeregister(serviceId).GetAwaiter().GetResult();
  109. });
  110.  
  111. Console.ForegroundColor = ConsoleColor.Blue;
  112. Console.WriteLine($"health check service:{httpCheck.HTTP}");
  113. }
  114.  
  115. //register localhost address
  116. //注册本地地址
  117. var localhostregistration = new AgentServiceRegistration()
  118. {
  119. Checks = new[] { new AgentServiceCheck()
  120. {
  121. DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(),
  122. Interval = TimeSpan.FromSeconds(),
  123. HTTP = $"{Uri.UriSchemeHttp}://localhost:{port}/HealthCheck",
  124. } },
  125. Address = "localhost",
  126. ID = $"{serviceOptions.Value.ServiceName}_localhost:{port}",
  127. Name = serviceOptions.Value.ServiceName,
  128. Port = port
  129. };
  130.  
  131. consul.Agent.ServiceRegister(localhostregistration).GetAwaiter().GetResult();
  132.  
  133. //send consul request after service stop
  134. //当服务停止后想consul发送的请求
  135. appLife.ApplicationStopping.Register(() =>
  136. {
  137. consul.Agent.ServiceDeregister(localhostregistration.ID).GetAwaiter().GetResult();
  138. });
  139.  
  140. app.Map("/HealthCheck", s =>
  141. {
  142. s.Run(async context =>
  143. {
  144. await context.Response.WriteAsync("ok");
  145. });
  146. });
  147. return app;
  148. }
  149. }
  150. }

再新建一个.netcore的webapi项目WebA,并且引用ConsulRegister项目

在WebA项目中的Startup.cs文件中加入Consul服务

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddConsul(Configuration);
  4. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  5. }
  6.  
  7. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  8. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  9. {
  10. if (env.IsDevelopment())
  11. {
  12. app.UseDeveloperExceptionPage();
  13. }
  14.  
  15. app.UseConsul();
  16. app.UseMvc();
  17. }

在WebA项目的appsettings.json配置文件中加入以下Consul服务端配置

  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Warning"
  5. }
  6. },
  7. "AllowedHosts": "*",
  8.  
  9. "ServiceDiscovery": {
  10. "ServiceName": "A",
  11. "Consul": {
  12. "HttpEndpoint": "http://192.168.74.161:8500"
  13. }
  14. }
  15. }

这里服务注册就算完成

Ocelot网关搭建

接下来继续Ocelot借助于Consul实现服务发现

新建项目Ocelot.Gateway

将以下依赖加入Ocelot.Gateway.csproj中:

  1. <Project Sdk="Microsoft.NET.Sdk.Web">
  2.  
  3. <PropertyGroup>
  4. <TargetFramework>netcoreapp2.1</TargetFramework>
  5. </PropertyGroup>
  6.  
  7. <ItemGroup>
  8. <PackageReference Include="Microsoft.AspNetCore.App" />
  9. <PackageReference Include="Ocelot" Version="12.0.1" />
  10. <PackageReference Include="Ocelot.Provider.Consul" Version="0.1.2" />
  11. </ItemGroup>
  12.  
  13. <ItemGroup>
  14. <Content Update="ocelot.json">
  15. <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  16. </Content>
  17. </ItemGroup>
  18.  
  19. </Project>

新建ocelot.json文件

  1. {
  2. "ReRoutes": [
  3. {
  4. "UseServiceDiscovery": true,
  5. "DownstreamPathTemplate": "/{url}",
  6. "DownstreamScheme": "http",
  7. "ServiceName": "A",
  8. "LoadBalancerOptions": {
  9. "Type": "RoundRobin"
  10. },
  11. "UpstreamPathTemplate": "/a/{url}",
  12. "UpstreamHttpMethod": [ "Get", "Post" ],
  13. "ReRoutesCaseSensitive": false
  14. }
  15. ],
  16. "GlobalConfiguration": {
  17. // 使用Consul服务治理
  18. "ServiceDiscoveryProvider": {
  19. "Host": "192.168.74.161",
  20. "Port": ,
  21. "ConfigurationKey": "Oceolot_A" //存储在Consul上的Key
  22. }
  23. }
  24. }

修改Startup.cs文件如下:

  1. public class Startup
  2. {
  3. public Startup(IConfiguration configuration)
  4. {
  5. Configuration = configuration;
  6. }
  7.  
  8. public IConfiguration Configuration { get; }
  9.  
  10. // This method gets called by the runtime. Use this method to add services to the container.
  11. public void ConfigureServices(IServiceCollection services)
  12. {
  13. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  14.  
  15. services.AddOcelot(
  16. new ConfigurationBuilder()
  17. .AddJsonFile("ocelot.json", optional: false, reloadOnChange: true).Build())
  18. .AddConsul()
  19. .AddConfigStoredInConsul();
  20. }
  21.  
  22. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  23. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  24. {
  25. if (env.IsDevelopment())
  26. {
  27. app.UseDeveloperExceptionPage();
  28. }
  29. else
  30. {
  31. app.UseHsts();
  32. }
  33.  
  34. app.UseHttpsRedirection();
  35. app.UseOcelot().Wait();
  36. }
  37. }

发布WebA后复制两份分别启动

dotnet WebA.dll --urls="http://0.0.0.0:2001"

dotnet WebA.dll --urls="http://0.0.0.0:2002"

到这里相当于2001和2002程序简单集群了一下

可以发现日志中有 http://192.168.74.161:2002/HealthCheck调用信息:

这其实是consul进行健康检查进行的调用。

启动多个程序后,打开浏览器打开Consuld界面会发现注册了两个服务

这里ocelot网关和consul的服务注册和发现就算初步集成。

生产部署

如果生产环境是windows的情况,将consul做成windwos服务即可

  1. sc create "ConsulServer" binPath="F:\XXX\consul.exe agent -config-dir XXX.json"

生产环境是linux则借助systemd做成守护进程即可

生产环境是docker,运行以下命令部署单节点,集群类似

  1. docker run --restart=always -d --name=c13 -p : consul agent -server -bootstrap -ui -data-dir tmp/consul -bind 0.0.0.0 -client 0.0.0.0 -node dockerserver

目前集群搭建成功,但是连接的话如果指定某个端点的ip进行连接,端点宕机,就会导致网关一样无法连接consul进行服务发现。所以还需进行配置暴露一个端点让客户端连接,配置详情:https://www.consul.io/docs/connect/configuration.html

不过也可以做成虚拟ip进行多台consul的负载。客户端连接虚拟ip即可

项目地址:

github地址

微服务之:从零搭建ocelot网关和consul集群的更多相关文章

  1. 庐山真面目之十微服务架构 Net Core 基于 Docker 容器部署 Nginx 集群

    庐山真面目之十微服务架构 Net Core 基于 Docker 容器部署 Nginx 集群 一.简介      前面的两篇文章,我们已经介绍了Net Core项目基于Docker容器部署在Linux服 ...

  2. 【微服务架构】SpringCloud之Eureka(注册中心集群篇)(三)

    上一篇讲解了spring注册中心(eureka),但是存在一个单点故障的问题,一个注册中心远远无法满足实际的生产环境,那么我们需要多个注册中心进行集群,达到真正的高可用.今天我们实战来搭建一个Eure ...

  3. 庐山真面目之十二微服务架构基于Docker搭建Consul集群、Ocelot网关集群和IdentityServer版本实现

    庐山真面目之十二微服务架构基于Docker搭建Consul集群.Ocelot网关集群和IdentityServer版本实现 一.简介      在第七篇文章<庐山真面目之七微服务架构Consul ...

  4. 庐山真面目之六微服务架构Consul集群、Ocelot网关集群和Nginx版本实现

    庐山真面目之六微服务架构Consul集群.Ocelot网关集群和Nginx版本实现 一.简介      在上一篇文章<庐山真面目之五微服务架构Consul集群.Ocelot网关和Nginx版本实 ...

  5. 庐山真面目之七微服务架构Consul集群、Ocelot网关集群和IdentityServer4版本实现

    庐山真面目之七微服务架构Consul集群.Ocelot网关集群和IdentityServer4版本实现 一.简介      在上一篇文章<庐山真面目之六微服务架构Consul集群.Ocelot网 ...

  6. ASP.NET Core微服务+Tabler前端框架搭建个人博客2--系统架构

    功能分析 在整个微服务架构的搭建过程中,我们需要做的第一步就是对服务进行拆分,将一个完整的系统模块化,通过对各个模块互联,共同完成一个系统的工作.既然要做到模块化,那么必须明白你的系统的需求到底是什么 ...

  7. 【NET CORE微服务一条龙应用】第一章 网关使用与配置

    简介 微服务的系统应用中,网关系统使用的是ocelot,ocelot目前已经比较成熟了 ocelot就不做介绍了,等整体介绍完后再进行各类扩展介绍,ocelot源码地址:https://github. ...

  8. 8分钟学会Consul集群搭建及微服务概念

    Consul介绍: Consul 是由 HashiCorp 公司推出的开源软件,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发现的方案,Consul 的方案更“一站式”,内置了服务注册与 ...

  9. Ocelot+Consul 集群搭建实践

    博客园已经有很多大神写过consul集群搭建了.大家都在玩,那我也不能托后退呢 不过自己研究下还是好的.毕竟每个人遇到的问题的不同 研究过才能说自己玩过consul,文章有部分名词解释是收集网络 Co ...

随机推荐

  1. 前端AntD框架的upload组件上传图片时遇到的一些坑

    前言 本次做后台管理系统,采用的是 AntD 框架.涉及到图片的上传,用的是AntD的 upload 组件. 前端做文件上传这个功能,是很有技术难度的.既然框架给我们提供好了,那就直接用呗.结果用的时 ...

  2. Cas 服务器 JDBC身份校验

    之前的Cas服务器一直使用静态配置的账号密码进行身份认证,现在要让Cas服务器通过MySQL数据库中的用户信息进行身份认证. 一.添加数据库访问依赖 <!-- https://mvnreposi ...

  3. 最详细的C++对应C#的数据类型转换

    C++ ---------------------- C# LDWORD ----------------IntPtr LLONG-------------------Intptr bool ---- ...

  4. [20181007]12cR2 Using SQL Patch.txt

    [20181007]12cR2 Using SQL Patch.txt --//12cR2 已经把sql打补丁集成进入dbms_sqldiag,不是11g的 DBMS_SQLDIAG_INTERNAL ...

  5. C#-类(九)

    类的定义 类是描述具有相同特征与行为的事物的抽象,类内部包含类的特征和类的行为 类支持继承 类的定义是关键字class为标志 类的格式 访问标识符 class 类名 { 类主体 } 访问标识符:指定了 ...

  6. SQL SERVER 查询表字段中是否有汉字

    SELECT * FROM TB WHERE COL LIKE N'%[吖-咗]%'

  7. App分享之微信微博等各个社交平台的分享授权规则和常见问题

    一.新浪微博分享规则 新浪微博支持分享类型: 应用内分享也就是网页分享支持: 文字,文字+图片,要分享链接需要链接添加在text里分享 客户端分享支持:文字,图片,文字+图片,图片+文字+链接 参数说 ...

  8. Amazon onsite behavior question

    https://www.1point3acres.com/bbs/thread-307462-1-1.html http://kraftshala.com/how-to-raise-the-bar-i ...

  9. VMare Workstation 安装Ubuntu 虚拟机教程

    1.VMware Workstation,选择左上角文件—新建虚拟机,开始新建一台虚拟机,典型的话许多配置为默认设置,因此,这里我选择自定义安装: 2.机硬件兼容性选择默认即可: 3.客户操作系统选择 ...

  10. Git&GitHub-基础教程

    目录 1. Git简介 1.1 什么是版本控制系统? 1.2. Git的历史 1.3. 什么是分布式?什么是集中式? 2. Git安装 3. 创建一个版本库 4. Git的语法教程 4.1. 提交一个 ...