.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

注意Body的数据类型要选择 form-data,并且 client_secret 要填写代码中配置的secret,当前教程为secret。
2、配置管理
使用Postman请求http://localhost:5000/administration/configuration如下所示,获取配置

使用上次获取的token。
以http://localhost:5000/GetUserInfo?name=Jonathan为例请求数据如下

可以成功请求并且获取数据。
然后修改配置如下

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

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

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

配置文件也已经修改。
可能在开发时会遇到修改完配置之后,下次调试时配置又回到了原来,是因为 configuration.json 选择成了总是复制,所以每次开始调试的时候都会替换 configuration.development.json 中的内容。
如果Ocelot Api网关程序没有读写文件的权限也会遇到修改配置失败的情况。
源码下载
完,下一篇介绍限流
作者:Weidaicheng
链接:https://www.jianshu.com/p/9e2fa5783211
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
.Netcore 2.0 Ocelot Api网关教程(6)- 配置管理的更多相关文章
- .Netcore 2.0 Ocelot Api网关教程(2)- 路由
.Netcore 2.0 Ocelot Api网关教程(1) 路由介绍 上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes ...
- .Netcore 2.0 Ocelot Api网关教程(7)- 限流
本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对 ...
- .Netcore 2.0 Ocelot Api网关教程(5)- 认证和授权
本文介绍Ocelot中的认证和授权(通过IdentityServer4),本文只使用最简单的IdentityServer,不会对IdentityServer4进行过多讲解. 1.Identity Se ...
- .Netcore 2.0 Ocelot Api网关教程(1)- 入门
Ocelot(Github)Ocelot官方文档(英文)本文不会介绍Api网关是什么以及Ocelot能干什么需要对Api网关及Ocelot有一定的理论了解 开始使用Ocelot搭建一个入门级Api网关 ...
- .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation
本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...
- .Netcore 2.0 Ocelot Api网关教程(9)- QoS
本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly ...
- .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现
本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...
- .Netcore 2.0 Ocelot Api网关教程(8)- 缓存
Ocelot中使用 CacheManager 来支持缓存,官方文档中强烈建议使用该包作为缓存工具.以下介绍通过使用CacheManager来实现Ocelot缓存. 1.通过Nuget添加 Ocelot ...
- .Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合
在实际的应用当中,经常会遇到同一个操作要请求多个api来执行.这里先假设一个应用场景:通过姓名获取一个人的个人信息(性别.年龄),而获取每种个人信息都要调用不同的api,难道要依次调用吗?在Ocelo ...
随机推荐
- ubunu安装qq、微信等、
参考: https://www.lulinux.com/archives/1319 安装下面下载deepin-wine-for-ubuntu,然后进去安装 https://github.com/wsz ...
- 配置IIS使其支持APK文件的下载
在管理工具里打开Internet 信息服务(IIS)管理器.然后选择需要配置的网站. 右侧的界面中会显示该网站的所有功能配置,我们选择并点击进入“MIME类型” 在左侧的操作区选择点击“添加”MI ...
- P4160 [SCOI2009]生日快乐 搜索
思路:无脑搜索 提交:1次 题解: 大力搜索,枚举每个状态\((x,y,l)\)(\(x\)指分配到的长(可能比\(y\)要短),\(y\)指分配到的宽(可能比\(x\)要长),\(l\)指剩余切的次 ...
- 022_STM32中断优先级分组解析
(0)STM32有十六个优先级 (一)STM32分组为:组0-4 (二)分组配置在寄存器SCB->AIRCR中: (三)解析第二点 1. 组0就是4位都用来设置成响应优先级,2^4=16位都是响 ...
- 五十.KVM简介、 Virsh管理 、 自定义虚拟机、虚拟设备管理
1.virsh基本管理操作 列出当前正在运行的虚拟机 查看虚拟机的信息 管理虚拟机 设置虚拟机开机自动运行 1.1 virsh基本操作 查看kvm节点(服务器)信息 ] virsh nodei ...
- DbVisualizer用JDBC连接SQL Server
1.安装驱动,解压后得到jar文件 Drivers 4.1 and 4.0 for SQL Serve http://www.microsoft.com/en-us/download/confirma ...
- 內嵌html字符串顯示
前端:System.Web.HttpUtility.HtmlEncode() @Html.Raw(htmlStr) 後端:System.Net.WebUtility.HtmlDe ...
- leetcode解题报告(8):Remove Element
描述 Given an array and a value, remove all instances of that value in place and return the new length ...
- Codeforces 1220 E Tourism
题面 可以发现一个边双必然是可以随意走的,所以我们就把原图求割边然后把边双缩成一个点,然后就是一个树上dp了. #include<bits/stdc++.h> #define ll lon ...
- windows游戏编程 绘图基础
本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/22451353 作者:jadeshu 邮箱: jades ...