{"id":1459,"date":"2023-03-23T22:33:15","date_gmt":"2023-03-23T14:33:15","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1459"},"modified":"2023-04-28T20:42:16","modified_gmt":"2023-04-28T12:42:16","slug":"spring-boot-integrate-disconf-client","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/23\/spring-boot-integrate-disconf-client\/","title":{"rendered":"Spring Boot \u96c6\u6210 disconf-client"},"content":{"rendered":"<h2>Disconf web\u7684\u5b89\u88c5\u914d\u7f6e<\/h2>\n<p>Disconf web\u662fDisconf\u7684\u7ba1\u7406\u754c\u9762\u53ca\u670d\u52a1\u5668\uff0c\u901a\u8fc7\u5b83\u6765\u914d\u7f6e\u7ba1\u7406\u6211\u4eec\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<h2>\u5ba2\u6237\u7aef\u914d\u7f6e<\/h2>\n<p><!-- more --><\/p>\n<p>\uff081\uff09\u521b\u5efa\u666e\u901a\u7684SpringBoot\u9879\u76eeconfig-disconf\uff0c\u5728pom.xml\u6587\u4ef6\u4e2d\u589e\u52a0\u5982\u4e0b\u4f9d\u8d56<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n  &lt;groupId&gt;com.baidu.disconf&lt;\/groupId&gt;\n  &lt;artifactId&gt;disconf-client&lt;\/artifactId&gt;\n  &lt;version&gt;2.6.36&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\uff082\uff09\u672c\u5730\u521b\u5efa<code>conf\/spring-disconf.xml<\/code>\u914d\u7f6e\u6587\u4ef6<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\n        http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.1.xsd\n        http:\/\/www.springframework.org\/schema\/aop\n        http:\/\/www.springframework.org\/schema\/aop\/spring-aop-3.0.xsd&quot;&gt;\n\n    &lt;aop:aspectj-autoproxy proxy-target-class=&quot;true&quot;\/&gt;\n\n    &lt;!-- \u4f7f\u7528disconf\u5fc5\u987b\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e --&gt;\n    &lt;bean id=&quot;disconfMgrBean&quot; class=&quot;com.baidu.disconf.client.DisconfMgrBean&quot;\n          destroy-method=&quot;destroy&quot;&gt;\n        &lt;property name=&quot;scanPackage&quot; value=&quot;cn.appblog.service.provider.config&quot;\/&gt;\n    &lt;\/bean&gt;\n    &lt;bean id=&quot;disconfMgrBean2&quot; class=&quot;com.baidu.disconf.client.DisconfMgrBeanSecond&quot;\n          init-method=&quot;init&quot; destroy-method=&quot;destroy&quot;&gt;\n    &lt;\/bean&gt;\n\n    &lt;!-- \u9700\u8981\u6258\u7ba1\u7684\u914d\u7f6e\u6587\u4ef6 --&gt;\n    &lt;bean id=&quot;configproperties_no_reloadable_disconf&quot;\n          class=&quot;com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean&quot;&gt;\n        &lt;property name=&quot;locations&quot;&gt;\n            &lt;list&gt;\n                &lt;value&gt;application.yml&lt;\/value&gt;\n                &lt;value&gt;disconf.properties&lt;\/value&gt;\n            &lt;\/list&gt;\n        &lt;\/property&gt;\n    &lt;\/bean&gt;\n\n&lt;\/beans&gt;<\/code><\/pre>\n<blockquote>\n<p>\u914d\u7f6escanPackage\uff08\u626b\u63cf\u914d\u7f6e\u6587\u4ef6\uff09<br \/>\n\u914d\u7f6elocations\uff08\u672c\u5730\u914d\u7f6e\u6587\u4ef6\uff09<\/p>\n<\/blockquote>\n<p>\uff083\uff09\u672c\u5730\u65b0\u589edisconf\u914d\u7f6e\u6587\u4ef6<code>disconf.properties<\/code><\/p>\n<pre><code># \u662f\u5426\u4f7f\u7528\u8fdc\u7a0b\u914d\u7f6e\u6587\u4ef6\n# true(\u9ed8\u8ba4)\u4f1a\u4ece\u8fdc\u7a0b\u83b7\u53d6\u914d\u7f6e false\u5219\u76f4\u63a5\u83b7\u53d6\u672c\u5730\u914d\u7f6e\ndisconf.enable.remote.conf=true\n\n#\n# \u914d\u7f6e\u670d\u52a1\u5668\u7684 HOST,\u7528\u9017\u53f7\u5206\u9694 127.0.0.1:8000,127.0.0.1:8000\n#\ndisconf.conf_server_host=192.168.1.20:8015\n\n# \u7248\u672c, \u8bf7\u91c7\u7528 X_X_X_X \u683c\u5f0f\ndisconf.version=1_0_0_0\n\n# APP \u8bf7\u91c7\u7528 \u4ea7\u54c1\u7ebf_\u670d\u52a1\u540d \u683c\u5f0f\ndisconf.app=service-provider\n\n# \u73af\u5883\ndisconf.env=rd\n\n# \u5ffd\u7565\u54ea\u4e9b\u5206\u5e03\u5f0f\u914d\u7f6e\uff0c\u7528\u9017\u53f7\u5206\u9694\ndisconf.ignore=\n\n# \u83b7\u53d6\u8fdc\u7a0b\u914d\u7f6e \u91cd\u8bd5\u6b21\u6570\uff0c\u9ed8\u8ba4\u662f3\u6b21\ndisconf.conf_server_url_retry_times=1\n# \u83b7\u53d6\u8fdc\u7a0b\u914d\u7f6e \u91cd\u8bd5\u65f6\u4f11\u7720\u65f6\u95f4\uff0c\u9ed8\u8ba4\u662f5\u79d2\ndisconf.conf_server_url_retry_sleep_seconds=1\n\n# \u81ea\u5b9a\u4e49\u7684\u4e0b\u8f7d\u8def\u5f84\ndisconf.user_define_download_dir=config\n\n#disconf.enable_local_download_dir_in_class_path=false<\/code><\/pre>\n<p>\uff084\uff09\u672c\u5730\u5e94\u7528\u914d\u7f6e\u6587\u4ef6<code>application.yml<\/code><\/p>\n<pre><code class=\"language-yml\">server:\n  port: 8081\n\nspring:\n  application:\n    name: service-provider<\/code><\/pre>\n<p>\uff085\uff09\u5728<code>ConfigDisconfApplication<\/code>\u4e0a\u5bfc\u5165\u4f9d\u8d56\u6587\u4ef6<\/p>\n<pre><code class=\"language-java\">@ImportResource({&quot;classpath:conf\/spring-disconf.xml&quot;})\n@SpringBootApplication\npublic class ConfigDisconfApplication {\n\n   public static void main(String[] args) {\n      SpringApplication.run(ConfigDisconfApplication.class, args);\n   }\n}<\/code><\/pre>\n<p>\u81f3\u6b64\uff0c\u5ba2\u6237\u7aef\u5df2\u7ecf\u914d\u7f6e\u5b8c\u6bd5<\/p>\n<h2>\u670d\u52a1\u7aef\u914d\u7f6e<\/h2>\n<h3>\u65b0\u5efa\u914d\u7f6eAPP<\/h3>\n<p>APP\u540d\u79f0\u9700\u4e0e<code>disconf.properties<\/code>\u4e2d\u7684<code>disconf.app<\/code>\u914d\u7f6e\u4e00\u81f4<\/p>\n<h3>\u65b0\u5efa\u914d\u7f6e\u6587\u4ef6<\/h3>\n<p>\uff081\uff09application.yml<\/p>\n<p>\u9009\u62e9\u8f93\u5165\u6587\u672c\u65b9\u5f0f\uff0c\u5185\u5bb9\u4e0e\u672c\u5730<code>application.yml<\/code>\u4e00\u81f4\uff0c\u4e0a\u4f20\u914d\u7f6e\u6587\u4ef6\u5bb9\u6613\u5bfc\u81f4\u4e2d\u6587\u4e71\u7801<\/p>\n<p>\uff082\uff09disconf.properties<\/p>\n<p>\u9009\u62e9\u8f93\u5165\u6587\u672c\u65b9\u5f0f\uff0c\u5185\u5bb9\u4e0e\u672c\u5730<code>disconf.properties<\/code>\u4e00\u81f4\uff0c\u4e0a\u4f20\u914d\u7f6e\u6587\u4ef6\u5bb9\u6613\u5bfc\u81f4\u4e2d\u6587\u4e71\u7801<\/p>\n<p>\uff083\uff09server.properties<\/p>\n<pre><code>host=192.168.1.1\nport=8818<\/code><\/pre>\n<p>\uff084\uff09\u542f\u52a8\u5e94\u7528\uff0c\u53ef\u4ee5\u770b\u5230\u914d\u7f6e\u5df2\u7ecf\u751f\u6548<\/p>\n<pre><code>--------------- LOAD CONFIG START ---------------\nSERVER disconf.conf_server_host: [192.168.1.20:8015]\nSERVER disconf.version: 1_0_0_0\nSERVER disconf.app: service-provider\nSERVER disconf.env: rd\nSERVER disconf.enable.remote.conf: true\nSERVER disconf.debug: false\nSERVER disconf.user_define_download_dir: config\nSERVER disconf.ignore: []\n--------------- LOAD CONFIG END ---------------\n******************************* DISCONF START FIRST SCAN *******************************\ngiven scan urls are empty. set urls in the configuration\nexecute http request [null], status code [200]\nexecute http request [null], status code [200]\nlog4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).\nlog4j:WARN Please initialize the log4j system properly.\nzookeeper: 192.168.1.23:2181,192.168.1.24:2181,192.168.1.25:2181 , connected.\nzoo prefix: \/disconf\nexists connect lost... will retry 0 org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for \/disconf\nsleep 2\nexists connect lost... will retry 1 org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for \/disconf\nsleep 4\nexists connect lost... will retry 2 org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for \/disconf\nsleep 6\nzk SyncConnected\n******************************* DISCONF END FIRST SCAN *******************************\nTomcat initialized with port(s): 8081 (http)\nStarting service [Tomcat]\nStarting Servlet engine: [Apache Tomcat\/9.0.21]\nInitializing Spring embedded WebApplicationContext\nRoot WebApplicationContext: initialization completed in 38823 ms\n******************************* DISCONF START SECOND SCAN *******************************\nConf File Map: \ndisconf-file:   server.properties   \n    DisconfCenterFile [\n    keyMaps={port=FileItemValue{value=8812, field=private int cn.appblog.service.provider.config.ServerConfig.port, setMethod=public void cn.appblog.service.provider.config.ServerConfig.setPort(int)}, host=FileItemValue{value=192.168.1.1, field=private java.lang.String cn.appblog.service.provider.config.ServerConfig.host, setMethod=public void cn.appblog.service.provider.config.ServerConfig.setHost(java.lang.String)}}\n    additionalKeyMaps={}\n    cls=class cn.appblog.service.provider.config.ServerConfig\n    remoteServerUrl=\/api\/config\/file?version=1_0_0_0&amp;app=service-provider&amp;env=rd&amp;key=server.properties&amp;type=0]\n\nConf Item Map: \n\n******************************* DISCONF END *******************************\nInvalid JavaBean property &#039;locations&#039; being accessed! Ambiguous write methods found next to actually used [public void com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean.setLocations(java.util.List)]: [public void org.springframework.core.io.support.PropertiesLoaderSupport.setLocations(org.springframework.core.io.Resource[])]\ncreate: \/disconf\/service-provider_1_0_0_0_rd\ncreate: \/disconf\/service-provider_1_0_0_0_rd\/file\ncreate: \/disconf\/service-provider_1_0_0_0_rd\/file\/application.yml\ncreate: \/disconf\/service-provider_1_0_0_0_rd\/file\/disconf.properties\ncreate: \/disconf\/service-provider_1_0_0_0_rd\/file\/server.properties<\/code><\/pre>\n<p>\uff085\uff09disconf-web\u72b6\u6001\u66f4\u65b0<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/Disconf%20\u914d\u7f6e.png\" alt=\"Disconf \u914d\u7f6e\" \/><\/p>\n<p>\uff086\uff09\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff0c\u770b\u770b\u662f\u5426\u5b9e\u65f6\u63a8\u9001\u914d\u7f6e\u66f4\u65b0<\/p>\n<pre><code>============GOT UPDATE EVENT WatchedEvent state:SyncConnected type:NodeDataChanged path:\/disconf\/service-provider_1_0_0_0_rd\/file\/server.properties: (\/disconf\/service-provider_1_0_0_0_rd\/file\/server.properties,server.properties,\u914d\u7f6e\u6587\u4ef6)======================\nstart to call class cn.appblog.service.provider.config.ServerConfig$$EnhancerBySpringCGLIB$$1937a869\n\u914d\u7f6e\u6587\u4ef6\u66f4\u65b0\uff01\u66f4\u65b0\u4e3a\uff1a[ServerConfig{host=&#039;192.168.1.1&#039;, port=8818}]<\/code><\/pre>\n<h2>\u5c0f\u7ed3<\/h2>\n<p>Disconf\u5bf9\u4e8e\u5206\u5e03\u5f0f\u7cfb\u7edf\u975e\u5e38\u6709\u7528\uff0c\u66f4\u65b0\u914d\u7f6e\u6587\u4ef6\u53ea\u9700\u8981\u53bb\u63a7\u5236\u53f0\u66f4\u65b0\uff0c\u5b83\u5c31\u4f1a\u81ea\u52a8\u63a8\u9001\u5230\u6bcf\u53f0\u670d\u52a1\u5668\uff0c\u5229\u7528\u597d\u76d1\u63a7\u914d\u7f6e\u529f\u80fd\u505a\u4e00\u4e9bdb\u3001redis\u7b49\u5b9e\u4f8b\u7684\u5237\u65b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Disconf web\u7684\u5b89\u88c5\u914d\u7f6e Disconf web\u662fDisconf\u7684\u7ba1\u7406\u754c\u9762\u53ca\u670d\u52a1\u5668\uff0c\u901a\u8fc7\u5b83\u6765\u914d\u7f6e\u7ba1\u7406\u6211 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[362,41],"tags":[360],"class_list":["post-1459","post","type-post","status-publish","format-standard","hentry","category-apollo-disconf","category-spring-boot","tag-disconf"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1459"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1459\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}