简介
熔断器的原理如同电力过载保护器。它可以实现快速失败,如果它在一段时间内侦测到许多类似的错误,会强迫其以后的多个调用快速失败,不再访问远程服务器,从而防止应用程序不断地尝试执行可能会失败的操作,使得应用程序继续执行而不用等待修正错误,或者浪费CPU时间去等到长时间的超时产生。
dubbo-consumer、dubbo-provider分别作为服务的消费者和生产者,所有熔断相关配置是在消费者中配置。
Pom依赖如下,要注意springboot版本,不匹配会报错。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>customer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>2.6.1</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<!-- 使用springCloud集成的熔断器,针对单个方法熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.3.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>ccom.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.7.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在resources下面创建dubbo-customer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:consumer timeout="30000" >
</dubbo:consumer>
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="customer" />
<!-- dubbo接口去除注册中心,采用直连的方式 -->
<!-- <dubbo:registry address="N/A" ></dubbo:registry> -->
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />-->
<!-- 注册中心服务地址 -->
<dubbo:registry id="zookeeper" protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 接口位置,check="false" mock="return 123456"服务降级返回固定值,check必须设置false -->
<dubbo:reference interface="com.example.user.service.findUser" id="findService" />
<!--自定义mock业务处理类进行返回-->
<!-- <dubbo:reference interface="com.example.user.service.findUser" id="findService" check="false" mock="true"/>-->
</beans>
application.properties配置
server.port=8082
server.context-path=/test
//超时后熔断,指定超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000
在启动类上面引入
@ImportResource({"classpath:dubbo-customer.xml"})
@EnableCircuitBreaker//开启熔断器功能
创建测试类
import com.example.user.service.findUser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class testController {
@Autowired
findUser finduser;
//使用@HystrixCommand熔断注解,并制定回调方法
@HystrixCommand(fallbackMethod = "hiError")
@RequestMapping("/getuser")
public String sayHi() {
return finduser.findUser();
}
//熔断回调方法,方法返回值和入参必须要和熔断方法保持一致
public String hiError() {
return "Hystrix fallback";
}
}
启动访问http://localhost:8082/test/getuser
使用Hystrix好处
1 服务出现异常, 超时, 失败率达到阈值时能够自动走降级逻辑;
2 在服务熔断期间, 所有对该服务的请求, 全部走降级.
3 当服务恢复时, 后续的请求能够正常访问到该服务.
源码提取(zookeeper+dubbo+生产者+消费者)
链接:https://pan.baidu.com/s/1_Jm_J9a0ODiWwalG_HNLng
提取码:fnqt