一、pom依赖引入
如需显示hystrix-dashbord,必须引入的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>${stater.feign.version}</version>
</dependency>
hystrix核心依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.3RELEASE</version>
</dependency>
二、yml配置
#熔断、降级服务配置
feign:
hystrix:
enabled: true
如需支持hystrix-dashboard必须配置项
hystrix:
dashboard:
proxy-stream-allow-list: "*"
threadpool:
default:
coreSize: 200 #并发执行的最大线程数,默认10
maxQueueSize: 1000 #BlockingQueue的最大队列数,默认值-1
queueSizeRejectionThreshold: 800 #即使maxQueueSize没有达到,达到
queueSizeRejectionThreshold该值后,请求也会被拒绝,默认值5
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000 #请求超时配置
三、启动类需添加代码
@EnableEurekaClient
@SpringBootApplication
@EnableCircuitBreaker //熔断、降级
@EnableHystrixDashboard //hystrix-dashboard
@EnableFeignClients //与feign连用
public class CheckPersonServerAPP {
public static void main(String[] args) {
SpringApplication.run(CheckPersonServerAPP.class, args);
}
//hystrix-dashboard配置
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
//负载均衡
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
四、另外需要的java文件
1、与feign联用时,调用系统外部接口
@FeignClient(name = "huayunapi",url = "http://xxxx:8888",fallback = HystrickFallBack.class)
public interface RemoteApi {
/***
* 调用外部接口,get请求、参数map、返回值string,与外部接口都得对应
* @param map
* @return
*/
@RequestMapping(value = "/api/hello",consumes = "application/json;charset=UTF-8",method = RequestMethod.GET)
String remoteApi (@RequestParam Map map);
}
对应外部接口:
@RestController
@RequestMapping("api")
public class SCGAWController {
@RequestMapping(value = "hello",method = RequestMethod.GET)
public String remote(@RequestParam Map map){
//正常业务逻辑
return "success";
}
}
对应降级方法类,必须继承@FeignClient注解的接口类,并实现接口方法:
@Component
public class HystrickFallBack implements RemoteApi {
@Override
public String remote(Map map){
//异常业务逻辑,即正常业务逻辑在异常之后,会执行该方法,也叫作降级服务方法
System.out.println("正常逻辑出现异常,异常方法执行");
return "error";
}
}
2、单独使用时,有多种方式,这里只用其一
@Service
public class Service {
@HystrixCommand(fallbackMethod = "failMethod")
public String imgSave(String id, String str) {
//正常业务逻辑
int i = 1/0;
}
public String failMethod(String id, String str) {
//异常业务逻辑,即正常业务逻辑在异常之后,会执行该方法,也叫作降级服务方法
System.out.println("正常逻辑出现异常,异常方法执行");
}
}
注意上面代码中:@HystrixCommand(fallbackMethod = "failMethod"),一定要有failMethod的方法存在,且imgSave方法不能再改类里面被其他方法调用,负责failMethod失效,fallbackMethod属性值是自定义的。
这里举一个实例运用场景:现有一个任务向第三方库存储数据,如果向第三方库存储失败了,则存向本地。
存储到第三方库为正常逻辑,当失败了或者超时了,就去执行异常逻辑,存向本地。
在系统中,当有请求向外部的时候,如果外部接口没有及时响应,这边会一直等待响应,要么失败,要么超时,总之时间久、耗费大,这时,当有成千上万的请求通过本地服务请求向外部服务的时候,很大程度会造成本地服务宕机,这样会导致其他调用本地的服务也跟着宕机,引起的一系列现象称之为,雪崩效应。
hystrix 主要是熔断、降级的作用和阿里的Sentinel类似作用。当请求出现问题的时候,会打开熔断开关,直接执行异常逻辑,避免东窗事件再此发生。当检测到外部请求可以访问时,就关闭熔断开关,执行正常逻辑。
正常状态下,电路处于关闭状态(Closed),如果调用持续出错或者超时,电路被打开进入熔断状态(Open),后续一段时间内的所有调用都会被拒绝(Fail Fast),一段时间以后,保护器会尝试进入半熔断状态(Half-Open),允许少量请求进来尝试,如果调用仍然失败,则回到熔断状态,如果调用成功,则回到电路闭合状态;
大概的用法和为什么用就暂时絮到这,后期空了再补上原理。
五、hystrix-dashboard
项目端口为:8999,所以默认url:http://localhost:8999/hystrix
在dashboard里面填写url为:http://localhost:8999/hystrix.stream
点击Monnitor Stream按钮后进入界面:
会在控制台打印:Proxy opening connection to: http://localhost:8999/hystrix.stream
但是hystrix只支持单个应用服务的接口监控,如需多个服务监控,可以配置Turbine,后期会补充关于Turbine的使用及原理