{"id":719,"date":"2024-03-20T15:08:12","date_gmt":"2024-03-20T07:08:12","guid":{"rendered":"http:\/\/www.huerpu.cc:7000\/?p=719"},"modified":"2024-03-20T15:38:02","modified_gmt":"2024-03-20T07:38:02","slug":"spring-cloudfinchley%e7%89%88%e6%9c%ac%e7%b3%bb%e5%88%97%e6%95%99%e7%a8%8b%e4%ba%94-%e6%9c%8d%e5%8a%a1%e7%bd%91%e5%85%b3zuul","status":"publish","type":"post","link":"http:\/\/www.huerpu.cc:7000\/?p=719","title":{"rendered":"Spring Cloud(Finchley\u7248\u672c)\u7cfb\u5217\u6559\u7a0b(\u4e94) \u670d\u52a1\u7f51\u5173(Zuul)"},"content":{"rendered":"<h3>Spring Cloud(Finchley\u7248\u672c)\u7cfb\u5217\u6559\u7a0b(\u4e94) \u670d\u52a1\u7f51\u5173(Zuul)<\/h3>\n<h3>\u4e00\u3001Zuul\u7b80\u4ecb<\/h3>\n<p>Zuul \u662f Netflix OSS \u4e2d\u7684\u4e00\u5458\uff0c\u662f\u4e00\u4e2a\u57fa\u4e8e JVM <strong>\u8def\u7531\u5668<\/strong>\u548c<strong>\u670d\u52a1\u7aef\u7684\u8d1f\u8f7d\u5747\u8861\u5668<\/strong>\u3002\u63d0\u4f9b\u8def\u7531\u3001\u76d1\u63a7\u3001\u5f39\u6027\u3001\u5b89\u5168\u7b49\u65b9\u9762\u7684\u670d\u52a1\u6846\u67b6\u3002Zuul \u80fd\u591f\u4e0e Eureka\u3001Ribbon\u3001Hystrix \u7b49\u7ec4\u4ef6\u914d\u5408\u4f7f\u7528\u3002<\/p>\n<p>zuul\u6838\u5fc3\u529f\u80fd\u662f<strong>\u8fc7\u6ee4\u5668<\/strong>\u3001\u8def\u7531\u3001\u5f02\u5e38\u5904\u7406\uff0c\u901a\u8fc7\u8fc7\u6ee4\u5668\u8fd8\u80fd\u6269\u5c55\u51fa\u5176\u4ed6\u529f\u80fd\uff1a<\/p>\n<blockquote>\n<p>1\uff09\u52a8\u6001\u8def\u7531\u30012\uff09\u8bf7\u6c42\u76d1\u63a7\u30013\uff09\u8ba4\u8bc1\u9274\u6743\u30014\uff09\u538b\u529b\u6d4b\u8bd5\u30015\uff09\u7070\u5ea6\u53d1\u5e03<\/p>\n<\/blockquote>\n<p>Zuul\u7684\u4e3b\u8981\u529f\u80fd\u662f\u8def\u7531\u8f6c\u53d1\u548c\u8fc7\u6ee4\u5668\u3002\u8def\u7531\u529f\u80fd\u662f\u5fae\u670d\u52a1\u7684\u4e00\u90e8\u5206\uff0c\u6bd4\u5982\/api\/user\u8f6c\u53d1\u5230\u5230user\u670d\u52a1\uff0c\/api\/shop\u8f6c\u53d1\u5230\u5230shop\u670d\u52a1\u3002zuul\u9ed8\u8ba4\u548cRibbon\u7ed3\u5408\u5b9e\u73b0\u4e86\u8d1f\u8f7d\u5747\u8861\u7684\u529f\u80fd\u3002<\/p>\n<h3>\u4e8c\u3001\u521b\u5efaZuul\u5de5\u7a0b<\/h3>\n<p>\u590d\u5236eurekaClient\u5de5\u7a0b\uff0c\u4fee\u6539\u540d\u79f0\u4e3ahepServiceZuul\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;artifactId&gt;hepServiceZuul&lt;\/artifactId&gt;\n&lt;name&gt;hepServiceZuul&lt;\/name&gt;\n&lt;description&gt;hepServiceZuul&lt;\/description&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-netflix-zuul&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-netflix-eureka-client&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>\u4e09\u3001\u542f\u7528\u5e76\u914d\u7f6eZuul<\/h3>\n<p>\u542f\u52a8\u7c7bHepServiceZuulApplication\u52a0\u4e0a\u6ce8\u89e3@EnableZuulProxy\uff0c\u5f00\u542fzuul\u7684\u529f\u80fd<\/p>\n<pre><code class=\"language-java\">import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.cloud.netflix.zuul.EnableZuulProxy;\n\n@SpringBootApplication\n@EnableDiscoveryClient\n@EnableZuulProxy\npublic class HepServiceZuulApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(HepServiceZuulApplication.class, args);\n    }\n\n}<\/code><\/pre>\n<p>\u5728application.yml\u4e0a\u589e\u52a0<code>\/hep-a\/**<\/code>\u548c<code>\/hep-b\/**<\/code><\/p>\n<pre><code class=\"language-yml\">zuul:\n  routes:\n    hep-a:\n      path: \/hep-a\/**\n      serviceId: hepServiceRibbonHystrix\n    hep-b:\n      path: \/hep-b\/**\n      serviceId: hepServiceOpenFeignHystrix<\/code><\/pre>\n<p>application.yml\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b<\/p>\n<pre><code class=\"language-YML\">server:\n  port: 8011 # \u7aef\u53e3\u53f7\n\nspring:\n  application:\n    name: hepServiceZuul # Eureka\u540d\u79f0\n\nzuul:\n  routes:\n    hep-a:\n      path: \/hep-a\/**\n      serviceId: hepServiceRibbonHystrix\n    hep-b:\n      path: \/hep-b\/**\n      serviceId: hepServiceOpenFeignHystrix\n\neureka:\n  instance:\n    prefer-ip-address: false\n    hostname: hepServiceZuul\n  client:\n    healthcheck:\n      enabled: true\n    fetch-registry: true\n    register-with-eureka: true\n    service-url:\n      defaultZone: http:\/\/eureka:eurekapwd@www.huerpu.cc:1678\/eureka\/<\/code><\/pre>\n<p>\u6307\u5b9a\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\u7684\u5730\u5740\u4e3ahttp:\/\/eureka:eurekapwd@www.huerpu.cc:1678\/eureka\/\uff0c\u670d\u52a1\u7684\u7aef\u53e3\u4e3a8011\uff0c\u670d\u52a1\u540d\u4e3ahepServiceZuul\uff1b\u4ee5\/hep-a\/ \u5f00\u5934\u7684\u8bf7\u6c42\u90fd\u8f6c\u53d1\u7ed9hepServiceRibbonHystrix\u670d\u52a1\uff1b\u4ee5\/hep-b\/\u5f00\u5934\u7684\u8bf7\u6c42\u90fd\u8f6c\u53d1\u7ed9hepServiceOpenFeignHystrix\u670d\u52a1\uff1b<\/p>\n<p>win11\u7684host\u6587\u4ef6\u589e\u52a0\u4e86\u4e0b\u9762\u4e09\u4e2a<\/p>\n<pre><code class=\"language-host\">127.0.0.1   hepServiceZuul\n127.0.0.1   hepServiceRibbonHystrix\n127.0.0.1   hepServiceOpenFeignHystrix<\/code><\/pre>\n<h3>\u56db\u3001\u9a8c\u8bc1Zuul<\/h3>\n<p>\u6253\u5f00\u6d4f\u89c8\u5668\u8bbf\u95eehttp:\/\/localhost:8011\/hep-a\/getUserById<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/03\/20\/image-20240320143408376.png\" alt=\"image-20240320143408376\" \/><\/p>\n<p>\u8bbf\u95eehttp:\/\/localhost:8011\/hep-b\/getUserById<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/03\/20\/image-20240320143438028.png\" alt=\"image-20240320143438028\" \/><\/p>\n<p>\u8fd9\u8bf4\u660ezuul\u8d77\u5230\u4e86\u8def\u7531\u7684\u4f5c\u7528\u3002<\/p>\n<h4>\u4e94\u3001\u670d\u52a1\u8fc7\u6ee4<\/h4>\n<p>zuul\u4e0d\u4ec5\u53ea\u662f\u8def\u7531\uff0c\u5e76\u4e14\u8fd8\u80fd\u8fc7\u6ee4\uff0c\u505a\u4e00\u4e9b\u5b89\u5168\u9a8c\u8bc1\u3002\u7ee7\u7eed\u6539\u9020\u5de5\u7a0b\uff0c\u589e\u52a0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684HepZuulFilter\u3002<\/p>\n<pre><code class=\"language-java\">package cc.huerpu.eurekaserver.config;\n\nimport com.netflix.zuul.ZuulFilter;\nimport com.netflix.zuul.context.RequestContext;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.stereotype.Component;\n\nimport javax.servlet.http.HttpServletRequest;\n\n@Component\npublic class HepZuulFilter extends ZuulFilter {\n\n    private static Logger log = LoggerFactory.getLogger(HepZuulFilter.class);\n    @Override\n    public String filterType() {\n        return &quot;pre&quot;;\n    }\n\n    @Override\n    public int filterOrder() {\n        return 0;\n    }\n\n    @Override\n    public boolean shouldFilter() {\n        return true;\n    }\n\n    @Override\n    public Object run() {\n        RequestContext ctx = RequestContext.getCurrentContext();\n        HttpServletRequest request = ctx.getRequest();\n        log.info(String.format(&quot;%s &gt;&gt;&gt; %s&quot;, request.getMethod(), request.getRequestURL().toString()));\n        Object accessToken = request.getParameter(&quot;token&quot;);\n        if(accessToken == null) {\n            log.warn(&quot;hep zuul caller token is empty&quot;);\n            ctx.setSendZuulResponse(false);\n            ctx.setResponseStatusCode(401);\n            try {\n                ctx.getResponse().getWriter().write(&quot;hep zuul caller token is empty&quot;);\n            }catch (Exception e){}\n\n            return null;\n        }\n        log.info(&quot;ok&quot;);\n        return null;\n    }\n}<\/code><\/pre>\n<p>filterType\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u4ee3\u8868\u8fc7\u6ee4\u5668\u7684\u7c7b\u578b\uff0c\u5728zuul\u4e2d\u5b9a\u4e49\u4e86\u56db\u79cd\u4e0d\u540c\u751f\u547d\u5468\u671f\u7684\u8fc7\u6ee4\u5668\u7c7b\u578b\uff0cpre\u8868\u793a\u8def\u7531\u4e4b\u524d\u3002<br \/>\n\u8fd9\u65f6\u8bbf\u95ee\uff1a<a href=\"http:\/\/localhost:8011\/hep-a\/getUserById\">http:\/\/localhost:8011\/hep-a\/getUserById<\/a> \u7f51\u9875\u663e\u793a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/03\/20\/image-20240320153518872.png\" alt=\"image-20240320153518872\" \/><\/p>\n<p>\u8bbf\u95ee\uff1a<a href=\"http:\/\/localhost:8011\/hep-a\/getUserById?token=PDIJPEIJ2308740HFE874Y69T43HHF77G36F6GF446FOQ83\">http:\/\/localhost:8011\/hep-a\/getUserById?token=PDIJPEIJ2308740HFE874Y69T43HHF77G36F6GF446FOQ83<\/a> \u7f51\u9875\u6b63\u5e38\u663e\u793a<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/03\/20\/image-20240320153739806.png\" alt=\"image-20240320153739806\" \/><\/p>\n<blockquote>\n<p>\u53c2\u8003\u6587\u7ae0https:\/\/forezp.blog.csdn.net\/article\/details\/81041012<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Spring Cloud(Finchley\u7248\u672c)\u7cfb\u5217\u6559\u7a0b(\u4e94) \u670d\u52a1\u7f51\u5173(Zuul) \u4e00\u3001Zuul\u7b80\u4ecb Zuu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-719","post","type-post","status-publish","format-standard","hentry","category-spring-cloudfinchley"],"_links":{"self":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=719"}],"version-history":[{"count":3,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/719\/revisions"}],"predecessor-version":[{"id":722,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/719\/revisions\/722"}],"wp:attachment":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}