(精华)2020年8月22日 微服务 Ocelot API网关的使用(.net core版)
Ocelot 相关地址:
https://github.com/ThreeMammals/Ocelot
Ocelot 单独使用,可以查看“Ocelot的简单使用”这篇文章
本文接着“Consul服务中心”这篇文章,通过cmd运行起“Student”和“Teacher”服务,接下来就是创建网关项目
一、新建webapi项目,命名为“Ocelot_Consul”,去掉HTTPS勾选,不需要Controller,改为控制台方式启动
二、打开程序包管理器控制台,依次执行命令:
Install-Package Ocelot
Install-Package Ocelot.Provider.Consul
Install-Package Ocelot.Provider.Polly
三、在项目根目录下,新建配置文件“ocelot.json”
"LoadBalancer"负载均衡类型:
RoundRobin:轮询机制,循环找到可以用的服务
LeastConnection:最少连接数,跟踪发现现在有最少请求或处理的可用服务
NoLoadBalancer:不使用负载均衡,直接访问第一个发现的可用的服务
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/Default/GetList",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/v1/Student/GetList",
"UpstreamHttpMethod": [ "Get" ],
"ServiceName": "Student",
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
"UseServiceDiscovery": true
// ,
//"RateLimitOptions": {
// "ClientWhitelist": [], //白名单
// "EnableRateLimiting": true, //是否限流
// "Period": "30s", //指定一个时间
// "PeriodTimespan": 10, //多少时间后,可以重新请求。
// "Limit": 5 //在Period的指定时间内,最多请求次数
//}
},
{
"DownstreamPathTemplate": "/api/Default/GetList",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/v1/Teacher/GetList",
"UpstreamHttpMethod": [ "Get" ],
"ServiceName": "Teacher",
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
"UseServiceDiscovery": true
}
],
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Host": "127.0.0.1",
"Port": 8500
}
}
}
四、在Program.cs的CreateHostBuilder中加入
.ConfigureAppConfiguration(conf => {
conf.AddJsonFile("ocelot.json", false, true);
})
五、找到Startup.cs
在ConfigureServices中加入:
services.AddOcelot().AddConsul().AddPolly();;
在Configure中加入:
app.UseOcelot().Wait();
六、通过VS启动“Ocelot_Consul”,由于“ocelot.json”配置的对外的路由
“Student”服务的访问地址为:http://localhost:5000/api/v1/Student/GetList
“Teacher”服务的访问地址为:http://localhost:5000/api/v1/Teacher/GetList
代码:https://files.cnblogs.com/files/shousiji/Ocelot_Consul.rar
ocelot配置文件详解
*****************************单地址********************************
//{
// "ReRoutes": [
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5726 //服务端口
// } //可以多个,自行负载均衡
// ],
// "UpstreamPathTemplate": "/T5726/{url}", //网关地址--url变量 //冲突的还可以加权重Priority
// "UpstreamHttpMethod": [ "Get", "Post" ]
// }
// ]
//}
//*****************************多地址多实例********************************
//{
// "ReRoutes": [
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5726 //服务端口
// } //可以多个,自行负载均衡
// ],
// "UpstreamPathTemplate": "/T5726/{url}", //网关地址--url变量 //冲突的还可以加权重Priority
// "UpstreamHttpMethod": [ "Get", "Post" ]
// },
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5727 //服务端口
// }
// ],
// "UpstreamPathTemplate": "/T5727/{url}", //网关地址--url变量
// "UpstreamHttpMethod": [ "Get", "Post" ]
// },
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5728 //服务端口
// }
// ],
// "UpstreamPathTemplate": "/T5728/{url}", //网关地址--url变量
// "UpstreamHttpMethod": [ "Get", "Post" ]
// }
// ]
//}
*****************************单地址多实例负载均衡********************************
//{
// "ReRoutes": [
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "DownstreamHostAndPorts": [
// {
// "Host": "localhost",
// "Port": 5726 //服务端口
// } //可以多个,自行负载均衡
// ,
// {
// "Host": "localhost",
// "Port": 5727 //服务端口
// },
// {
// "Host": "localhost",
// "Port": 5728 //服务端口
// }
// ],
// "UpstreamPathTemplate": "/T5/{url}", //网关地址--url变量 //冲突的还可以加权重Priority
// "UpstreamHttpMethod": [ "Get", "Post" ],
// "LoadBalancerOptions": {
// "Type": "RoundRobin" //轮询 LeastConnection-最少连接数的服务器 NoLoadBalance不负载均衡
// }
// }
// ]
//}
*****************************单地址多实例负载均衡+Consul********************************
//{
// "ReRoutes": [
// {
// "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
// "DownstreamScheme": "http",
// "UpstreamPathTemplate": "/TConsul/{url}", //网关地址--url变量
// "UpstreamHttpMethod": [ "Get", "Post" ],
// "ServiceName": "{{consul服务名称}}", //consul服务名称
// "LoadBalancerOptions": {
// "Type": "RoundRobin" //轮询 LeastConnection-最少连接数的服务器 NoLoadBalance不负载均衡
// },
// "UseServiceDiscovery": true
// }
// ],
// "GlobalConfiguration": {
// "BaseUrl": "http://127.0.0.1:6299", //网关对外地址
// "ServiceDiscoveryProvider": {
// "Host": "localhost",
// "Port": 8500,
// "Type": "Consul" //由Consul提供服务发现
// }
// }
//}
//*****************************单地址多实例负载均衡+Consul+Polly********************************
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/consul/{url}", //网关地址--url变量
"UpstreamHttpMethod": [ "Get", "Post" ],
"ServiceName": "{{consul服务名称}}", //consul服务名称
"LoadBalancerOptions": {
"Type": "RoundRobin" //轮询 LeastConnection-最少连接数的服务器 NoLoadBalance不负载均衡
},
"UseServiceDiscovery": true,
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3, //允许多少个异常请求
"DurationOfBreak": 10000, // 熔断的时间,单位为ms
"TimeoutValue": 10000 //如果下游请求的处理时间超过多少则自如将请求设置为超时 默认90秒
}
//"RateLimitOptions": {
// "ClientWhitelist": [], //白名单
// "EnableRateLimiting": true,
// "Period": "5m", //1s, 5m, 1h, 1d jeffzhang
// "PeriodTimespan": 5, //多少秒之后客户端可以重试
// "Limit": 5 //统计时间段内允许的最大请求数量
//},
//"FileCacheOptions": {
// "TtlSeconds": 10
//} //"缓存"
}
],
"GlobalConfiguration": {
"BaseUrl": "http://127.0.0.1:6299", //网关对外地址
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500,
"Type": "Consul" //由Consul提供服务发现
},
//"RateLimitOptions": {
// "QuotaExceededMessage": "Too many requests, maybe later? 11", // 当请求过载被截断时返回的消息
// "HttpStatusCode": 666 // 当请求过载被截断时返回的http status
//}
}
}
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/108150082
- 点赞
- 收藏
- 关注作者
评论(0)