写个重新加载 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. facenet 进行人脸识别测试

    1.简介:facenet 是基于 TensorFlow 的人脸识别开源库,有兴趣的同学可以扒扒源代码:https://github.com/davidsandberg/facenet 2.安装和配置 ...

  2. python识别图片文字

    因为学校要求要刷一门叫<包装世界>的网课,而课程里有200多道选择题,而且只能在手机完成,网页版无法做题,而看视频是不可能看视频的,这辈子都不可能看...所以写了几行代码来进行百度搜答案. ...

  3. 【转】MySQL datetime数据类型设置当前时间为默认值

    转自http://blog.csdn.net/u014694759/article/details/30295285 方法一: MySQL目前不支持列的Default 为函数的形式,如达到你某列的默认 ...

  4. Linux时间子系统之四:Timer在用户和内核空间流程

    用户空间应用中创建一个Timer(alarm/setitimer/POSIX Timer等等),然后程序继续执行: 内核进入创建/设置Timer系统调用,开始计时,在超时后通过何种方式通知用户空间: ...

  5. 【Web】一个非常简单的移动web消息框

    适用:h5+jquery,移动网页最佳 最近在写个简单的公众号页面,前端验证时有些信息要提示,很简单的需求实在不想找啥现成的轮子,又不至于用alert这么粗暴,遂写了个非常简单的消息框,效果如图: 特 ...

  6. Spring cloud整体框架

    研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  7. C#关于winform时间格式2017.05.27

    winform中的控件为DateTimePicker DateTime startdate = startTime.Value;//获取到Winform控件的时间 String start = sta ...

  8. 利用javaScript实现鼠标在文字上悬浮时弹出悬浮层

    在人人,CSDN等一些网站,当鼠标在某个东西上悬浮时,会弹出一个悬浮层,鼠标移开悬浮层消失.比如说CSDN的通知(应该是进入写新文章的页面后页面上方的那个铃铛),具体是什么实现的呢?上代码: < ...

  9. counter.go

    package engine func (engine *Engine) NumTokenIndexAdded() uint64 {     return engine.numTokenIndexAd ...

  10. compress.go

    The Gorilla Authors. All rights reserved. // Use of this source code is governed by a BSD-style // l ...