使用.net core操作Apollo

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;
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章