写个重新加载 ocelot 配置的接口

Intro

我们想把 ocelot 的配置放在自己的存储中,放在 Redis 或者数据库中,当修改了 Ocelot 的配置之后希望即时生效,又不想在网关这边定时刷新 ocelot 配置,ocelot 配置没变化的时候,定时刷新配置是一种无意义的资源浪费,ocelot 自带的有一个 Administration ,感觉对于我来说,有点太重了,不想去集成这个东西,于是就想自己实现一个重新加载配置的接口。

实现代码

在集成 Ocelot 网关的项目的 Startup 里的 Configure 方法中添加如下代码:

#region 更新 Ocelot 配置接口

            // PUT /ocelot/admin/configuration 需要 Admin 的角色
app.Map(new PathString("/ocelot/admin/configuration"), appBuilder =>
{
appBuilder.Use(async (context, next) =>
{
if (context.Request.Method.ToUpper() != "PUT")
{
context.Response.StatusCode = 404;
return;
}
var authenticateResult = await context.AuthenticateAsync(AuthenticationProviderKey);
if (!authenticateResult.Succeeded)
{
context.Response.StatusCode = 401;
return;
}
if (authenticateResult.Principal.IsInRole("Admin"))
{
var configurationRepo =
context.RequestServices.GetRequiredService<IFileConfigurationRepository>();
var ocelotConfiguration = await configurationRepo.Get();
var logger = context.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger("OcelotConfiguration");
if (!ocelotConfiguration.IsError)
{
var internalConfigurationRepo = context.RequestServices.GetRequiredService<IInternalConfigurationRepository>();
var internalConfigurationCreator =
context.RequestServices.GetRequiredService<IInternalConfigurationCreator>();
var internalConfiguration = await internalConfigurationCreator.Create(ocelotConfiguration.Data);
if (!internalConfiguration.IsError)
{
internalConfigurationRepo.AddOrReplace(internalConfiguration.Data);
context.Response.StatusCode = 200;
return;
}
else
{
logger.LogError($"update ocelot configuration error, error in create ocelot internal configuration, error messages:{string.Join(", ", ocelotConfiguration.Errors)}");
}
}
else
{
logger.LogError($"update ocelot configuration error, error in get ocelot configuration from configurationRepo, error messages:{string.Join(", ", ocelotConfiguration.Errors)}");
}
context.Response.StatusCode = 500;
}
else
{
context.Response.StatusCode = 403;
}
});
}); #endregion 更新 Ocelot 配置接口

这里的代码包含了一些逻辑,检查了要操作的用户是否拥有 Admin 的角色,可以自己根据自己的需要自行修改进行定制,可以自定义要操作的角色,自定义要操作的接口地址以及请求方式。

AuthenticationProviderKey 是在 ConfigureServices 方法中定义的认证方式,示例代码如下:

        public IConfiguration Configuration { get; }

        private readonly string AuthenticationProviderKey = "IdentityApiKey";

        // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//…
services.AddAuthentication()
.AddIdentityServerAuthentication(AuthenticationProviderKey, x =>
{
x.Authority = Configuration["Authorization:Authority"];
x.ClaimsIssuer = Configuration["Authorization:ClaimsIssuer"];
x.RequireHttpsMetadata = false;
});
services.AddOcelot();
// ......
}

调用 API 接口更新配置

可以使用 Postman 或者 fiddler 等调用 API 来测试

返回 200 即配置更新成功

Memo

Enjoy it~

写个重新加载 ocelot 配置的接口的更多相关文章

  1. asp.net core重新加载应用配置

    asp.net core重新加载应用配置 Intro 我把配置放在了数据库或者是Redis里,配置需要修改的时候我要直接修改数据库,然后调用一个接口去重新加载应用配置,于是就尝试写一个运行时重新加载配 ...

  2. 利用ChromeOptions()加载用户配置

    一. 如何绕过页面登录 我们在登录网站的时候,通常需要输入用户名.密码和验证码,那么有没有办法绕过登录环节呢? 有两种方法可以解决这个问题,一种是利用chrome浏览器的用户配置,一种是利用cooki ...

  3. atitit.动态加载数据库配置in orm hibernate mybatis

    atitit.动态加载数据库配置in orm 1. 动态加载数据库配置的优点::: 1 1.1. 组合多个配置文件... 1 1.2. 连接多个数据库 1 2. 基本的流程:::getCfg内存对象, ...

  4. 自动化测试-14.selenium加载FireFox配置

    前言 有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile ...

  5. 2.14 加载Firefox配置

    2.14 加载Firefox配置(略,已在2.1.8讲过,请查阅2.1.8节课) 回到顶部 2.14-1 加载Chrome配置 一.加载Chrome配置chrome加载配置方法,只需改下面一个地方,u ...

  6. 加载 Firefox 配置

    有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用 firebug在打开的页面上继续定位页面元素,调试起来不方便 .加载浏览器配置,需要用 FirefoxProfile(profile_d ...

  7. Selenium2学习(十四)-- 加载Firefox配置

    前言有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile_ ...

  8. JAVA加载Properties配置资源文件

    JAVA加载Properties配置资源文件 制作人:全心全意 配置文件(资源文件):以properties作为拓展名的文件 Java代码是如何加载properties文件的? 必须使用Propert ...

  9. Selenium3+python 加载Firefox配置

    有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile_di ...

随机推荐

  1. 一个基于原生JavaScript开发的、轻量的验证码生成插件

    Vcode.js 一个基于原生JavaScript开发的.轻量的验证码生成插件 V: 1.0.0 DEMO:https://jofunliang.github.io/Vcode.js/example. ...

  2. 基于Spring的RPC通讯模型.

    一.概念和原理 RPC(remote procedure call),远程过程调用,是客户端应用和服务端之间的会话.在客户端,它所需要的一些功能并不在该应用的实现范围之内,所以应用要向提供这些功能的其 ...

  3. PAT1028:List Sorting

    1028. List Sorting (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Excel ca ...

  4. springboot之启动原理解析

    前言 SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起一步步揭开SpringBoot的神秘面 ...

  5. Redis的九大应用场景

    毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...

  6. SSM-SpringMVC-13:SpringMVC中XmlViewResolver视图解析器

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 还记得上篇博客提出来的问题吗? BeanNameViewResolver视图解析器每使用一道视图,就得手工配 ...

  7. sql server 高可用日志传送

    一. 日志传送概述 SQL Server使用日志传送,可以自动将主服务器的事务日志备份发送到一个或多个辅助数据库上.可选的监视服务器,记录备份和还原操作的历史记录及状态. 优点 提供灾难恢复解决方案 ...

  8. 在线OJ的小demo

    牛课网OJ规则 用readLine()代替read_line() 用readLine()代替read_line() 用readLine()代替read_line() 用readLine()代替read ...

  9. Redis Rpop 命令

    Redis Rpop 命令用于移除并返回列表的最后一个元素. 语法 redis Rpop 命令基本语法如下: redis 127.0.0.1:6379> RPOP KEY_NAME 可用版本 & ...

  10. Selenium 3 没办法启用指定的Firefox Profile

    系统总会把profile复制到一个temp文件夹里,但是相关信息并不复制回去, 导致在测试注册登录功能时, 必须写在同一个脚本里:如果分成两段脚本, 登录会失败. public static void ...