.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:

640?wx_fmt=other

本次更新修改了负载均衡配置,同时添加了一个新的基于cookie的负载类型。

Ocelot 11.0 release

640?wx_fmt=other

本次更新修改了服务发现的添加方式,需要手动引用 

Ocelot.Provider.Consul

 包(如果使用Consul作为负载均衡器),或者 

Ocelot.Provider.Eureka

 包(如果使用Eureka作为负载均衡器),同时在路由配置中不再需要 

UseServiceDiscovery

 配置,只需要一个 

ServiceName

 配置即可。

2、开工,改代码

1、首先升级Ocelot版本之13.x

640?wx_fmt=other

选择Ocelot包,然后选择想要升级的版本(此处为13.0)点击升级即可。

 2、引入

Ocelot.Administration

 包(如果有用到配置管理)。

640?wx_fmt=other

选择包,点击引入既可。

 3、引入 

Ocelot.Provider.Consul

 包


640?wx_fmt=other

选择包,点击引入即可。

 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截图

640?wx_fmt=other

源码参见:

https://github.com/Weidaicheng/OcelotTutorial/tree/ocelotV13upgrade 

原文地址:https://www.jianshu.com/p/154ad3e244b2

.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com
640?wx_fmt=jpeg


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值