{"id":1471,"date":"2023-03-24T22:25:39","date_gmt":"2023-03-24T14:25:39","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1471"},"modified":"2023-04-28T20:35:14","modified_gmt":"2023-04-28T12:35:14","slug":"spring-cloud-refreshscope-and-eventlistener-implement-nacos-configuration-update-listening","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/24\/spring-cloud-refreshscope-and-eventlistener-implement-nacos-configuration-update-listening\/","title":{"rendered":"Spring Cloud @RefreshScope\u548c@EventListener\u5b9e\u73b0Nacos\u914d\u7f6e\u66f4\u65b0\u76d1\u542c"},"content":{"rendered":"<h2>\u95ee\u9898\u63cf\u8ff0<\/h2>\n<p>\u4f7f\u7528<code>@RefreshScope<\/code>\u4f1a\u5237\u65b0\u5728Sprign IOC\u4e2d\u6240\u6709Bean\u4e2d\u4f7f\u7528<code>@Value<\/code>\u7684\u503c\uff0c\u4f46\u662f\u5728\u914d\u7f6e\u7c7b\u4e2d\u4f7f\u7528\u65b9\u6cd5\u53bb\u914d\u7f6e\u7684\u5176\u4ed6\u7c7b\u53c2\u6570\u5e76\u4e0d\u4f1a\u6539\u53d8\uff0c\u4f8b\u5982<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">@Slf4j\n@Configuration\n@RefreshScope\npublic class SentinelConfig {\n    @Bean\n    public ZuulFilter sentinelZuulPreFilter() {\n        return new SentinelZuulPreFilter();\n    }\n\n    @Bean\n    public ZuulFilter sentinelZuulPostFilter() {\n        return new SentinelZuulPostFilter();\n    }\n\n    @Bean\n    public ZuulFilter sentinelZuulErrorFilter() {\n        return new SentinelZuulErrorFilter();\n    }\n\n    @PostConstruct\n    public void init() {\n        ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());\n        Config config = ConfigService.getAppConfig();\n        \/\/Nacos\u540e\u53f0\u914d\u7f6e\u66f4\u65b0\uff0cflowRules\u5e76\u4e0d\u4f1a\u6539\u53d8\uff0c\u9700\u8981\u624b\u52a8loadRules\n        String flowRulesJson = config.getProperty(&quot;sentinel.flowRules&quot;,\n                &quot;[{\\&quot;burst\\&quot;:0,\\&quot;controlBehavior\\&quot;:0,\\&quot;count\\&quot;:3.0,\\&quot;grade\\&quot;:1,\\&quot;intervalSec\\&quot;:1,\\&quot;maxQueueingTimeoutMs\\&quot;:500,\\&quot;resource\\&quot;:\\&quot;appblog-service\\&quot;,\\&quot;resourceMode\\&quot;:1}]&quot;);\n        Set&lt;GatewayFlowRule&gt; flowRules = JSON.parseObject(flowRulesJson, new TypeReference&lt;Set&lt;GatewayFlowRule&gt;&gt;(){});\n        GatewayRuleManager.loadRules(flowRules);\n    }\n}<\/code><\/pre>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<pre><code class=\"language-java\">\/\/\u4f7f\u7528\u6b64\u65b9\u6cd5\u76d1\u542c\u4e8b\u4ef6\n@EventListener\npublic void envListener(EnvironmentChangeEvent event) {\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@Slf4j\n@Configuration\n@RefreshScope\npublic class SentinelConfig {\n    @Value(&quot;${sentinel.flowRules:[{\\&quot;burst\\&quot;:0,\\&quot;controlBehavior\\&quot;:0,\\&quot;count\\&quot;:3.0,\\&quot;grade\\&quot;:1,\\&quot;intervalSec\\&quot;:1,\\&quot;maxQueueingTimeoutMs\\&quot;:500,\\&quot;resource\\&quot;:\\&quot;appblog-service\\&quot;,\\&quot;resourceMode\\&quot;:1}]}&quot;)\n    private String flowRulesJson;\n\n    @Bean\n    public ZuulFilter sentinelZuulPreFilter() {\n        return new SentinelZuulPreFilter();\n    }\n\n    @Bean\n    public ZuulFilter sentinelZuulPostFilter() {\n        return new SentinelZuulPostFilter();\n    }\n\n    @Bean\n    public ZuulFilter sentinelZuulErrorFilter() {\n        return new SentinelZuulErrorFilter();\n    }\n\n    @PostConstruct\n    public void init() {\n        ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());\n        Config config = ConfigService.getAppConfig();\n        String flowRulesJson = config.getProperty(&quot;sentinel.flowRules&quot;,\n                &quot;[{\\&quot;burst\\&quot;:0,\\&quot;controlBehavior\\&quot;:0,\\&quot;count\\&quot;:3.0,\\&quot;grade\\&quot;:1,\\&quot;intervalSec\\&quot;:1,\\&quot;maxQueueingTimeoutMs\\&quot;:500,\\&quot;resource\\&quot;:\\&quot;appblog-service\\&quot;,\\&quot;resourceMode\\&quot;:1}]&quot;);\n        Set&lt;GatewayFlowRule&gt; flowRules = JSON.parseObject(flowRulesJson, new TypeReference&lt;Set&lt;GatewayFlowRule&gt;&gt;(){});\n        GatewayRuleManager.loadRules(flowRules);\n    }\n\n    @EventListener\n    public void envChangeListener(EnvironmentChangeEvent event) {\n        log.info(&quot;Sentinel\u914d\u7f6e\u5237\u65b0&quot;);\n        log.info(flowRulesJson);\n        Set&lt;GatewayFlowRule&gt; flowRules = JSON.parseObject(flowRulesJson, new TypeReference&lt;Set&lt;GatewayFlowRule&gt;&gt;(){});\n        GatewayRuleManager.loadRules(flowRules);\n    }\n}<\/code><\/pre>\n<h2>\u539f\u56e0\u5206\u6790<\/h2>\n<p>\u5728\u8c03\u7528\u5237\u65b0\u65b9\u6cd5\u662f\u4f1a\u4ea7\u751f\u4e00\u4e2a<code>EnvironmentChangeEvent<\/code>\u4e8b\u4ef6\u3002\u8fdb\u5165<code>ContextRefresher<\/code>\u6e90\u7801\uff0c\u770b\u4e0brefresh\u63a5\u53e3\uff1b<\/p>\n<pre><code class=\"language-java\">public synchronized Set&lt;String&gt; refresh() {\n    Map&lt;String, Object&gt; before = extract(\n            this.context.getEnvironment().getPropertySources());\n    addConfigFilesToEnvironment();\n    Set&lt;String&gt; keys = changes(before,\n            extract(this.context.getEnvironment().getPropertySources())).keySet();\n    \/\/ \u6ce8\u610f\u8fd9\u4e00\u884c\uff0c\u629b\u51fa\u4e86\u4e00\u4e2a\u53d8\u66f4\u4e8b\u4ef6\n    this.context.publishEvent(new EnvironmentChangeEvent(context, keys));\n    this.scope.refreshAll();\n    return keys;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u63cf\u8ff0 \u4f7f\u7528@RefreshScope\u4f1a\u5237\u65b0\u5728Sprign IOC\u4e2d\u6240\u6709Bean\u4e2d\u4f7f\u7528@Value\u7684\u503c\uff0c\u4f46\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,42],"tags":[],"class_list":["post-1471","post","type-post","status-publish","format-standard","hentry","category-nacos","category-spring-cloud"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1471","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=1471"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1471\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}