Hystrix入门服务熔断降级
- Hystrix就是隔离措施的一种实现,可以设置在某种超时或者失败情形下断开依赖调用或者返回指定逻辑,从而提高分布式系统的稳定性.
比如:订单系统请求库存系统,结果一个请求过去,因为各种原因,网络超时,在规定几秒内没反应,或者服务本身就挂了,这时候更多的请求来了,不断的请求库存服务,不断的创建线程,因为没有返回,也就资源没有释放,这也导致了系统资源被耗尽,你的服务奔溃了,这订单系统好好的,你访问了一个可能有问题的库存系统,结果导致你的订单系统也奔溃了,你再继续调用更多的依赖服务,可会会导致更多的系统奔溃,这时候Hystrix可以实现快速失败。
- 本文描述客户端调用服务时,如果服务提供者宕机将使用hystrix进行自动降级。
- 代码地址https://github.com/Jacwo/eureka-server-hystrix 欢迎start
开始
- 使用idea新建一个maven项目 可以使用spring Initializr
- 替换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> <groupId>com.yyl</groupId> <artifactId>eureka-server-hystrix</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-server-hystrix</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>RELEASE</version> </dependency> <!--暴露各种指标--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--hystrix熔断器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
- 编写启动类
package com.yyl.hystrix; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /** * @author yangyuanliang */ @SpringBootApplication @EnableHystrix public class EurekaServerHystrixApplication { @Bean @LoadBalanced RestTemplate getRestTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(EurekaServerHystrixApplication.class, args); } }
- 编写controller
package com.yyl.hystrix.controller; import com.yyl.hystrix.service.RibbonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * author:yangyuanliang Date:2019-08-15 Time:11:00 **/ @RestController public class RibbonController { @Autowired private RibbonService ribbonService; @RequestMapping("/hystrix/test") public String testHystrix(){ return ribbonService.helloService(); } }
- 编写service
package com.yyl.hystrix.service; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; /** * author:yangyuanliang Date:2019-08-15 Time:11:00 **/ @Service public class RibbonService { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hystrixFallback") public String helloService(){ return restTemplate.getForEntity("http://hello-service/hello",String.class).getBody(); } public String hystrixFallback(){ return "error"; } }
- 编写application.properties
server.port=8088 spring.application.name=hystrix eureka.client.service-url.defaultZone=http://localhost:8672/eureka/,http://localhost:8673/eureka/
- 启动mvn spring-boot:run
- 启动注册中心参考https://blog.csdn.net/cccfire/article/details/99453834
- 启动生产者参考https://blog.csdn.net/cccfire/article/details/99455466
- 测试输入http://localhost:8088/hystrix/test 关闭生产者测试熔断