場景
SpringCloud-服務注冊與實作-Eureka創建服務注冊中心(附原始碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102535957
SpringCloud-服務注冊與實作-Eureka創建服務提供者(附原始碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558004
SpringCloud-創建服務消費者-Ribbon方式(附代碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558080
SpringCloud-創建服務消費者-Feign方式(附代碼下載)::
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102595895
在上面已經實作服務注冊中心、服務提供者和以Ribbon方式和Fign方式實作服務消費者的前提下,使用熔斷器防止服務雪崩,
在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以通過 RPC 相互呼叫,在 Spring Cloud 中可以用 RestTemplate + Ribbon 和 Feign 來呼叫,為了保證其高可用,單個服務通常會集群部署,由于網路原因或者自身的原因,服務并不能保證 100% 可用,如果單個服務出現問題,呼叫這個服務就會出現執行緒阻塞,此時若有大量的請求涌入,Servlet 容器的執行緒資源會被消耗完畢,導致服務癱瘓,服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重后果,這就是服務故障的 “雪崩” 效應,
熔斷器打開后,為了避免連鎖故障,通過 fallback 方法可以直接回傳一個固定值,
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載,
實作
Ribbon中使用熔斷器
SpringCloud-創建服務消費者-Ribbon方式(附代碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102558080
在上面使用Ribbon實作創建服務消費者,
我們在pom.xml中加入hystrix的依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>
完整pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.badao</groupId> <artifactId>hello-spring-cloud-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath> </parent> <artifactId>hello-spring-cloud-web-admin-ribbon</artifactId> <packaging>jar</packaging> <name>hello-spring-cloud-web-admin-ribbon</name> <url>https://blog.csdn.net/badao_liumang_qizhi</url> <inceptionYear>2019-Now</inceptionYear> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- Spring Cloud End --> <!-- 解決 thymeleaf 模板引擎一定要執行嚴格的 html5 格式校驗問題 --> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.funtl.hello.spring.cloud.web.admin.ribbon.WebAdminRibbonApplication</mainClass> </configuration> </plugin> </plugins> </build></project>
然后在應用啟動類Application中增加@EnableHystrix注解
package com.badao.hello.spring.cloud.web.admin.ribbon;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClient@EnableHystrixpublic class WebAdminRibbonApplication { public static void main(String[] args) { SpringApplication.run(WebAdminRibbonApplication.class, args); }}
然后在Service中添加@HystrixCommand注解
在 Ribbon 呼叫方法上增加 @HystrixCommand 注解并指定 fallbackMethod 熔斷方法,
package com.badao.hello.spring.cloud.web.admin.ribbon.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class AdminService { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiError") public String sayHi(String message) { return restTemplate.getForObject("http://hello-spring-cloud-service-admin/hi?message=" + message, String.class); } public String hiError(String message) { return "Hi,your message is :\"" + message + "\" but request error."; }}
測驗熔斷器
為了測驗熔斷器效果,我們將服務提供者關閉,此時再次請求:
http://localhost:8764/hi?message=HelloRibbon

Feign中使用熔斷器
Feign自帶熔斷器,所以不用添加依賴,只需要在組態檔中配置打開,
feign: hystrix: enabled: true

完整組態檔:
spring: application: name: hello-spring-cloud-web-admin-feign thymeleaf: cache: false mode: LEGACYHTML5 encoding: UTF-8 servlet: content-type: text/htmlserver: port: 8765eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/feign: hystrix: enabled: true
然后再Service中增加fallback指定類
package com.badao.hello.spring.cloud.web.feign.service;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;@FeignClient(value = "hello-spring-cloud-service-admin", fallback = AdminServiceHystrix.class)public interface AdminService { @RequestMapping(value = "hi", method = RequestMethod.GET) public String sayHi(@RequestParam(value = https://www.cnblogs.com/badaoliumangqizhi/p/"message") String message);}
此時再service包下創建熔斷器并實作對應的Feign介面
package com.badao.hello.spring.cloud.web.feign.service;import org.springframework.stereotype.Component;@Componentpublic class AdminServiceHystrix implements AdminService { @Override public String sayHi(String message) { return "Hi,your message is :\"" + message + "\" but request error."; }}
然后將服務提供者關閉,再次請求:
http://localhost:8765/hi?message=HelloFeign

代碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11871136
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/44287.html
標籤:架構設計
上一篇:在 Java 中如何比較日期?
下一篇:SVN中怎樣忽略當前檔案不提交
