断路器Hystrix
- 1. 概述
- 2. Ribbon的断路器
-
- 2.1 引入包
- 2.2 启动类引入hystrix
- 2.3 服务调用类配置
- 3. Feign的断路器
-
- 3.1 启用配置
- 3.2 feign接口配置
- 3.3 后备类
- 3.4 配置说明
1. 概述
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。为了解决这个问题,spring cloud提供断路器模型hystrix。
Spring cloud的hystrix有3种模式的断路器:
1) 断路器模式:远程服务不可用时,快速失败。
2) 后备模式:远程服务不可用时,使用替代方案。
3) 舱壁模式:不同远程服务使用不同线程池,不会导致一个远程服务不可用,整个服务挂掉。
2. Ribbon的断路器
2.1 引入包
利用前面service-ribbon工程《服务消费方Ribbon》中,在pom中引入hystrix的jar。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.2 启动类引入hystrix
在启动类ServiceRibbonApplication.java中,引入注解@EnableHystrix即可。下面代码红色标记处。
package org.service.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceRibbonApplication.class, args );
HelloService s = new HelloService();
System.out.println(s.hiService("linwu"));
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
2.3 服务调用类配置
服务调用类中,在调用方法上面增加注解@HystrixCommand,可以配置包括后备模式、舱壁模式等属性,下面代码使用一个后备模式(属性fallbackMethod,配置后备模式的方法,方面入参和出参要与服务调用一样。本例子中后备方法为:hiError):
package org.service.ribbon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
public String hiError(String name) {
return "hi,"+name+",sorry,error!" +";this feign!";
}
}
3. Feign的断路器
3.1 启用配置
Feign是自带断路器的,在D版本的Spring Cloud之后,它没有默认打开。需要在配置文件中配置打开它。利用前面service-feign工程《服务消费方Feign》在配置文件application.yml加以下代码:
feign:
hystrix:
enabled: true //feign开启hystrix支持
3.2 feign接口配置
在接口的注解上面增加fallback属性即可。Fallback属性对应一个java类,此类实现feign接口。代码如下:
package org.service.feign;
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 = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
3.3 后备类
编写一个后备类SchedualServiceHiHystric.java。实现feign接口。代码如下:
package org.service.feign;
import org.springframework.stereotype.Component;
@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
@Override
public String sayHiFromClientOne(String name) {
return "sorry "+name +";this feign!";
}
}
3.4 配置说明
1) 使用commandProperties属性可以配置断路器的属性(超时、次数等)
2) 后备模式可以使用fallback的配置。
3) 舱壁模式使用threadPoolKey和threadPoolProperties属性配置线程池。
4) 使用commandPoolProperties配置线程池的断路属性。
本文参考文献:https://blog.csdn.net/forezp/article/details/70148833/