当前位置:坤哥网-kwan-.Net 使用 Apollo 测试使用记录

.Net 使用 Apollo 测试使用记录

2019/9/8 9:08:42 IT综合阅读(47) 评论(0)


前面的文章 《Apollo配置中心本地测试使用记录》 介绍了在本地搭建一个 Apollo 配置中心测试环境。

本文介绍在 .Net 中使用 Apollo。


下载 .Net 源码


下载源码:https://github.com/ctripcorp/apollo.net

如果只是在.net framework 上使用选择下载 Branch 为 master 的分支:

bf2b095a-1bba-412d-b44b-8d71f746725d.png

下载完成之后我们用 visual studio 打开解决方案,里面有2个项目:

d04e6e87-b025-4397-954e-32481694b218.png

Apollo 是源码,ApolloDemo 是一个 Demo,引用中的 Apollo 就是 Apollo 生成的 Framework.Apollo.Client.dll:

908d120e-5ff7-433e-bcc4-b29c424dbc3b.png


运行 Demo 程序


什么都不改,运行一下 ApolloDemo ,报错了:12c2b3f9-66db-4f54-8537-465e80a5932b.png

获取 Config 对象的时候报的,很明显报的是 C:\opt\settings\server.properties 这个文件不存在,在本机查看这个文件确实不存在。这个文件用来干嘛的呢?

9bcebf7d-8983-4eae-9ff1-53febaa158ca.png

查看源码找到在类 DefaultServerProvider 中配置了一个字符串常量:

e88401e5-f199-44a3-8dd0-5c1bdf687cc1.png

在初始化的时候使用到了:

a668ace9-25b0-4490-8c29-487fac1acb41.png

如果不需要使用c盘下这个文件,我们可以修改源代码。

我们直接新建这个文件。


程序产生了这个目录 C:\opt\data\100004458\config-cache,但是里面没有 json 文件。这个是本地缓存路径:

2fceb86e-d6a0-4ca7-b59a-c3e9688048df.png

配置文件配的就是 100004458 这个 app id:

e04b4939-59f4-4811-b144-46e7d7baaae7.png

在本地搭的 Apollo 配置中没有配置这个 AppID,官方给了个例子配的是 SampleApp:

72ac4073-ce85-4802-97a3-3cc565e514c5.png


我们把 AppID 改成 SampleApp,重新运行一下:

e2e33133-13f6-46f3-b8b2-5b511cde7c7a.png

F10继续下一步:e1f126ea-e5f9-4fa9-88fa-103173818e48.png

[INFO] App Id is set to [SampleApp] from System.Configuration.ConfigurationManager.AppSettings[AppID].

[INFO] Loading server properties from C:\opt\settings\server.properties
[INFO] EnvType is not available from ENV environment variable.
[INFO] EnvType is set to [DEV] by 'env' property from server.properties
[INFO] SubEnvType is not available from 'subenv' property of server.properties.
[INFO] Data Center is not available from 'idc' property of server.properties.
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/application?ip=192.168.2.196
[DEBUG] Loaded config for application: ApolloConfig{appId='SampleApp', cluster='default', namespaceName='application', configurations=System.Collections.Generic.Dictionary`2[System.String,System.String], releaseKey='20161009155425-d3a0749c6e20bc15'}
[DEBUG] Remote Config refreshed!
[DEBUG] Schedule periodic refresh with interval: 300000 ms
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[DEBUG] Long polling response: 304, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[WARN] Sync config failed, will retry. Repository Com.Ctrip.Framework.Apollo.Internals.RemoteConfigRepository, reason: Load Apollo Config failed - appId: SampleApp, cluster: dev, namespace: FX.apollo, url: http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196 [Cause: [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!]
[DEBUG] Schedule periodic refresh with interval: 300000 ms
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[WARN] Sync config from upstream repository Com.Ctrip.Framework.Apollo.Internals.RemoteConfigRepository failed, reason: Load Apollo Config failed - appId: SampleApp, cluster: dev, namespace: FX.apollo, url: http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196 [Cause: [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!]
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[WARN] Sync config from upstream repository Com.Ctrip.Framework.Apollo.Internals.RemoteConfigRepository failed, reason: Load Apollo Config failed - appId: SampleApp, cluster: dev, namespace: FX.apollo, url: http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196 [Cause: [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!]
[WARN] Loading config from local cache file C:\opt\data\SampleApp\config-cache\SampleApp+dev+FX.apollo.json failed [Cause: 未能找到文件“C:\opt\data\SampleApp\config-cache\SampleApp+dev+FX.apollo.json”。]
[WARN] Sync config failed, will retry. Repository Com.Ctrip.Framework.Apollo.Internals.LocalFileConfigRepository, reason: Load config from local config failed! [Cause: Loading config from local cache file C:\opt\data\SampleApp\config-cache\SampleApp+dev+FX.apollo.json failed [Cause: 未能找到文件“C:\opt\data\SampleApp\config-cache\SampleApp+dev+FX.apollo.json”。]]
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[WARN] Sync config from upstream repository Com.Ctrip.Framework.Apollo.Internals.RemoteConfigRepository failed, reason: Load Apollo Config failed - appId: SampleApp, cluster: dev, namespace: FX.apollo, url: http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196 [Cause: [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!]
[WARN] Loading config from local cache file C:\opt\data\SampleApp\config-cache\SampleApp+dev+FX.apollo.json failed [Cause: 未能找到文件“C:\opt\data\SampleApp\config-cache\SampleApp+dev+FX.apollo.json”。]
[WARN] Init Apollo Local Config failed - namespace: FX.apollo, reason: Load config from local config failed! [Cause: Loading config from local cache file C:\opt\data\SampleApp\config-cache\SampleApp+dev+FX.apollo.json failed [Cause: 未能找到文件“C:\opt\data\SampleApp\config-cache\SampleApp+dev+FX.apollo.json”。]].

查看 data 目录,这个用来本地缓存的 json 文件产生了:

72db07ff-5eb3-44bc-94a7-5dbc52911a3d.png

程序继续往后走:

2883d8a0-e918-491e-8a08-e47c5e2243f4.png

打印出了环境信息,继续:

062b7a09-7400-460b-91d6-3dc8b6e4673a.png

如上已经打印出了配置 timeout 配置的值。


添加配置


在配置管理界面中添加一个key测试一把:

d5fbed28-c628-4f1a-bf83-e250bbbd952e.png

2bb3743e-9a4e-4ac1-8da4-ba7bb296b553.png

默认是没有发布的,点击发布:

e1802055-7f1c-43b0-baff-8cbc1d74a05b.png

d4525207-8a07-4017-be9d-5a8a7071b570.png

现在已经是发布状态了,程序已经监听到了:64460201-4f9d-43a7-9339-4cfe3ba27b72.png

AppId: SampleApp, Env: DEV, DC: , IP: 192.168.2.196
Apollo Config Demo. Please input key to get the value. Input quit to exit.
> timeout
Loading key: timeout with value: 100
> abc
[WARN] Could not load config for namespace FX.apollo from Apollo, please check whether the configs are released in Apollo! Return default value now!
Loading key: abc with value: undefined
> [DEBUG] Long polling response: 304, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling response: 304, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling response: 304, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling response: 304, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] refresh config services
[DEBUG] refresh config for namespace: application
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/application?releaseKey=20161009155425-d3a0749c6e20bc15&ip=192.168.2.196
[DEBUG] Config server responds with 304 HTTP status code.
[DEBUG] refresh config for namespace: FX.apollo
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196
[WARN] [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!
[WARN] Sync config failed, will retry. Repository Com.Ctrip.Framework.Apollo.Internals.RemoteConfigRepository, reason: Load Apollo Config failed - appId: SampleApp, cluster: dev, namespace: FX.apollo, url: http://192.168.2.196:8080/configs/SampleApp/dev/FX.apollo?ip=192.168.2.196 [Cause: [status code: 404] Could not find config for namespace - appId: SampleApp, cluster: dev, namespace: FX.apollo, please check whether the configs are released in Apollo!]
[DEBUG] Long polling response: 304, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling response: 304, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling response: 200, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a-1%7d%5d&ip=192.168.2.196
[DEBUG] Loading config from http://192.168.2.196:8080/configs/SampleApp/dev/application?releaseKey=20161009155425-d3a0749c6e20bc15&ip=192.168.2.196&messages=%7b%22details%22%3a%7b%22sampleApp%2bdefault%2bapplication%22%3a1%7d%7d
[DEBUG] Loaded config for application: ApolloConfig{appId='SampleApp', cluster='default', namespaceName='application', configurations=System.Collections.Generic.Dictionary`2[System.String,System.String], releaseKey='20190908092956-1dc5510202741016'}
[DEBUG] Remote Config refreshed!
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a1%7d%5d&ip=192.168.2.196
Changes for namespace application
Change - key: maxQueryCount, oldValue: , newValue: 500, changeType: ADDED

> [DEBUG] Long polling response: 304, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling response: 304, url: http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a1%7d%5d&ip=192.168.2.196
[DEBUG] Long polling from http://192.168.2.196:8080/notifications/v2?appId=SampleApp&cluster=dev&notifications=%5b%7b%22namespaceName%22%3a%22FX.apollo%22%2c%22notificationId%22%3a-1%7d%2c%7b%22namespaceName%22%3a%22application%22%2c%22notificationId%22%3a1%7d%5d&ip=192.168.2.196

获取下:

1e5e3799-9a88-43df-b435-e30d429f8f6b.png

可以拿到值。


新建项目


在配置管理界面中新建一个项目,应用Id为 19090801:

df183fe9-5ed5-4882-980c-4b5e389c9f27.png

创建完成之后,默认的命名空间是application,再新建一个配置:

a70eaa1a-16bc-4d48-adbd-b5912deeb42b.png

程序中 AppID 配置修改成 19090801,运行:c19f7d06-234c-4f74-a4c3-dd9b5f42fba2.png

配置的值已经取到。



创建命名空间


创建一个命名空间,点击【添加Namespace】:

0bcd7e4d-dde3-4ee4-9443-3638360ace7f.png

填写命名空间的名称:dfccbc1b-6871-4d00-828a-3fe580101065.png

设置权限:

da65076f-a4ee-4da8-bd71-d4b66ee74a22.png

新建一个和默认命名空间 application 一样的配置 contractCfgIds ,再新建一个它没有的配置 prdtKeys 并发布:

b3cd5a93-3d5a-4495-8a9b-d981d99a7fc2.png

将 demo 中 anotherConfig 对象获取方法参数改成:

anotherConfig = ConfigService.GetConfig("TEST1.kunggeNS");

运行程序测试:

493782f5-93cf-4673-bb9e-6447c72b1b70.png

默认命名空间下没有则会去 anotherConfig 对象使用的命名空间下找。



源码分析


获取 Config 对象,demo 中获取了2个:

config = ConfigService.GetAppConfig();
anotherConfig = ConfigService.GetConfig("FX.apollo");

第1个 Config 对象 config,调用 ConfigService 的 GetAppConfig 方法没有传参数:

732ec59b-ffe2-42b5-bbca-4504caeb3158.png

没有参数时使用的是一个默认参数 "application",给的 AppId 为 SampleApp 就是一个默认的 application 命名空间:

12ce1cfa-d576-432d-b76a-c3dbfcb92ee8.png

而上面的另外一个 Config 对象 anotherConfig 使用的是命名空间为 FX.apollo ,而这个命名空间在我们的配置中是不存在的。 



参考文档:https://github.com/ctripcorp/apollo/wiki/.Net%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97

分类: 配置中心

发表评论 没有账号,注册评论