Apollo提供了一套的Http REST接口,使第三方应用能够自己管理配置。
apollo第三方接入平台文档说明:apollo第三方接入平台文档说明
第一步:需要下载和引用Com.Ctrip.Framework.Apollo.OpenApi.dll组件,可以下载apollo项目生成组件,地址:apollo项目
第二步:用管理员账号登录apollo,点击管理员工具—开放平台授权管理
申请token
第三步:配置程序配置文件,在appsettings.Development.json中增加apollo配置
修改Program.cs文件
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, builder) =>
{
builder
.AddApollo(builder.Build().GetSection("apollo"))
.AddNamespace("application")
.AddDefault(ConfigFileFormat.Xml)
.AddDefault(ConfigFileFormat.Json)
.AddDefault();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
第四步:开始编程
public static INamespaceClient CreateNamespaceClient() => Factory.CreateNamespaceClient(Current.appid, Env);//第一个参数是要操作的appid,第二个参数是要操作的环境
private static IOpenApiFactory Factory { get; } = new OpenApiFactory(new OpenApiOptions
{
PortalUrl = new Uri(Current.Url),//apollo地址
Token = Current.Token//刚申请的token值
});
//查询
public async Task<string> GetData()
{
string key = Current.Key;//要操作的key值
var client = CreateNamespaceClient();
var v = await client.GetItem(key).ConfigureAwait(false);
return v.Value ;
}
//修改
public async Task<bool> UpdateData()
{
string key = Current.Key;//要操作的key值
var client = CreateNamespaceClient();
var v = await client.GetItem(key).ConfigureAwait(false);
v.Value ="12121212";
await client.UpdateItem(v).ConfigureAwait(false);
return true;
}
//删除
public async Task<bool> DelData()
{
string key = Current.Key;//要操作的key值
var client = CreateNamespaceClient();
var v = await client.RemoveItem(key).ConfigureAwait(false);
return v;
}
//创建
public async Task<bool> CreateData()
{
var client = CreateNamespaceClient();
var item = await client.CreateItem(new Item
{
Key = key,
Value = value1,
DataChangeCreatedBy = "apollo"
}).ConfigureAwait(false);
return v;
}
//发布
public async Task<bool> PublishData()
{
string key = Current.Key;//要操作的key值
var client = CreateNamespaceClient();
var result = await client.GetNamespaceInfo().ConfigureAwait(false);
var release = new NamespaceRelease
{
ReleasedBy = result.DataChangeCreatedBy,
ReleaseComment = "发布",
ReleaseTitle = $"{DateTime.Now:yyyyMMddHHmmss}-release"
};
await client.Publish(release).ConfigureAwait(false);
return true;
}