API 网关通常放到微服务的最前端,而且要让API 网关变成由应用所发起的每一个请求的入口。这样就能够明显的简化客户端实现和微服务应用程序之间的沟通方式。之前的话,客户端不得不去请求微服务A,而后再到微服务B,而后是微服务C。客户端须要去知道怎么去一块儿来消费这三个不一样的service。使用API网关,咱们能够抽象全部这些复杂性,并建立客户端们可使用的优化后的端点,并向那些模块们发出请求。API网关的核心要点是:全部的客户端和消费端都经过统一的网关接入微服务,在网关层处理全部的非业务功能(好比验证、鉴权、监控、限流、请求合并...)前端
Ocelot是一个使用.NET Core平台上的一个API Gateway,这个项目的目标是在.NET上面运行微服务架构。它功能强大,包括了:路由、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器与Service Fabric、Butterfly Tracing集成,还引入了Polly来进行故障处理。node
Polly是一种.NET弹性和瞬态故障处理库,容许咱们以很是顺畅和线程安全的方式来执诸如行重试,断路,超时,故障恢复等策略。json
重试策略(Retry)
重试策略针对的前置条件是短暂的故障延迟且在短暂的延迟以后可以自我纠正。容许咱们作的是可以自动配置重试机制。bootstrap
断路器(Circuit-breaker)
断路器策略针对的前置条件是当系统繁忙时,快速响应失败总比让用户一直等待更好。保护系统故障免受过载,Polly能够帮其恢复。api
超时(Timeout)
超时策略针对的前置条件是超过必定的等待时间,想要获得成功的结果是不可能的,保证调用者没必要等待超时。缓存
隔板隔离(Bulkhead Isolation)安全
隔板隔离针对的前置条件是当进程出现故障时,多个失败一直在主机中对资源(例如线程/ CPU)一直占用。下游系统故障也可能致使上游失败。这两个风险都将形成严重的后果。都说一粒老鼠子屎搅浑一锅粥,而Polly则将受管制的操做限制在固定的资源池中,免其余资源受其影响。架构
缓存(Cache)
缓存策略针对的前置条件是数据不会很频繁的进行更新,为了不系统过载,首次加载数据时将响应数据进行缓存,若是缓存中存在则直接从缓存中读取。app
回退(Fallback)
操做仍然会失败,也就是说当发生这样的事情时咱们打算作什么。也就是说定义失败返回操做。负载均衡
策略包装(PolicyWrap)
策略包装针对的前置条件是不一样的故障须要不一样的策略,也就意味着弹性灵活使用组合
Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置,内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案、在Ocelot已经支持简单的负载功能,也就是当下游服务存在多个结点的时候,Ocelot可以承担起负载均衡的做用。可是它不提供健康检查,服务的注册也只能经过手动在配置文件里面添加完成。这不够灵活而且在必定程度下会有风险。这个时候咱们就能够用Consul来作服务发现并实现健康检查。
文件结构信息
data
conf
consul.exe
配置conf.json信息 并运行 :consul agent -config-dir="E:/PersonCode/.Net Core/consul/conf/conf.json"
{
"datacenter": "wf",
"data_dir": "E:/PersonCode/.Net Core/consul/data",
"log_level": "INFO",
"server": true,
"ui": true,
"bind_addr": "192.168.14.8",
"client_addr": "127.0.0.1",
"advertise_addr": "192.168.14.8",
"bootstrap_expect": 1,
"ports":{
"http": 8500,
"dns": 8600,
"server": 8300,
"serf_lan": 8301,
"serf_wan": 8302
}
}
运行结果:
一、建立 api项目 添加swagger Ui
二、引用Consul-1.6.1.1版本
三、添加Consul 服务配置
"Consul": {
"ServiceName": "Zfkr.WF.Core.API",
"ServiceIP": "localhost",
"ConsulClientUrl": "http://localhost:8500",
"HealthCheckRelativeUrl": "/wf/Base/health",
"HealthCheckIntervalInSecond": 5
}
四、添加Consul 服务注册类(RegisterCansulExtension)
五、添加服务注册与中间件
六、启用api 多个服务
dotnet Zfkr.WF.Core.API.dll --urls=http://*:8001
dotnet Zfkr.WF.Core.API.dll --urls=http://*:8002
dotnet Zfkr.WF.Core.API.dll --urls=http://*:8003
public static class RegisterCansulExtension
{
public static void RegisterToConsul(this IApplicationBuilder app, IConfiguration configuration, IHostApplicationLifetime lifetime)
{
lifetime.ApplicationStarted.Register(() =>
{
string serviceName = configuration.GetValue<string>("Consul:ServiceName");
string serviceIP = configuration.GetValue<string>("Consul:ServiceIP");
string consulClientUrl = configuration.GetValue<string>("Consul:ConsulClientUrl");
string healthCheckRelativeUrl = configuration.GetValue<string>("Consul:HealthCheckRelativeUrl");
int healthCheckIntervalInSecond = configuration.GetValue<int>("Consul:HealthCheckIntervalInSecond");
ICollection<string> listenUrls = app.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
if (string.IsNullOrWhiteSpace(serviceName))
{
throw new Exception("Please use --serviceName=yourServiceName to set serviceName");
}
if (string.IsNullOrEmpty(consulClientUrl))
{
consulClientUrl = "http://127.0.0.1:8500";
}
if (string.IsNullOrWhiteSpace(healthCheckRelativeUrl))
{
healthCheckRelativeUrl = "health";
}
healthCheckRelativeUrl = healthCheckRelativeUrl.TrimStart('/');
if (healthCheckIntervalInSecond <= 0)
{
healthCheckIntervalInSecond = 1;
}
string protocol;
int servicePort = 0;
if (!TryGetServiceUrl(listenUrls, out protocol, ref serviceIP, out servicePort, out var errorMsg))
{
throw new Exception(errorMsg);
}
var consulClient = new ConsulClient(ConsulClientConfiguration => ConsulClientConfiguration.Address = new Uri(consulClientUrl));
var httpCheck = new AgentServiceCheck()
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(10),//服务启动多久后注册
Interval = TimeSpan.FromSeconds(healthCheckIntervalInSecond),
HTTP = $"{protocol}://{serviceIP}:{servicePort}/{healthCheckRelativeUrl}",
Timeout = TimeSpan.FromSeconds(2)
};
// 生成注册请求
var registration = new AgentServiceRegistration()
{
Checks = new[] { httpCheck },
ID = Guid.NewGuid().ToString(),
Name = serviceName,
Address = serviceIP,
Port = servicePort,
Meta = new Dictionary<string, string>() { ["Protocol"] = protocol },
Tags = new[] { $"{protocol}" }
};
consulClient.Agent.ServiceRegister(registration).Wait();
//服务中止时, 主动发出注销
lifetime.ApplicationStopping.Register(() =>
{
try
{
consulClient.Agent.ServiceDeregister(registration.ID).Wait();
}
catch
{ }
});
});
}
private static bool TryGetServiceUrl(ICollection<string> listenUrls, out string protocol, ref string serviceIP, out int port, out string errorMsg)
{
protocol = null;
port = 0;
errorMsg = null;
if (!string.IsNullOrWhiteSpace(serviceIP)) // 若是提供了对外服务的IP, 只须要检测是否在listenUrls里面便可
{
foreach (var listenUrl in listenUrls)
{
Uri uri = new Uri(listenUrl);
protocol = uri.Scheme;
var ipAddress = uri.Host;
port = uri.Port;
if (ipAddress == serviceIP || ipAddress == "0.0.0.0" || ipAddress == "[::]")
{
return true;
}
}
errorMsg = $"The serviceIP that you provide is not in urls={string.Join(',', listenUrls)}";
return false;
}
else // 没有提供对外服务的IP, 须要查找本机全部的可用IP, 看看有没有在 listenUrls 里面的
{
var allIPAddressOfCurrentMachine = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
.Select(p => p.GetIPProperties())
.SelectMany(p => p.UnicastAddresses)
// 这里排除了 127.0.0.1 loopback 地址
.Where(p => p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address))
.Select(p => p.Address.ToString()).ToArray();
var uris = listenUrls.Select(listenUrl => new Uri(listenUrl)).ToArray();
// 本机全部可用IP与listenUrls进行匹配, 若是listenUrl是"0.0.0.0"或"[::]", 则任意IP都符合匹配
var matches = allIPAddressOfCurrentMachine.SelectMany(ip =>
uris.Where(uri => ip == uri.Host || uri.Host == "0.0.0.0" || uri.Host == "[::]")
.Select(uri => new { Protocol = uri.Scheme, ServiceIP = ip, Port = uri.Port })
).ToList();
if (matches.Count == 0)
{
errorMsg = $"This machine has IP address=[{string.Join(',', allIPAddressOfCurrentMachine)}], urls={string.Join(',', listenUrls)}, none match.";
return false;
}
else if (matches.Count == 1)
{
protocol = matches[0].Protocol;
serviceIP = matches[0].ServiceIP;
port = matches[0].Port;
return true;
}
else
{
errorMsg = $"Please use --serviceIP=yourChosenIP to specify one IP, which one provide service: {string.Join(",", matches)}.";
return false;
}
}
}
一、建立.core Api 项目 并引用相关包文件
Consul 1.6.11
Ocelot 16.0.1
Ocelot.Provider.Consul 16.0.1
二、添加ocelot.json文件 并配置
注意:"ServiceName": "Zfkr.WF.Core.API", 服务名称必须与第三步中的 注册服务时的 服务名称一致,若未注册服务 可配置DownstreamHostAndPorts实现负载,之因此要使用Consul 是由于Ocelot自身 没有没法 实现健康检查 服务自动添加与移除
{
"Routes": [
/*Swagger 网关*/
{
"DownstreamPathTemplate": "/swagger/WorkFlow/swagger.json",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8001
}
],
"LoadBalancer": "RoundRobin",
"UpstreamPathTemplate": "/WorkFlow/swagger/WorkFlow/swagger.json",
"UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ]
},
//{
// "UseServiceDiscovery": true, // 使用服务发现
// "DownstreamPathTemplate": "/swagger/TaskSchedul/swagger.json",
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 8002
// }
// ],
// "LoadBalancer": "RoundRobin",
// "UpstreamPathTemplate": "/TaskSchedul/swagger/TaskSchedul/swagger.json",
// "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ]
//},
/*Api网关*/
{
"UseServiceDiscovery": true, //启用服务发现,若Ocelot集合Consul必须配置此项
"DownstreamPathTemplate": "/WF/{url}",
"DownstreamScheme": "http",
//"DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 8001
// },
// {
// "Host": "localhost",
// "Port": 8003
// }
//],
"UpstreamPathTemplate": "/WF/{url}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"ServiceName": "Zfkr.WF.Core.API", //服务名称
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
}
//,
//{
// "DownstreamPathTemplate": "/TS/{url}",
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 8002
// }
// ],
// "ServiceName": "node-2", // 服务名称
// "UseServiceDiscovery": true,
// "UpstreamPathTemplate": "/TS/{url}",
// "UpstreamHttpMethod": [ "Get", "Post" ],
// "LoadBalancerOptions": {
// "Type": "LeastConnection"
// }
//}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:8899", //网关对外地址
"ReRouteIsCaseSensitive": false, //是否区分路由字母大小写
"ServiceDiscoveryProvider": { //服务发现提供者,配置Consul地址
"Host": "localhost", //Consul主机名称
"Port": 8500, //Consul端口号
"Type": "Consul" //必须指定Consul服务发现类型
}
//,
//"限流相关配置"
//"RateLimitOptions": {
// "ClientIdHeader": "ClientId",
// "QuotaExceededMessage": "RateLimit SCscHero", //限流响应提示
// "RateLimitCounterPrefix": "ocelot",
// "DisableRateLimitHeaders": false,
// "HttpStatusCode": 429
//}
}
}
三、添加Consul注册类 并注册到中间件,添加健康检查服务
3.一、添加appsettings 配置信息
"Consul": {
"ServiceName": "Gateway-9988-Service",
"Datacenter": "wf",
"ServiceIP": "localhost",
"ConsulClientUrl": "http://localhost:8500",
"HealthCheckRelativeUrl": "/wf/Base/health",
"HealthCheckIntervalInSecond": 5
}
3.二、添加Consul注册类
public static class RegisterCansulExtension
{
public static void RegisterToConsul(this IApplicationBuilder app, IConfiguration configuration, IHostApplicationLifetime lifetime)
{
lifetime.ApplicationStarted.Register(() =>
{
string serviceName = configuration.GetValue<string>("Consul:ServiceName");
string serviceIP = configuration.GetValue<string>("Consul:ServiceIP");
string consulClientUrl = configuration.GetValue<string>("Consul:ConsulClientUrl");
string healthCheckRelativeUrl = configuration.GetValue<string>("Consul:HealthCheckRelativeUrl");
int healthCheckIntervalInSecond = configuration.GetValue<int>("Consul:HealthCheckIntervalInSecond");
ICollection<string> listenUrls = app.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
if (string.IsNullOrWhiteSpace(serviceName))
{
throw new Exception("Please use --serviceName=yourServiceName to set serviceName");
}
if (string.IsNullOrEmpty(consulClientUrl))
{
consulClientUrl = "http://127.0.0.1:8500";
}
if (string.IsNullOrWhiteSpace(healthCheckRelativeUrl))
{
healthCheckRelativeUrl = "health";
}
healthCheckRelativeUrl = healthCheckRelativeUrl.TrimStart('/');
if (healthCheckIntervalInSecond <= 0)
{
healthCheckIntervalInSecond = 1;
}
string protocol;
int servicePort = 0;
if (!TryGetServiceUrl(listenUrls, out protocol, ref serviceIP, out servicePort, out var errorMsg))
{
throw new Exception(errorMsg);
}
var consulClient = new ConsulClient(ConsulClientConfiguration => ConsulClientConfiguration.Address = new Uri(consulClientUrl));
var httpCheck = new AgentServiceCheck()
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(10),//服务启动多久后注册
Interval = TimeSpan.FromSeconds(healthCheckIntervalInSecond),
HTTP = $"{protocol}://{serviceIP}:{servicePort}/{healthCheckRelativeUrl}",
Timeout = TimeSpan.FromSeconds(2)
};
// 生成注册请求
var registration = new AgentServiceRegistration()
{
Checks = new[] { httpCheck },
ID = Guid.NewGuid().ToString(),
Name = serviceName,
Address = serviceIP,
Port = servicePort,
Meta = new Dictionary<string, string>() { ["Protocol"] = protocol },
Tags = new[] { $"{protocol}" }
};
consulClient.Agent.ServiceRegister(registration).Wait();
//服务中止时, 主动发出注销
lifetime.ApplicationStopping.Register(() =>
{
try
{
consulClient.Agent.ServiceDeregister(registration.ID).Wait();
}
catch
{ }
});
});
}
private static bool TryGetServiceUrl(ICollection<string> listenUrls, out string protocol, ref string serviceIP, out int port, out string errorMsg)
{
protocol = null;
port = 0;
errorMsg = null;
if (!string.IsNullOrWhiteSpace(serviceIP)) // 若是提供了对外服务的IP, 只须要检测是否在listenUrls里面便可
{
foreach (var listenUrl in listenUrls)
{
Uri uri = new Uri(listenUrl);
protocol = uri.Scheme;
var ipAddress = uri.Host;
port = uri.Port;
if (ipAddress == serviceIP || ipAddress == "0.0.0.0" || ipAddress == "[::]")
{
return true;
}
}
errorMsg = $"The serviceIP that you provide is not in urls={string.Join(',', listenUrls)}";
return false;
}
else // 没有提供对外服务的IP, 须要查找本机全部的可用IP, 看看有没有在 listenUrls 里面的
{
var allIPAddressOfCurrentMachine = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
.Select(p => p.GetIPProperties())
.SelectMany(p => p.UnicastAddresses)
// 这里排除了 127.0.0.1 loopback 地址
.Where(p => p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address))
.Select(p => p.Address.ToString()).ToArray();
var uris = listenUrls.Select(listenUrl => new Uri(listenUrl)).ToArray();
// 本机全部可用IP与listenUrls进行匹配, 若是listenUrl是"0.0.0.0"或"[::]", 则任意IP都符合匹配
var matches = allIPAddressOfCurrentMachine.SelectMany(ip =>
uris.Where(uri => ip == uri.Host || uri.Host == "0.0.0.0" || uri.Host == "[::]")
.Select(uri => new { Protocol = uri.Scheme, ServiceIP = ip, Port = uri.Port })
).ToList();
if (matches.Count == 0)
{
errorMsg = $"This machine has IP address=[{string.Join(',', allIPAddressOfCurrentMachine)}], urls={string.Join(',', listenUrls)}, none match.";
return false;
}
else if (matches.Count == 1)
{
protocol = matches[0].Protocol;
serviceIP = matches[0].ServiceIP;
port = matches[0].Port;
return true;
}
else
{
errorMsg = $"Please use --serviceIP=yourChosenIP to specify one IP, which one provide service: {string.Join(",", matches)}.";
return false;
}
}
}
}
四、运行网关服务 dotnet Zfkr.WF.Core.Gateway.dll --urls=http://*:9988