.NET 开源配置组件 AgileConfig 初体验

介绍

网上购物优惠券 m.cqfenfa.com

在微服务大行其道的今天,系统会被拆分成多个模块,作为单独的服务运行,同时为了集中化管理,我们还需要日志中心,配置中心等,很多开发人员可能更熟悉 ApolloConfig,这个组件功能也很完善,dotnet的客户端也主要是国人在维护和开发,之前公司就是选型的Apollo,而且也在生产环境稳定运行,但是服务端是Java的,而且部署服务端也有些麻烦。

今天主要介绍 AgileConfig,这是一个基于.net core开发的轻量级配置中心,之前跟作者也有过沟通,也很热情帮忙解决问题,它的特点有部署简单、配置简单、使用简单、学习简单等,同时也支持高可用(多节点)和docker部署,使用了FreeSql支持了多种多种数据库存储,Sqlserver, Mysql, Sqlite, PostgreSql,Oracle,FreeSql大法好!

架构图

部署服务端

首先,我们需要部署服务端,包含了控制台UI和节点服务,节点服务使用了长连接,配置变更后,会实时推送给客户端,部署推荐使用docker部署,因为项目是开源的,而且是.net core 编写的,还有一种方式是可以到github下载源代码,编译后发布运行。

配置信息是在数据库记录的,我们需要新建一个空的数据库,然后运行命令,程序会自动帮我们初始化表结构和数据。

docker run --name agile_config -e adminConsole=true -e db:provider=mysql -e db:conn="DataBase=configdb;Data Source=host.docker.internal;User Id=root;Password=123456;" -p 5000:5000 -d kklldog/agile_config:latest

参数介绍:

  • adminConsole 配置程序是否为管理控制台,如果为true则启用控制台功能,访问该实例会出现管理界面,多节点部署的话,只需要开启一个控制台即可。
  • db:provider 配置程序的数据库类型,可选 sqlserver,mysql,sqlite,npgsql,oracle。
  • db:conn 配置数据库连接串

访问 http://localhost:5000/, 第一次启动,程序会要求我们初始化管理员密码,设置完成以后,进入到首页,另外作者最近已经用 React 重构了UI,最近会更新,大家也可以尝鲜。

然后还需要配置节点,多节点保证了高可用,那如果所有节点都挂了的话,程序会读取本地的配置缓存,来保证正常的运行,这点大家不用担心, 我们需要在节点管理页面手动添加节点,这里我启动了一个节点,添加的地址为 http://192.168.100.103:5000 ,需要注意的是,这里在docker环境不能使用 localhost。

接下来需要配置应用,点击新增应用,填写应用名称,应用Id和应用密钥。

在客户端程序使用

这里使用了asp net core web项目示例,首先需要安装客户端组件,执行一下命令或者通过Nuget安装。

Install-Package AgileConfig.Client

然后修改appsetting.json 文件

{ 
  "AgileConfig": {
    "appId": "LogService",
    "secret": "123456",
    "nodes": "http://localhost:5000,http://localhost:5001"//多个节点使用逗号分隔
  }
}

然后修改 Program.cs

 public static IHostBuilder CreateHostBuilder(string[] args) =>
       Host.CreateDefaultBuilder(args)
       .ConfigureAppConfiguration((context, config) =>
        {
           var configClient = new ConfigClient();
           config.AddAgileConfig(configClient);
       })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

准备工作完成,接下来就要开始使用了,我们在配置项页面上面添加配置,然后选中点击上线。

然后可以在代码中使用注入的 IConfiguration 获取配置即可

 [Route("[controller]/[action]")]
    public class HomeController : ControllerBase
    {    
        private readonly IConfiguration _config;

        public HomeController(IConfiguration config)
        {
            _config = config; 
        }

        [HttpGet]
        public IActionResult Index()
        {
            var value = _config["AgileKey"];

            return Ok(new { value });
        }

然后启动程序,就可以尽情使用 AgileConfig 了,在页面上修改配置的话,我们的客户端配置也是实时修改的。

在配置页面上的配置都是字符串键值对的,那对于Json字符串应该怎么处理呢, 我们可以使用 Tuhu.Extensions.Configuration.ValueBinder.Json 扩展,通过Nuget安装即可,然后修改Startup.cs 文件

public void ConfigureServices(IServiceCollection services)
{ 
      services.ConfigureJsonValue<LogOptions>("", Configuration.GetSection("LogOptions"));  
}

LogOptions:

public class LogOptions : IOptions<LogOptions>
{
        public string Level { get; set; }

        public int Count { get; set; }

        public LogOptions Value => this;
}

这样我们就可以在代码中,使用注入的 IOptions<LogOptions> 来获取读取配置。

总结

AgileConfig 是使用 .net core 开发的配置组件,部署和使用起来都很简单,但是目前还有一些不足,比如多账号权限管理,和多环境支持,一般是开发,灰度和正式,不过没有关系,项目都是开源的,大家感兴趣的一起建设,完善不足的功能,现在.NET 社区是越来越好了,如果对大家有帮助的,可以支持一下!

https://github.com/kklldog/AgileConfig

Agile.net Code Protection - Powerful .NET Code Protection & Obfuscation Solution Agile.NET code protection solution offers better protection for your .NET code by providing a protection that goes beyond standard obfuscation methods. Our advanced code protection technology delivers a layered protection approach that combines our patented code virtualization technology, code encryption, obfuscation, and anti-reverse engineering countermeasures embedded into existing application code. Unbreakable Code Protection Agile.net uses a patented technology that transforms MSIL code into virtual opcodes that will only be understood by a secure virtual machine. The virtual machine processes the virtual opcodes directly, emulating the original code behavior without transforming the code back to its original form. Breaking the protection becomes NP-complete problem, thus making Agile.net the only obfuscator that guarantees your code can't be broken! Next Generation Obfuscator Obfuscates every aspect of your code, including class and method names, managed resources, user strings, methods implementation, system and library calls. Agile.net Protects more of your application than any other obfuscator because it knows what is safe to change and what needs to be left alone. Nevertheless, it gives you full control of the obfuscation process. Works with all .NET Technologies Join our rapidly growing customer base consisting of thousands of customers using Agile.net every day to protect their software running on WinRT, WP7, WP8, .NET 2.0 and up, Compact Framework 2.0 and up, ASP. NET, Silverlight 2.0 and up (XAML), WPF (BAML), XNA, ClickOnce and more
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值