本文介绍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

 
token from internal id server.png

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

2、配置管理

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

 
get configuration.png

使用上次获取的token。

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

 
GetUserInfo.png

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

然后修改配置如下

 
change configuration.png

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

 
GetUserInfo 404.png

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

 
GetUserInfochanged.png

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

 
configuration.Development.json.png

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

完,下一篇介绍限流

作者:Weidaicheng
链接:https://www.jianshu.com/p/9e2fa5783211
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

.Netcore 2.0 Ocelot Api网关教程(6)- 配置管理的更多相关文章

  1. .Netcore 2.0 Ocelot Api网关教程(2)- 路由

    .Netcore 2.0 Ocelot Api网关教程(1) 路由介绍 上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes ...

  2. .Netcore 2.0 Ocelot Api网关教程(7)- 限流

    本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对  ...

  3. .Netcore 2.0 Ocelot Api网关教程(5)- 认证和授权

    本文介绍Ocelot中的认证和授权(通过IdentityServer4),本文只使用最简单的IdentityServer,不会对IdentityServer4进行过多讲解. 1.Identity Se ...

  4. .Netcore 2.0 Ocelot Api网关教程(1)- 入门

    Ocelot(Github)Ocelot官方文档(英文)本文不会介绍Api网关是什么以及Ocelot能干什么需要对Api网关及Ocelot有一定的理论了解 开始使用Ocelot搭建一个入门级Api网关 ...

  5. .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation

    本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...

  6. .Netcore 2.0 Ocelot Api网关教程(9)- QoS

    本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly  ...

  7. .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现

    本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...

  8. .Netcore 2.0 Ocelot Api网关教程(8)- 缓存

    Ocelot中使用 CacheManager 来支持缓存,官方文档中强烈建议使用该包作为缓存工具.以下介绍通过使用CacheManager来实现Ocelot缓存. 1.通过Nuget添加 Ocelot ...

  9. .Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合

    在实际的应用当中,经常会遇到同一个操作要请求多个api来执行.这里先假设一个应用场景:通过姓名获取一个人的个人信息(性别.年龄),而获取每种个人信息都要调用不同的api,难道要依次调用吗?在Ocelo ...

随机推荐

  1. Selenium(十一)使用EXCEL读取用户数据和生成测试报表

    1.下载两个模块 2.xlrd的使用方法 3.使用excel获取数据 在userdata文件中增加代码: login.py: 4.使用excel生成测试报表 举例: 在log_module中定义函数: ...

  2. Oracle自动化安装脚本-part01-亲试ok

      #!/bin/bash   node_num=$1 base_config=./network.conf   网络配置文件 software_config=./software.conf  软件包 ...

  3. android-studio打包APK出现有关apk图标问题

    报的错很多,有build gradle中的两个大红感叹号,由此引发了一大堆问题 注意到最后出现红色打包错误的代码: Failed to read PNG signature: file does no ...

  4. PHP mysqli_connect_errno() 函数

    返回上一次连接错误的错误代码: <?php $con=mysqli_connect("localhost","wrong_user","my_p ...

  5. Lavevel 中 trait 如何继承与复写

    1 写一个基类 2 基类中 use YourTrait 3 写一个子类 extends 基类 4 子类中覆写 YourTrait 中的同名方法 $query = parent::scopeOfPara ...

  6. 011_9*9 乘法表(编写 shell 脚本,打印 9*9 乘法表)

    #!/bin/bashfor i in `seq 9`do    for j in `seq $i`        do           echo -n "$i*$j=$[i*j] &q ...

  7. number-progression-network

    T1给定一个 $n$ 位的数字串,要求修改若干位,使得至少包含 $k$ 个相同的数位,最小化代价. Sol 考虑枚举那种数字作为答案,选代价前 $k$ 小的修改成目标数字. 有一部分的数字是必须修改的 ...

  8. 串结构练习——字符串连接(SDUT 2124)

    Problem Description 给定两个字符串string1和string2,将字符串string2连接在string1的后面,并将连接后的字符串输出. 连接后字符串长度不超过110. Inp ...

  9. java.security.InvalidKeyException: Illegal key size or default parameters

    今天在使用idea打包maven项目时,出现这个错误:java.security.InvalidKeyException: Illegal key size or default parameters ...

  10. IntelliJ IDEA 设置忽略SVN文件和文件夹

       IntelliJ IDEA 在提交文件至SVN时,可以设置忽略某些文件和文件夹,以免误提交不需要提交的文件.最后,插个题外话,介绍一下如何设置代码默认折叠或者展开.下面使用IntelliJ ID ...