管理 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
*
*
*