{"id":2140,"date":"2023-04-02T11:00:31","date_gmt":"2023-04-02T03:00:31","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2140"},"modified":"2023-04-05T19:59:12","modified_gmt":"2023-04-05T11:59:12","slug":"spring-boot-apollo-client-monitoring-configuration-changes-and-dynamic-refreshing","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/02\/spring-boot-apollo-client-monitoring-configuration-changes-and-dynamic-refreshing\/","title":{"rendered":"SpringBoot Apollo\u5ba2\u6237\u7aef\u76d1\u542c\u914d\u7f6e\u53d8\u5316\u3001\u52a8\u6001\u5237\u65b0"},"content":{"rendered":"<blockquote>\n<p><code>@Value<\/code>\uff0c\u52a8\u6001\u5237\u65b0<br \/>\n<code>@ConfigurationProperties<\/code>\uff0c\u9700\u8981\u6dfb\u52a0apollo\u914d\u7f6e\u76d1\u542c\u5668<code>@ApolloConfigChangeListener<\/code>\u5b9e\u73b0\u52a8\u6001\u5237\u65b0<\/p>\n<\/blockquote>\n<h2>\u914d\u7f6e<\/h2>\n<p><!-- more --><\/p>\n<h3>pom.xml<\/h3>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;com.ctrip.framework.apollo&lt;\/groupId&gt;\n    &lt;artifactId&gt;apollo-client&lt;\/artifactId&gt;\n    &lt;version&gt;1.6.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>application.yml<\/h3>\n<pre><code class=\"language-yml\">app:\n  id: 5ff576fef194fa22505f4331\napollo:\n  cluster: default\n  meta: http:\/\/dev.apollo.appblog.cn:8080,http:\/\/dev.apollo.appblog.cn:8081\n  cacheDir: \/opt\/app\/data\n  bootstrap:\n    enabled: true\n    eagerLoad:\n      enabled: true\n    namespaces: application,.mysql-config<\/code><\/pre>\n<h3>@EnableApolloConfig<\/h3>\n<p><code>@EnableApolloConfig<\/code>\u4e0d\u4e00\u5b9a\u8981\u52a0\u5728\u542f\u52a8\u7c7b\u4e0a\uff0c\u52a0\u5728\u88abSpring\u7ba1\u7406\u7684\u7c7b\u4e0a\u5373\u53ef<\/p>\n<h2>\u5c5e\u6027\u6620\u5c04<\/h2>\n<h3>@Value\uff1a\u76f4\u63a5\u5c5e\u6027\u6620\u5c04<\/h3>\n<p><code>@Value<\/code>\uff0c\u52a8\u6001\u5237\u65b0<\/p>\n<pre><code class=\"language-java\">@Value(&quot;${user.userName}&quot;)\nprivate String userName;<\/code><\/pre>\n<h3>@ConfigurationProperties\uff1abean\u5c5e\u6027\u6620\u5c04<\/h3>\n<p><code>@ConfigurationProperties<\/code>\uff0c\u9700\u8981\u6dfb\u52a0apollo\u914d\u7f6e\u76d1\u542c\u5668<code>@ApolloConfigChangeListener<\/code>\u5b9e\u73b0\u52a8\u6001\u5237\u65b0<\/p>\n<pre><code class=\"language-java\">@ApolloConfigChangeListener()\uff1a\u9ed8\u8ba4\u76d1\u542c\u7684\u662fapplication\u547d\u540d\u7a7a\u95f4\n@ApolloConfigChangeListener(&quot;mysql-config&quot;)\uff1a\u6307\u5b9amysql-config\u547d\u540d\u7a7a\u95f4\n@ApolloConfigChangeListener(&quot;${apollo.bootstrap.namespaces}&quot;)\uff1a\u6307\u5b9a\u53c2\u6570\u6240\u914d\u7f6e\u7684\u547d\u540d\u7a7a\u95f4\uff08\u591a\u4e2a\u7528\u9017\u53f7\u5206\u9694\uff09<\/code><\/pre>\n<pre><code class=\"language-java\">import com.ctrip.framework.apollo.model.ConfigChange;\nimport com.ctrip.framework.apollo.model.ConfigChangeEvent;\nimport com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.beans.BeansException;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.cloud.context.environment.EnvironmentChangeEvent;\nimport org.springframework.cloud.context.scope.refresh.RefreshScope;\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.ApplicationContextAware;\nimport org.springframework.stereotype.Component;\n\n\/**\n * \u81ea\u52a8\u5237\u65b0 ConfigurationProperties \u6807\u6ce8\u7684\u7c7b\u5c5e\u6027\n *\/\n@Slf4j\n@Component\npublic class ApolloConfigChanged implements ApplicationContextAware {\n\n    private ApplicationContext applicationContext;\n\n    @Autowired\n    RefreshScope refreshScope;\n\n    \/**\n     * \u5237\u65b0\u7684namespace\u7684\u540d\u5b57\uff1adevGroup.coupon.application\n     * apollo\u81ea\u5b9a\u4e49\u7684\u51e0\u4e2anamespace\uff1a{@link com.ctrip.framework.apollo.core.ConfigConsts}\n     * \u5df2\u91cd\u5199\u4e0d\u9700\u8981\u914d\u7f6evalue\uff0c\u9ed8\u8ba4\u6240\u6709\u7684namespaces\n     *\n     * @param changeEvent\n     *\/\n    @ApolloConfigChangeListener(&quot;${apollo.bootstrap.namespaces}&quot;)\n    private void someChangeHandler(ConfigChangeEvent changeEvent) {\n        log.info(&quot;================Apollo \u81ea\u52a8\u5237\u65b0\u503c \u5f00\u59cb ===========================&quot;);\n\n        for (String changedKey : changeEvent.changedKeys()) {\n\n            ConfigChange configChange = changeEvent.getChange(changedKey);\n            String oldValue = configChange.getOldValue();\n            String newValue = configChange.getNewValue();\n            log.info(&quot;[changedKey:{},oldValue={}, newValue:{}]&quot;, changedKey, oldValue, newValue);\n        }\n\n        refreshProperties(changeEvent);\n\n        log.info(&quot;================Apollo \u81ea\u52a8\u5237\u65b0\u503c \u7ed3\u675f ===========================&quot;);\n    }\n\n    public void refreshProperties(ConfigChangeEvent changeEvent) {\n        \/\/ \u66f4\u65b0\u76f8\u5e94\u7684bean\u7684\u5c5e\u6027\u503c\uff0c\u4e3b\u8981\u662f\u5b58\u5728@ConfigurationProperties\u6ce8\u89e3\u7684bean\n        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));\n        refreshScope.refreshAll();\n    }\n\n    @Override\n    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {\n        this.applicationContext = applicationContext;\n    }\n}<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u7528\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"language-java\">Config config = ConfigService.getAppConfig();\nconfig.addChangeListener(new ConfigChangeListener() {\n    @Override\n    public void onChange(ConfigChangeEvent changeEvent) {\n        for (String key : changeEvent.changedKeys()) {\n            ConfigChange change = changeEvent.getChange(key);\n            System.out.println(String.format(\n                &quot;Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s&quot;,\n                change.getPropertyName(), change.getOldValue(),\n                change.getNewValue(), change.getChangeType()));\n        }\n    }\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>@Value\uff0c\u52a8\u6001\u5237\u65b0 @ConfigurationProperties\uff0c\u9700\u8981\u6dfb\u52a0apollo\u914d\u7f6e\u76d1\u542c\u5668@Ap [&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":[],"class_list":["post-2140","post","type-post","status-publish","format-standard","hentry","category-apollo-disconf","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2140","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=2140"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2140\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}