{"id":644,"date":"2020-06-01T18:05:53","date_gmt":"2020-06-01T10:05:53","guid":{"rendered":"https:\/\/blog.humh.cn\/?p=644"},"modified":"2020-06-01T18:05:53","modified_gmt":"2020-06-01T10:05:53","slug":"apollo%e4%b9%8b%e7%9b%91%e5%90%ac%e5%99%a8%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/blog.humh.cn\/?p=644","title":{"rendered":"Apollo\u4e4b\u76d1\u542c\u5668\u4f7f\u7528"},"content":{"rendered":"\n<p>Apollo \u672c\u8eab\u662f\u53ef\u4ee5\u57fa\u4e8e\u4e8b\u4ef6\u76d1\u542c<strong>\u5b9e\u65f6\u63a8\u9001<\/strong>\uff08Http\u957f\u8fde\u63a5\uff09\u53d8\u66f4\uff08AutoUpdateConfigChangeListener\uff09\uff0c\u4e5f\u4f1a<strong>\u5b9a\u65f6\u62c9\u53d6<\/strong>\uff08fallback\uff09\u6700\u65b0\u914d\u7f6e\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8bf4\u6211\u4eec\u8fd8\u9700\u8981\u5728\u914d\u7f6e\u66f4\u65b0\u65f6\uff0c\u505a\u4e00\u4e9b\u5176\u4ed6\u7684\u4e1a\u52a1\uff0c\u6bd4\u5982\uff1a\u5bf9\u914d\u7f6e\u505a\u4e86\u4e00\u4e9b\u81ea\u5df1\u7684\u6570\u636e\u7f13\u5b58\uff0c\u6216\u8005\u4e1a\u52a1\u53d8\u66f4\u7b49\uff0c\u90a3\u4e48\u6211\u4eec\u9700\u8981\u63a7\u5236\u76d1\u542c\u5668\uff0capollo\u63d0\u4f9b\u4e86\u8fd9\u6837\u7684\u7b56\u7565\u3002\u53ef\u624b\u52a8\u914d\u7f6e\u4e00\u4e2a\u76d1\u542c\u5668\u5728\u5bb9\u5668\u4e2d\u3002<\/p>\n\n\n\n<p>\u6709\u4e24\u79cd\u65b9\u5f0f\u5982\u4e0b\uff1a <\/p>\n\n\n\n<h4>\u4e00\u3001\u624b\u52a8\u4ee3\u7801\u521d\u59cb\u5316\u76d1\u542c\u5668\u5e76\u914d\u7f6e\u5230Config\u4e0a\u4e0b\u6587\u4e2d<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootApplication\npublic class ConfigApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(ConfigApplication.class, args);\n\n        \/\/ \u624b\u52a8\u914d\u7f6e apolloConfigListener\uff0c\u6dfb\u52a0\u914d\u7f6e\u6539\u52a8\u76d1\u542c\n        ConfigChangeListener configChangeListener = new ConfigChangeListener() {\n            @Override\n            public void onChange(ConfigChangeEvent configChangeEvent) {\n                ConfigChange change;\n                for (String key : configChangeEvent.changedKeys()) {\n                    change = configChangeEvent.getChange(key);\n                    \/\/ \u6253\u5370\u6539\u52a8\u7684\u914d\u7f6e\u4fe1\u606f\n                    System.out.println(\n                            String.format(String.format(\"Change - key: %s, oldValue: %s, newValue: %s, changeType: %s\",\n                            change.getPropertyName(), change.getOldValue(), change.getNewValue(),\n                            change.getChangeType())));\n                }\n            }\n        };\n        \/\/ \u4e5f\u53ef@ApolloConfig\u6ce8\u5165\n        ConfigService.getAppConfig().addChangeListener(configChangeListener);\n    }\n\n}<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6211\u5c06\u5b9e\u73b0\u8fc7\u7a0b\u5199\u5728\u542f\u52a8\u7c7b\u4e2d\uff0c\u624b\u52a8\u5b9e\u73b0\u4e00\u4e2a\u533f\u540d\u7c7bConfigListener\u7136\u540e\u914d\u7f6e\u5230Config\u4e2d\uff0cConfigService.getAppConfig()\u662fApollo\u63d0\u4f9b\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u5bb9\u5668\u9ed8\u8ba4\u7684ApolloConfig\uff0c\u9ed8\u8ba4\u662f\u6307\u7684namespace\u662f\u9ed8\u8ba4\u201capplication\u201d\uff0c\u4e5f\u5c31\u662f\u8bf4\u83b7\u53d6\u7684\u662f\u201capplication\u201d\u7684config\uff0c\u4e0a\u9762\u8fd9\u6837\u64cd\u4f5c\uff0c\u76d1\u542c\u7684\u662fapplication\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230getAppConfig\u7684\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-8.png\"><img width=\"567\" height=\"90\" class=\"wp-image-645 lazyload\" title=\"Apollo\u4e4b\u76d1\u542c\u5668\u4f7f\u7528\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-8.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-8.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-8.png 567w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-8-300x48.png 300w\" sizes=\"(max-width: 567px) 100vw, 567px\"><\/a><\/figure><\/div>\n\n\n\n<p>\u5982\u679c\u60f3\u8981\u83b7\u5f97\u5176\u4ed6namespace\u7684\u8bdd\uff0cgetConfig(\u201cnamespace\u201d)\uff0c\u5f62\u53c2\u4e3a\u4f60\u6240\u6307\u5b9a\u7684namespace\uff0c\u7136\u540eaddChangeListener()\uff0c\u5373\u53ef\u4ee5\u76d1\u542c\u6307\u5b9anamespace\u4e0b\u7684\u914d\u7f6e\u53d8\u6362\u3002\u53ef\u4ee5\u7406\u89e3Config\u662fnamespace\u7ef4\u5ea6\u4e0b\u7684\uff0c\u6bcf\u4e2anamespace\uff08\u540c\u4e00\u5e94\u7528\uff0c\u540c\u4e00\u96c6\u7fa4\uff0c\u540c\u4e00\u73af\u5883\uff09\u4e0b\u90fd\u4e00\u4e2aConfig\u3002<\/p>\n\n\n\n<p>\u652f\u6301\u76d1\u542cnamespace\u4e0b\u6307\u5b9a\u914d\u7f6ekey\uff0c\u63d0\u4f9b\u4e09\u4e2a\u91cd\u8f7d\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u65e0\u53c2\u7684\uff0c\u5219\u76d1\u542cnamespace\u4e0b\u6240\u6709key\uff0c\u4e0b\u56fe\u4e2d\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u76d1\u542c\u6307\u5b9akey\uff0c\u7b2c\u4e8c\u4e2a\u76d1\u542c\u6307\u5b9a\u4ee5\u5176\u5f00\u5934\u7684key\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-9.png\"><img width=\"1186\" height=\"501\" class=\"wp-image-646 lazyload\" title=\"Apollo\u4e4b\u76d1\u542c\u5668\u4f7f\u7528\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-9.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-9.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-9.png 1186w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-9-300x127.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-9-1024x433.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-9-768x324.png 768w\" sizes=\"(max-width: 1186px) 100vw, 1186px\"><\/a><\/figure><\/div>\n\n\n\n<p>\u4f7f\u7528\u5982\u4e0b\uff1a <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">       config.addChangeListener(configChangeListener, new HashSet&lt;String&gt;(){{\n            add(\"nwpu.name\");\n        }}, new HashSet&lt;String&gt;(){{\n            add(\"nwpu\");\n        }});<\/pre>\n\n\n\n<h4> \u4e8c\u3001\u7528@ApolloConfigChangeListener\u6ce8\u89e3\u6807\u8bb0\uff0c\u81ea\u52a8\u6ce8\u518cConfigChangeListener <\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Configuration\npublic class ApolloConfig {\n\n    @Autowired\n    private RefreshScope refreshScope;\n\n    @ApolloConfigChangeListener(value = {\"application\", \"datasource\"})\n    private void onChangeToAll(ConfigChangeEvent changeEvent) {\n        ConfigChange change;\n        for (String key : changeEvent.changedKeys()) {\n            \/\/ \u5982\u679c@ConfigurationProperties\u914d\u7f6e\u53d1\u751f\u53d8\u5316\uff0c\u5219\u9700\u8981\u624b\u52a8\u5237\u65b0\n\/\/            if (key.substring(0, 5).equals(\"nwpu\")) {\n\/\/              refreshScope.refresh(\"nwpuDTO\");\n\/\/            }\n            change = changeEvent.getChange(key);\n            \/\/ \u6253\u5370\u51fa\u65b0\u589e\u6216\u8005\u53d8\u5316\u7684\u914d\u7f6e \u76f8\u5173\u4fe1\u606f\n            System.out.println(String.format(\"Change - key: %s, oldValue: %s, newValue: %s, changeType: %s\",\n                    change.getPropertyName(), change.getOldValue(), change.getNewValue(),\n                    change.getChangeType()));\n        }\n    }\n\n    @ApolloConfigChangeListener(value = \"application\", interestedKeyPrefixes = \"nwpu\")\n    private void onChangeToApplication(ConfigChangeEvent changeEvent) {\n        ConfigChange change;\n        for (String key : changeEvent.changedKeys()) {\n            \/\/ \u5982\u679c@ConfigurationProperties\u914d\u7f6e\u53d1\u751f\u53d8\u5316\uff0c\u5219\u9700\u8981\u624b\u52a8\u5237\u65b0\n            refreshScope.refresh(\"nwpuDTO\");\n            change = changeEvent.getChange(key);\n            \/\/ \u6253\u5370\u51fa\u65b0\u589e\u6216\u8005\u53d8\u5316\u7684\u914d\u7f6e \u76f8\u5173\u4fe1\u606f\n            System.out.println(String.format(\"Change - key: %s, oldValue: %s, newValue: %s, changeType: %s\",\n                    change.getPropertyName(), change.getOldValue(), change.getNewValue(),\n                    change.getChangeType()));\n        }\n    }\n\n}<\/pre>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5e94\u8be5\u662f\u7528\u6ce8\u89e3\u6807\u8bb0\uff0c\u7136\u540eaop\u5207\u9762\u6807\u8bb0\u7684\u65b9\u6cd5\uff0c\u7136\u540e\u6ce8\u5165Config\u4e2d\u3002\u6807\u8bb0\u65b9\u6cd5\u7684\u5b9e\u73b0\u662f\u914d\u7f6e\u53d8\u5316\u540e\u7684\u5904\u7406\u4e1a\u52a1\u3002<\/p>\n\n\n\n<p>\u5206\u6790\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u603b\u5171\u5bf9\u4e24\u4e2aconfig\u64cd\u4f5c\uff0c\u4e00\u4e2aapplication\n config\uff0c\u4e00\u4e2adatasource \nconfig\uff0c\u7136\u540e\u5206\u522b\u6ce8\u518c\u76d1\u542c\u5668\uff0c\u6ce8\u610fapplication\u6ce8\u518c\u4e24\u6b21\u76d1\u542c\u5668\uff0c\u7b2c\u4e00\u6b21\u6ca1\u6709\u6307\u5b9akey\uff0c\u7b2c\u4e8c\u6b21\u6307\u5b9a\u4e86key\u524d\u7f00\uff0c\u6d4b\u8bd5\u53d1\u73b0\u5747\u53ef\u7528\uff0c\u4e5f\u5c31\u662f\u8bf4config\u5141\u8bb8\u540c\u65f6\u6ce8\u518c\u591a\u4e2a\u76d1\u542c\u5668\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apollo \u672c\u8eab\u662f\u53ef\u4ee5\u57fa\u4e8e\u4e8b\u4ef6\u76d1\u542c\u5b9e\u65f6\u63a8\u9001\uff08Http\u957f\u8fde\u63a5\uff09\u53d8\u66f4\uff08AutoUpdateConfigChang [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[35,83,82],"_links":{"self":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/644"}],"collection":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=644"}],"version-history":[{"count":3,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":649,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/644\/revisions\/649"}],"wp:attachment":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}