Net6集成ServiceStack 使用Apollo

1、在appsettings.json中配置apollo连接

{
  "apollo": {
    "AppId": "Test",
    "MetaServer": "https://apollo..com"//配置中心地址
  }
}

 

2、引用包Com.Ctrip.Framework.Apollo 在Program中初始化Configuation

builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
    config.AddApollo(config.Build().GetSection("apollo"));
});

 

3、将生成的ConfigurationManage注入到ServiceStack Appsetting中

builder.Services.AddSingleton<ServiceStack.Configuration.IAppSettings>(new NetCoreAppSettings(builder.Configuration));

 

4、启动ServiceStack

app.UseServiceStack(new AppHost("APITemplate", typeof(HealthCheckService).Assembly) { AppSettings = new NetCoreAppSettings(builder.Configuration) });

 

5、使用,在Apphost中直接调用

var licenseKeyText = this.AppSettings.Get<string>("ServiceStackLicense");

在服务层,直接利用构造函数注入

public class ApolloApplicationService : IApolloApplicationService
    {
        public readonly IAppSettings _appSettings;

        public ApolloApplicationService(IAppSettings appSettings)
        {
            _appSettings = appSettings;
        }

        public string GetValue(string key)
        {
            return _appSettings.Get<string>(key);
        }
    }

 

posted on 2021-10-29 16:02  !!!!!!!!!!!!!!!  阅读(274)  评论(0)    收藏  举报

导航