.Netcore 2.0 Ocelot Api网关教程(6)- 配置管理

本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置。由于该功能权限很高,所以需要授权才能进行相关操作。有两种方式来认证,外部Identity Server或内部Identity Server。

1、外部Identity Server

修改 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())
.AddAdministration("/administration", options);

services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication("TestKey", options);
}

其中复用了Identity Server的配置。

2、内部Identity Server

修改 Startup 中的 ConfigureServices 方法如下:

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

services.AddOcelot(new ConfigurationBuilder()
.AddJsonFile("configuration.json")
.Build())
.AddAdministration("/administration", "secret");

services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication("TestKey", options =>
{
options.Authority = "http://localhost:6000";
options.RequireHttpsMetadata = false;
options.ApiName = "api1";
});
}

其中为secret值为"secret",后边会用得到。

其上为添加配置管理的两种方式,本例中以内部Identity Server为例。

Administration一共提供了3组Api

  • Token获取

  • 配置管理

  • 缓存管理
    其中Token获取Api只在使用内部Identity Server时有效。由于缓存的教程还没更新,所以缓存管理的Api在后边的文章介绍。

1、Token获取

使用Postman请求http://localhost:5000/administration/connect/token如下所示,可以获得一个token

640?wx_fmt=other

token from internal id server.png


注意Body的数据类型要选择 form-data,并且 client_secret 要填写代码中配置的secret,当前教程为secret。


2、配置管理

使用Postman请求http://localhost:5000/administration/configuration如下所示,获取配置

640?wx_fmt=other

get configuration.png


使用上次获取的token。


以http://localhost:5000/GetUserInfo?name=Jonathan为例请求数据如下

640?wx_fmt=other

GetUserInfo.png


可以成功请求并且获取数据。


然后修改配置如下

640?wx_fmt=other

change configuration.png


注意此次请求为Post请求,并且不要忘记添加认证头token,此次请求的body参数为之前获取的配置并且修改了/GetUserInfo链接为/GetUserInfochanged。
再次使用Postman请求http://localhost:5000/GetUserInfo?name=Jonathan如下

640?wx_fmt=other

GetUserInfo 404.png


得到了404,修改链接为http://localhost:5000/GetUserInfochanged?name=Jonathan再次请求如下

640?wx_fmt=other

GetUserInfochanged.png


此次配置修改成功,打开到路径/OcelotTutorial/OcelotGetway/bin/Debug/netcoreapp2.0下有一个 configuration.Development.json 文件打开查看如下

640?wx_fmt=other

configuration.Development.json.png


配置文件也已经修改。
可能在开发时会遇到修改完配置之后,下次调试时配置又回到了原来,是因为 configuration.json 选择成了总是复制,所以每次开始调试的时候都会替换 configuration.development.json 中的内容。
如果Ocelot Api网关程序没有读写文件的权限也会遇到修改配置失败的情况。

 

原文地址:https://www.jianshu.com/p/9e2fa5783211

 
 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值