.Netcore 2.0 Ocelot Api网关教程(番外篇)- Ocelot v13.x升级
由于Ocelot系列博客好久沒更新(差不多有10個月的時間了),在此先說聲抱歉,Ocelot系列會繼續更新下去。
在寫上一篇配置管理的時候發現官方文檔已經和以前的不一樣,而Ocelot也從5.0版本更新到了13.x版本,進行了很多的修改與feature新增。
本篇文章就來介紹一下從5.0版本升級到13.0版本需要注意的事項。
1、Ocelot的兩次重大更新
在Ocelot的release頁面可以看到在6.0和11.0版本分別進行了一次斷層更新,具體來看一下有哪些變化。
Ocelot 6.0 release:
本次更新修改了負載均衡配置,同時添加了一個新的基于cookie的負載類型。
Ocelot 11.0 release
本次更新修改了服務發現的添加方式,需要手動引用?
Ocelot.Provider.Consul
?包(如果使用Consul作為負載均衡器),或者?
Ocelot.Provider.Eureka
?包(如果使用Eureka作為負載均衡器),同時在路由配置中不再需要?
UseServiceDiscovery
?配置,只需要一個?
ServiceName
?配置即可。
2、開工,改代碼
1、首先升級Ocelot版本之13.x
選擇Ocelot包,然后選擇想要升級的版本(此處為13.0)點擊升級即可。
?2、引入
Ocelot.Administration
?包(如果有用到配置管理)。
選擇包,點擊引入既可。
?3、引入?
Ocelot.Provider.Consul
?包
選擇包,點擊引入即可。
?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、修改試用服務發現的配置如下
{"DownstreamPathTemplate": "/api/Counter/Count",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/count",
"UpstreamHttpMethod": [ "Get" ],
"ServiceName": "Count",
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
}
其中Type為要使用的負載均衡類型。
最后放一張此次升級的git change log截圖
源碼參見:
https://github.com/Weidaicheng/OcelotTutorial/tree/ocelotV13upgrade?
原文地址:https://www.jianshu.com/p/154ad3e244b2
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
以上是生活随笔為你收集整理的.Netcore 2.0 Ocelot Api网关教程(番外篇)- Ocelot v13.x升级的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .net Core2.2 WebApi通
- 下一篇: ASP.NET Core 沉思录 - 环