由于Ocelot系列博客好久沒更新(差不多有10個(gè)月的時(shí)間了),在此先說聲抱歉,Ocelot系列會(huì)繼續(xù)更新下去。
在寫上一篇配置管理的時(shí)候發(fā)現(xiàn)官方文檔已經(jīng)和以前的不一樣,而Ocelot也從5.0版本更新到了13.x版本,進(jìn)行了很多的修改與feature新增。
本篇文章就來介紹一下從5.0版本升級(jí)到13.0版本需要注意的事項(xiàng)。
1、Ocelot的兩次重大更新
在Ocelot的release頁面可以看到在6.0和11.0版本分別進(jìn)行了一次斷層更新,具體來看一下有哪些變化。
Ocelot 6.0.png
本次更新修改了負(fù)載均衡配置,同時(shí)添加了一個(gè)新的基于cookie的負(fù)載類型。
Ocelot 11.0.png
本次更新修改了服務(wù)發(fā)現(xiàn)的添加方式,需要手動(dòng)引用
Ocelot.Provider.Consul
包(如果使用Consul作為負(fù)載均衡器),或者 Ocelot.Provider.Eureka
包(如果使用Eureka作為負(fù)載均衡器),同時(shí)在路由配置中不再需要 UseServiceDiscovery
配置,只需要一個(gè) ServiceName
配置即可。
2、開工,改代碼
1、首先升級(jí)Ocelot版本之13.x
Ocelot package upgrade.png
選擇Ocelot包,然后選擇想要升級(jí)的版本(此處為13.0)點(diǎn)擊升級(jí)即可。
2、引入
Ocelot.Administration
包(如果有用到配置管理)。import Ocelot.Administration.png
選擇包,點(diǎn)擊引入既可。
3、引入
Ocelot.Provider.Consul
包import Ocelot.Provider.Consul.png
選擇包,點(diǎn)擊引入即可。
4、修改Startup中的ConfigureServices如下
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
void options(IdentityServerAuthenticationOptions o)
{
o.Authority = "http://localhost:6000";
o.RequireHttpsMetadata = false;
o.ApiName = "api1";
}
services
.AddOcelot(new ConfigurationBuilder()
.AddJsonFile("configuration.json")
.Build())
.AddConsul()
.AddAdministration("/administration", "secret");
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication("TestKey", options);
}
5、修改試用服務(wù)發(fā)現(xiàn)的配置如下
{
"DownstreamPathTemplate": "/api/Counter/Count",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/count",
"UpstreamHttpMethod": [ "Get" ],
"ServiceName": "Count",
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
}
其中Type為要使用的負(fù)載均衡類型。
最后放一張此次升級(jí)的git change log截圖
git change log.png
源碼下載