{"id":2158,"date":"2023-04-02T13:31:07","date_gmt":"2023-04-02T05:31:07","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2158"},"modified":"2023-04-05T19:45:13","modified_gmt":"2023-04-05T11:45:13","slug":"nacos-implements-zuul-gateway-dynamic-routing-configuration-refresh","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/02\/nacos-implements-zuul-gateway-dynamic-routing-configuration-refresh\/","title":{"rendered":"Nacos\u5b9e\u73b0Zuul\u7f51\u5173\u52a8\u6001\u8def\u7531\u914d\u7f6e\u5237\u65b0"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\u610f\uff1a\u5982\u679cZuul\u7f51\u5173\u5e94\u7528\u6ce8\u518c\u5230Eureka\u6216Nacos\uff0c\u5219\u4f1a\u81ea\u52a8\u5237\u65b0\u8def\u7531\u914d\u7f6e\uff0c\u65e0\u9700\u624b\u52a8\u5237\u65b0<\/p>\n<\/blockquote>\n<h3>Nacos\u914d\u7f6e<\/h3>\n<pre><code class=\"language-yml\">routes:\n  services:\n    alipay: ok-cloud-service-alipay\n    wechatpay: ok-cloud-service-wechatpay<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">import lombok.Data;\nimport org.springframework.beans.factory.annotation.Value;\nimport org.springframework.boot.context.properties.ConfigurationProperties;\nimport org.springframework.cloud.context.config.annotation.RefreshScope;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\nimport java.util.HashMap;\nimport java.util.Map;\n\n\/**\n * @author yezhou\n *\/\n@Data\n@Configuration\n@RefreshScope\npublic class RouteConfig {\n\n    @Value(&quot;${zuul.prefix}&quot;)\n    private String prefix;\n\n    @Bean\n    @ConfigurationProperties(prefix = &quot;routes.services&quot;)\n    public Map&lt;String, String&gt; services() {\n        return new HashMap&lt;&gt;();\n    }\n\n}<\/code><\/pre>\n<h3>\u52a8\u6001\u8def\u7531\u5b9e\u73b0<\/h3>\n<pre><code class=\"language-java\">import com.alibaba.fastjson.JSON;\nimport com.lianlian.global.thai.provider.channel.callback.config.RouteConfig;\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.autoconfigure.web.ServerProperties;\nimport org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;\nimport org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;\nimport org.springframework.cloud.netflix.zuul.filters.ZuulProperties;\nimport org.springframework.stereotype.Component;\nimport org.springframework.util.StringUtils;\n\nimport java.util.LinkedHashMap;\nimport java.util.Map;\n\n\/**\n * @author yezhou\n *\/\n@Slf4j\n@Component\npublic class DynamicZuulRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {\n\n    private ZuulProperties properties;\n\n    @Autowired\n    private RouteConfig routeConfig;\n\n    public DynamicZuulRouteLocator(ServerProperties serverProperties, ZuulProperties properties) {\n        super(serverProperties.getServlet().getContextPath(), properties);\n        this.properties = properties;\n    }\n\n    @Override\n    public void refresh() {\n        doRefresh();\n    }\n\n    @Override\n    protected Map&lt;String, ZuulProperties.ZuulRoute&gt; locateRoutes() {\n        log.info(&quot;DynamicZuulRouteLocator.locateRoutes: {}&quot;, JSON.toJSONString(routeConfig.services()));\n        Map&lt;String, ZuulProperties.ZuulRoute&gt; routesMap = new LinkedHashMap&lt;&gt;();\n\n        \/\/ \u9759\u6001\u8def\u7531\u4fe1\u606f\n        routesMap.putAll(super.locateRoutes());\n\n        \/\/ \u52a8\u6001\u52a0\u8f7d\u8def\u7531\u4fe1\u606f\n        Map&lt;String, String&gt; services = routeConfig.services();\n        for (Map.Entry&lt;String, String&gt; entry : services.entrySet()) {\n            ZuulProperties.ZuulRoute zuulRoute = new ZuulProperties.ZuulRoute();\n            zuulRoute.setId(entry.getKey());\n            String path = String.format(&quot;\/%s\/**&quot;, entry.getKey());\n            zuulRoute.setPath(path);\n            zuulRoute.setServiceId(entry.getValue());\n            zuulRoute.setRetryable(true);\n            routesMap.put(path, zuulRoute);\n        }\n\n        Map&lt;String, ZuulProperties.ZuulRoute&gt; values = new LinkedHashMap&lt;&gt;();\n        for (Map.Entry&lt;String, ZuulProperties.ZuulRoute&gt; entry : routesMap.entrySet()) {\n            String path = entry.getKey();\n            if (!path.startsWith(&quot;\/&quot;)) {\n                path = &quot;\/&quot; + path;\n            }\n            if (StringUtils.hasText(this.properties.getPrefix())) {\n                path = this.properties.getPrefix() + path;\n                if (!path.startsWith(&quot;\/&quot;)) {\n                    path = &quot;\/&quot; + path;\n                }\n            }\n            values.put(path, entry.getValue());\n        }\n        return values;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\u610f\uff1a\u5982\u679cZuul\u7f51\u5173\u5e94\u7528\u6ce8\u518c\u5230Eureka\u6216Nacos\uff0c\u5219\u4f1a\u81ea\u52a8\u5237\u65b0\u8def\u7531\u914d\u7f6e\uff0c\u65e0\u9700\u624b\u52a8\u5237\u65b0 Nacos\u914d\u7f6e  [&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":[440,250],"class_list":["post-2158","post","type-post","status-publish","format-standard","hentry","category-nacos","category-spring-cloud","tag-nacos","tag-zuul"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2158","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=2158"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2158\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}