一、熔断器简介
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以通过 RPC 相互调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证 100% 可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet 容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的 “雪崩” 效应。
二、如何使用熔断器?
Dubbo Provider 中使用熔断器
【1】在 pom.xml 中增加如下依赖。
<!--添加熔断器的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
【2】在 Application 中增加 @EnableHystrix 注解。
package com.czd.hellodubboserviceuserprovider;
import com.alibaba.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
//开启熔断器功能
@EnableHystrix
@SpringBootApplication
public class HelloDubboServiceUserProviderApplication {
public static void main(String[] args) {
SpringApplication.run(HelloDubboServiceUserProviderApplication.class, args);
// 启动 Provider 容器,注意这里的 Main 是 com.alibaba.dubbo.container 包下的
Main.main(args);
}
}
【3】在 ServiceImpl 中增加 @HystrixCommand 注解。
package com.czd.hellodubboserviceuserprovider.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.czd.hello.dubbo.service.user.api.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
/**
* @author czd
*/
@Service(version = "${user.service.version}")
public class UserServiceImpl implements UserService {
@Value("${dubbo.protocol.port}")
private String port;
// @HystrixCommand(commandProperties = {
// //断路器熔断的最小请求数为10次,默认是20次
// @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
// //执行的超时时间为2秒,默认为5秒
// @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
// })
@HystrixCommand
@Override
public String sayHello() {
// return "Hello Dubbo the port is " + port;
throw new RuntimeException("Exception to show hystrix enabled.");
}
}
Dubbo Consumer 中使用熔断器
【1】在 pom.xml 中增加如下依赖。
<!--添加熔断器的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
【2】在 Application 中增加 @EnableHystrix 注解。
package com.czd.hellodubboserviceuserconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
//开启熔断器功能
@EnableHystrix
@SpringBootApplication
public class HelloDubboServiceUserConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(HelloDubboServiceUserConsumerApplication.class, args);
}
}
【3】在调用方法上增加 @HystrixCommand 注解,并指定 fallbackMethod 方法。
package com.czd.hellodubboserviceuserconsumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.czd.hello.dubbo.service.user.api.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author czd
*/
@RestController
public class UserServiceController {
@Reference(version = "${user.service.version}")
private UserService userService;
@HystrixCommand(fallbackMethod = "hystrix")
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello() {
return userService.sayHello();
}
/**
* 熔断器触发的fallbackMethod方法
* @return
*/
public String hystrix() {
return "Hystrix fallback";
}
}
三、测试熔断器
- 此时我们再次请求服务提供者(Dubbo Provider),控制台出现
Exception to show hystrix enabled.
- 此时我们再次请求服务提供者,浏览器会显示:
Hystrix fallback
四、配置熔断器仪表盘
使用熔断器仪表盘监控
【1】在 pom.xml 中增加依赖。
<!--添加熔断器仪表盘依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
【2】在 Application 中增加 @EnableHystrixDashboard 注解。
package com.czd.hellodubboserviceuserconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
//开启熔断器功能、熔断器仪表盘功能
@EnableHystrix
@EnableHystrixDashboard
@SpringBootApplication
public class HelloDubboServiceUserConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(HelloDubboServiceUserConsumerApplication.class, args);
}
}
【3】创建 hystrix.stream 的 Servlet 配置(即创建一个HystrixDashboardConfiguration类),我的是创建一个config包来存放HystrixDashboardConfiguration类,代码如下所示。
package com.czd.hellodubboserviceuserconsumer.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author czd
* Spring Boot 2.x 版本开启 Hystrix Dashboard 与 Spring Boot 1.x 的方式略有不同
* 需要增加一个 HystrixMetricsStreamServlet 的配置
*/
@Configuration
public class HystrixDashboardConfiguration {
@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;
}
}
五、测试 Hystrix Dashboard
步骤一:打开网址:http://localhost:9090/hystrix,出现如下界面。
步骤二:在链接框中输入http://localhost:9090/hystrix.stream
,出现如下界面。
注意:在 Provider 和 Consumer 项目增加 Hystrix 仪表盘功能,两个项目的改造方式相同。