1、说明
当一些配置需要修改在进行获取时,通常做法是修改完配置文件后再重启web服务器或者docker进行完成,下面我介绍一种热更新方法,修改完配置文件后,不需要重启服务器即可获取最新的配置文件,让服务感知配置的变化。
2、实践
下面我通过二种方式来讲解一下.Net Core实现选择数据热更新,让服务感知配置的变化。
2.1 通过AddSingleton单例方式注入,然后使用 IOptionsMonitor实现数据热更新
2.1.1 首先在Startup.cs文件中的ConfigureServices方法添加配置
//通过读取配置文件加载到SystemPath类中
services.Configure<SystemPath>(Configuration.GetSection("SystemPath"));
//添加服务注入
services.AddSingleton<IPathService, PathService>();
public class SystemPath
{
public string FilePath { get; set; }
}
2.1.2 在PathService构造器中注入IOptionsMonitor<SystemPath>实现数据热更新
public class PathService : IPathService
{
IOptionsMonitor<SystemPath> _options;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="blogData"></param>
public PathService(IOptionsMonitor<SystemPath> options)
{
_options = options;
}
public string GetPath()
{
return _options.CurrentValue.FilePath;
}
}
2.1.3 在PathController中通过调用接口方式读取最新配置路径
/// <summary>
/// 路径
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class PathController : ControllerBase
{
private readonly IPathService _pathService;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="pathService"></param>
public PathController(IPathService pathService)
{
_pathService = pathService;
}
/// <summary>
/// 获取系统路径
/// </summary>
/// <returns></returns>
[HttpGet]
public MethodResult GetSystemPath()
{
return new MethodResult(_pathService.GetPath());
}
}
运行看一下效果:
现在读取到的路径是D:/File/2.jpg,我们修改一下配置文件然后重新调用接口看一下,这时会更新最新的路径。
2.2 通过AddScoped 方式注入,然后使用 IOptionsSnapshot 实现数据热更新
2.2.1 首先在Startup.cs文件中的ConfigureServices方法添加配置
//通过读取配置文件加载到SystemPath类中
services.Configure<SystemPath>(Configuration.GetSection("SystemPath"));
//添加服务注入
services.AddScoped<IPathService, PathService>();
public class SystemPath
{
public string FilePath { get; set; }
}
2.2.2 在PathService构造器中注入IOptionsMonitor<SystemPath>实现数据热更新
public class PathService : IPathService
{
IOptionsSnapshot<SystemPath> _options;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="blogData"></param>
public PathService(IOptionsSnapshot<SystemPath> options)
{
_options = options;
}
public string GetPath()
{
return _options.Value.FilePath;
}
}
2.2.3 在PathController中通过调用接口方式读取最新配置路径
/// <summary>
/// 路径
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class PathController : ControllerBase
{
private readonly IPathService _pathService;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="pathService"></param>
public PathController(IPathService pathService)
{
_pathService = pathService;
}
/// <summary>
/// 获取系统路径
/// </summary>
/// <returns></returns>
[HttpGet]
public MethodResult GetSystemPath()
{
return new MethodResult(_pathService.GetPath());
}
}
运行看一下效果:
现在读取到的路径是D:/File/2.jpg,我们修改一下配置文件然后重新调用接口看一下,这时会更新最新的路径。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。



