{"id":716,"date":"2024-02-11T11:10:33","date_gmt":"2024-02-11T03:10:33","guid":{"rendered":"http:\/\/www.huerpu.cc:7000\/?p=716"},"modified":"2024-02-11T11:10:33","modified_gmt":"2024-02-11T03:10:33","slug":"spring-cloudfinchley%e7%89%88%e6%9c%ac%e7%b3%bb%e5%88%97%e6%95%99%e7%a8%8b%e5%9b%9b-%e6%96%ad%e8%b7%af%e5%99%a8hystrix","status":"publish","type":"post","link":"http:\/\/www.huerpu.cc:7000\/?p=716","title":{"rendered":"Spring Cloud(Finchley\u7248\u672c)\u7cfb\u5217\u6559\u7a0b(\u56db) \u65ad\u8def\u5668(Hystrix)"},"content":{"rendered":"<h3>Spring Cloud(Finchley\u7248\u672c)\u7cfb\u5217\u6559\u7a0b(\u56db) \u65ad\u8def\u5668(Hystrix)<\/h3>\n<h3>\u4e00\u3001\u65ad\u8def\u5668Hystrix<\/h3>\n<p><code>hystrix<\/code>\u662f<code>Netlifx<\/code>\u5f00\u6e90\u7684\u4e00\u6b3e\u5bb9\u9519\u6846\u67b6\uff0c\u9632\u96ea\u5d29\u5229\u5668\uff0c\u5177\u5907\u670d\u52a1\u964d\u7ea7\uff0c\u670d\u52a1\u7194\u65ad\uff0c\u4f9d\u8d56\u9694\u79bb\uff0c\u76d1\u63a7(Hystrix Dashboard)\u7b49\u529f\u80fd\u3002hystrix\u662f\u4e00\u4e2a\u5e93\uff0c\u901a\u8fc7\u5ef6\u8fdf\u5bb9\u5fcd\u548c\u5bb9\u9519\u903b\u8f91\uff0c\u63a7\u5236\u5206\u5e03\u5f0f\u670d\u52a1\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\u5b83\u901a\u8fc7\u9694\u79bb\u670d\u52a1\u95f4\u7684\u8bbf\u95ee\u70b9\u3001\u9632\u6b62\u7ea7\u8054\u5931\u8d25\u548c\u63d0\u4f9b\u56de\u9000\u9009\u9879\uff0c\u4fdd\u8bc1\u7cfb\u7edf\u7684\u6574\u4f53\u5f39\u6027\u3002<\/p>\n<h3>\u4e8c\u3001\u5728Ribbon\u4e2d\u4f7f\u7528\u65ad\u8def\u5668<\/h3>\n<p>\u590d\u5236<code>eurekaClientConsumer<\/code>\u9879\u76ee\uff0c\u91cd\u547d\u540d\u4e3a<code>hepServiceRibbonHystrix<\/code>\uff0c\u4fee\u6539<code>artifactId<\/code>\u3001<code>name<\/code>\u3001<code>description<\/code>\u4e3a<code>hepServiceRibbonHystrix<\/code>\uff0c\u5e76\u589e\u52a0<code>hystrix<\/code>\u4f9d\u8d56\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;artifactId&gt;hepServiceRibbonHystrix&lt;\/artifactId&gt;\n&lt;name&gt;hepServiceRibbonHystrix&lt;\/name&gt; \n&lt;description&gt;hepServiceRibbonHystrix&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-hystrix&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4fee\u6539<code>EurekaClientConsumerApplication<\/code>\u542f\u52a8\u7c7b\u4e3a<code>HepServiceRibbonHystrix<\/code>\uff0c\u5e76\u5728\u7a0b\u5e8f\u7684\u542f\u52a8\u7c7b<code>HepServiceRibbonHystrix<\/code>\u52a0<code>@EnableHystrix<\/code>\u6ce8\u89e3\u5f00\u542f<code>Hystrix<\/code>\u3002<\/p>\n<pre><code class=\"language-java\">package cc.huerpu.eurekaserver;\n\nimport com.netflix.loadbalancer.IRule;\nimport com.netflix.loadbalancer.RandomRule;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\nimport org.springframework.cloud.client.loadbalancer.LoadBalanced;\nimport org.springframework.cloud.netflix.hystrix.EnableHystrix;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.web.client.RestTemplate;\n\n@SpringBootApplication\n@EnableDiscoveryClient\n@EnableHystrix\npublic class HepServiceRibbonHystrix {\n    public static void main(String[] args) {\n        SpringApplication.run(HepServiceRibbonHystrix.class, args);\n    }\n    @Bean\n    public IRule ribbonRule() {\n        \/\/ \u8d1f\u8f7d\u5747\u8861\u89c4\u5219\uff0c\u6539\u4e3a\u968f\u673a\n        return new RandomRule();\n    }\n    @Bean\n    @LoadBalanced\n    public RestTemplate restTemplate() {\n        return new RestTemplate();\n    }\n}<\/code><\/pre>\n<p>\u4fee\u6539\u539f\u6765<code>ConsumerController<\/code>\u7c7b\u4e2d\u7684\u6d88\u8d39\u63a5\u53e3\uff0c\u589e\u52a0<code>@HystrixCommand<\/code>\u6ce8\u89e3\uff0c\u5e76\u6307\u5b9a\u56de\u8c03\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">@RequestMapping(&quot;\/consumerEurekaClient&quot;)\n@HystrixCommand(fallbackMethod = &quot;consumerEurekaClientErrorHandler&quot;)\npublic String consumerEurekaClient(){\n    String res = restTemplate.getForObject(&quot;http:\/\/eurekaClient\/getUserById&quot;,String.class);\n    System.out.println(res);\n    return &quot;consumerEurekaClient:&quot; + res;\n}\n\npublic String consumerEurekaClientErrorHandler() {\n    return &quot;consumerEurekaClientErrorHandler:sorry,EurekaClient cannot handle this request,please try later!&quot;;\n}<\/code><\/pre>\n<p>\u4fee\u6539\u9879\u76ee\u7aef\u53e3\u53f7\u4e3a8005\u3002<\/p>\n<p>\u6d4f\u89c8\u5668\u8bbf\u95ee<code>http:\/\/localhost:8005\/consumerEurekaClient<\/code>\uff0c\u6b64\u65f6<code>http:\/\/eurekaClient\/getUserById<\/code>\u63a5\u53e3\u662f\u505c\u6389\u7684\uff0c\u4e5f\u5c31\u6210\u529f\u8fd4\u56de\u4e86<code>consumerEurekaClientErrorHandler<\/code>\u56de\u8c03\u51fd\u6570\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/02\/08\/image-20240208155557210.png\" alt=\"image-20240208155557210\" \/><\/p>\n<h3>\u4e09\u3001\u5728OpenFeign\u4e2d\u4f7f\u7528\u65ad\u8def\u5668<\/h3>\n<p>\u590d\u5236\u9879\u76ee<code>hepServiceOpenFeign<\/code>\uff0c\u91cd\u547d\u540d\u4e3a<code>hepServiceOpenFeignHystrix<\/code>\uff0c\u4fee\u6539<code>artifactId<\/code>\u3001<code>name<\/code>\u3001<code>description<\/code>\u4e3a<code>hepServiceOpenFeignHystrix<\/code><\/p>\n<pre><code class=\"language-xml\">&lt;artifactId&gt;hepServiceOpenFeignHystrix&lt;\/artifactId&gt;\n&lt;name&gt;hepServiceOpenFeignHystrix&lt;\/name&gt;\n&lt;description&gt;hepServiceOpenFeignHystrix&lt;\/description&gt;\n\n&lt;!-- \u5e76\u589e\u52a0web\u4f9d\u8d56 --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4fee\u6539\u9879\u76ee\u540d\u79f0\u4e3aHepServiceOpenFeignHystrixApplication\u3002<\/p>\n<pre><code class=\"language-java\">package cc.huerpu.eurekaserver;\n\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\nimport org.springframework.cloud.openfeign.EnableFeignClients;\n\n@SpringBootApplication\n@EnableDiscoveryClient\n@EnableFeignClients\npublic class HepServiceOpenFeignHystrixApplication {\n    public static void main(String[] args) {\n        SpringApplication.run(HepServiceOpenFeignHystrixApplication.class, args);\n    }\n}<\/code><\/pre>\n<p>\u5728<code>UserService<\/code>\u63a5\u53e3\u4e0a\u589e\u52a0<code>fallback = OpenFeignHytrix.class<\/code>\uff0c<code>OpenFeignHytrix<\/code>\u5b9e\u73b0<code>UserService<\/code>\u63a5\u53e3\uff0c\u7528\u4e8e\u5904\u7406\u8c03\u7528\u5931\u8d25\u7684\u56de\u8c03\u3002<\/p>\n<pre><code class=\"language-java\">package cc.huerpu.eurekaserver.feign;\n\nimport cc.huerpu.eurekaserver.config.OpenFeignConfig;\nimport cc.huerpu.eurekaserver.config.OpenFeignHytrix;\nimport org.springframework.cloud.openfeign.FeignClient;\nimport org.springframework.web.bind.annotation.RequestMapping;\n\n@FeignClient(value = &quot;eurekaClient&quot;,configuration = OpenFeignConfig.class,fallback = OpenFeignHytrix.class)\npublic interface UserService {\n    @RequestMapping(value = &quot;\/getUserById&quot;)\n    String getUserById();\n}<\/code><\/pre>\n<p>\u5b9a\u4e49\u4e00\u4e2a<code>OpenFeignHytrix<\/code>\uff0c\u5b9e\u73b0<code>UserService<\/code>\uff0c\u5e76\u8ba9\u5b83\u6ce8\u5165\u5230\u5bb9\u5668\u4e2d\u3002<\/p>\n<pre><code class=\"language-java\">package cc.huerpu.eurekaserver.config;\n\nimport cc.huerpu.eurekaserver.feign.UserService;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class OpenFeignHytrix implements UserService {\n    @Override\n    public String getUserById(){\n        return &quot;Sorry  call getUserById interface error, OpenFeignHytrix recommend you try later &quot; ;\n    }\n}<\/code><\/pre>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u6253\u5f00<code>hystrix<\/code><\/p>\n<pre><code class=\"language-yml\">feign:\n  hystrix:\n    enabled: true<\/code><\/pre>\n<p>\u4fee\u6539\u9879\u76ee\u7aef\u53e3\u53f7\u4e3a8009\uff0c\u91cd\u542f\u9879\u76ee\uff0c\u8c03\u7528<code>http:\/\/localhost:8009\/getUserByIdFeign<\/code>\uff0c\u6b64\u65f6\u53ea\u542f\u52a8\u4e86<code>HepServiceOpenFeignHystrixApplication<\/code>\u4e00\u4e2a\u9879\u76ee\uff0c\u5176\u4f59\u9879\u76ee\u90fd\u6ca1\u8fd0\u884c\uff0c\u56e0\u6b64\u89e6\u53d1\u4e86<code>Hystrix<\/code>\u7194\u65ad<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/02\/08\/image-20240208164536594.png\" alt=\"image-20240208164536594\" \/><\/p>\n<h3>\u56db\u3001\u6574\u5408Dashboard\u67e5\u770b\u76d1\u63a7\u6570\u636e<\/h3>\n<p>\u5728\u9879\u76ee<code>hepServiceRibbonHystrix<\/code>\u4e2d\u6dfb\u52a0<code>hystrix-dashboar<\/code>\u4f9d\u8d56 \uff0c\u5e76\u786e\u4fdd\u6709<code>actuator<\/code>\u5065\u5eb7\u68c0\u67e5\u4f9d\u8d56\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-netflix-hystrix-dashboard&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-boot-starter-actuator&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u5728\u542f\u52a8\u7c7b\u4e0a\u589e\u52a0<code>@EnableHystrixDashboard<\/code>\u6ce8\u89e3\u3002<\/p>\n<pre><code class=\"language-java\">package cc.huerpu.eurekaserver;\n\nimport com.netflix.loadbalancer.IRule;\nimport com.netflix.loadbalancer.RandomRule;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\nimport org.springframework.cloud.client.loadbalancer.LoadBalanced;\nimport org.springframework.cloud.netflix.hystrix.EnableHystrix;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.web.client.RestTemplate;\nimport org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;\n@SpringBootApplication\n@EnableDiscoveryClient\n@EnableHystrix\n@EnableHystrixDashboard\npublic class HepServiceRibbonHystrix {\n    public static void main(String[] args) {\n        SpringApplication.run(HepServiceRibbonHystrix.class, args);\n    }\n    @Bean\n    public IRule ribbonRule() {\n        \/\/ \u8d1f\u8f7d\u5747\u8861\u89c4\u5219\uff0c\u6539\u4e3a\u968f\u673a\n        return new RandomRule();\n    }\n    @Bean\n    @LoadBalanced\n    public RestTemplate restTemplate() {\n        return new RestTemplate();\n    }\n}<\/code><\/pre>\n<p>\u9996\u5148\u8c03\u7528\u4e00\u4e0b<code>http:\/\/localhost:8005\/consumerEurekaClient<\/code>\uff0c\u7136\u540e\u8c03\u7528<code>http:\/\/localhost:8005\/actuator\/hystrix.stream<\/code>\uff0c\u53ef\u4ee5\u770b\u5230ping\u7684\u4fe1\u606f<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/02\/11\/image-20240211110633145.png\" alt=\"image-20240211110633145\" \/><\/p>\n<p>\u8f93\u5165<code>http:\/\/localhost:8005\/hystrix\/<\/code>\uff0c\u628a\u521a\u521a\u7684\u5730\u5740<code>http:\/\/localhost:8005\/actuator\/hystrix.stream<\/code>\u8d34\u8fdb\u6765\u3002<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/02\/11\/image-20240211110604556.png\" alt=\"image-20240211110604556\" \/><\/p>\n<p>\u70b9\u51fb\u8fdb\u53bb\uff0c\u53ef\u4ee5\u770b\u5230<code>dashboard<\/code>\u4fe1\u606f\u4e86<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/02\/11\/image-20240211110901642.png\" alt=\"image-20240211110901642\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Cloud(Finchley\u7248\u672c)\u7cfb\u5217\u6559\u7a0b(\u56db) \u65ad\u8def\u5668(Hystrix) \u4e00\u3001\u65ad\u8def\u5668Hystr [&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-716","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\/716","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=716"}],"version-history":[{"count":1,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions"}],"predecessor-version":[{"id":717,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions\/717"}],"wp:attachment":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}