写个重新加载 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. PAT1106:Lowest Price in Supply Chain

    1106. Lowest Price in Supply Chain (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CH ...

  2. popup_layer插件示例

    导入popup_layer.js插件 设置好显示的div: <div class="main" id="showImg" style="disp ...

  3. String的indexOf()用于获取字符串中某个子字符串的位置

    indexOf作用:用于检索一个字符串在另一个字符串中的位置. indexOf的几个重载方法如下: int indexOf(String str)  意思为在字符串中检索str第一次出现的位置,如果找 ...

  4. SpringMVC, Spring和Mybatis整合案例一

    一  准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二  整合思路 Dao层: 1.SqlMapConfig. ...

  5. Linux内存使用情况以及内存泄露分析之工具与方法

    <Linux C/C++ Memory Leak Detection Tool> 1. 内存使用情况分析 1.1 系统总内存分析 通过cat /proc/meminfo,可用的物理内存=M ...

  6. HTTPS 之 TLS 性能调优

    HTTPS(HTTP over SSL)是以安全为目标的 HTTP 通道,可以理解为 HTTP + SSL/TLS,即在 HTTP 下加入 SSL/TLS 层作为安全基础.其中 TLS 的前身是 SS ...

  7. Git协作流程

    Git 作为一个源码管理系统,不可避免涉及到多人协作. 协作必须有一个规范的流程,让大家有效地合作,使得项目井井有条地发展下去."协作流程"在英语里,叫做"workflo ...

  8. java之集合Collection详解之3

    package cn.itcast_03; public class Student { // 成员变量 private String name; private int age; // 构造方法 p ...

  9. Python replace()方法

    描述 Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次. 语法 replace()方法语法: st ...

  10. Python json & pickle, shelve 模块

    json 用于字符串和python的数据类型间的转换 四个功能 dumps dump loads load pickle 用于python特有的类型和python的数据类型进行转换 四个功能 dump ...