SpringCloud-使用熔断器防止服务雪崩-Ribbon和Feign方式(附代码下载)
场景
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 方法可以直接返回一个固定值。
注:
博客:
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
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 @enablehystrix public 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; @service public 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/html server: port: 8765 eureka: 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 = "message") string message); }
此时再service包下创建熔断器并实现对应的feign接口
package com.badao.hello.spring.cloud.web.feign.service; import org.springframework.stereotype.component; @component public 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
代码下载