基于Dubbo SPI引入Hystrix。提供如下功能:
- rest服务接口生成监控数据
- 服务消费方业务代码零改动具有熔断、限流功能
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo-sf.version}</version>
</dependency>
3. 下载hystrix-dashboard并解压至tomcat容器ROOT目录下启动
4. 打开本地tomcat并添加Hystrix地址http://127.0.0.1:8888/hystrix.stream
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo-sf.version}</version>
</dependency>
<dubbo:reference check="false" id="demoUserRestService"
interface="com.sf.test.service.DemoUserRestService" registry="regZK2">
<dubbo:parameter key="hystrix" value="true"/> <!-- (1) -->
<dubbo:method name="selectByPrimaryKey">
<dubbo:parameter key="hystrix" value="false"/> <!-- (2) -->
</dubbo:method>
</dubbo:reference>
注: 上边的(1)为demoUserRestService接口所有方法启用hystrix熔断、限流功能; 上边的(2)为关闭demoUserRestService接口selectByPrimaryKey方法的hystrix熔断、限流功能; 合理配置dubbo:parameter即可,true为打开,其它为关闭。
package com.sf.test.service.conf;
import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
import com.netflix.config.ConfigurationManager;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
/**
* hystrix配置
*
* 在disconf统一配置中心后台改动会自动重新加载最新配置
*
* @author YANGLiiN 2017-06-27 10:41
*/
@Component
@DisconfFile(filename = HystrixConfig.HYSTRIX_CONFIG_NAME)
@DisconfUpdateService(classes = {HystrixConfig.class})
public class HystrixConfig implements InitializingBean, IDisconfUpdate {
private static final Logger log = LoggerFactory.getLogger(HystrixConfig.class);
public static final String HYSTRIX_CONFIG_NAME = "hystrix.properties";
@Override
public void afterPropertiesSet() throws Exception {
loadProperties();
}
@Override
public void reload() throws Exception {
loadProperties();
}
private void loadProperties() {
try {
ConfigurationManager.getConfigInstance().clear();
ConfigurationManager.loadPropertiesFromResources(HYSTRIX_CONFIG_NAME);
} catch (IOException e) {
log.error("加载hystrix.properties配置发错", e);
}
}
}
##################默认配置项##################
### 线程池大小
hystrix.threadpool.default.coreSize=200
### 是否强制打开熔断器,默认关闭
hystrix.command.default.circuitBreaker.forceOpen=false
### 触发熔断后,拒绝请求后多长时间开始尝试再次执行,默认5000毫秒
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=10000
### 触发熔断的错误比例。默认50,即50%
hystrix.command.default.circuitBreaker.errorThresholdPercentage=10
### 超时时间,默认1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000
### 是否开启超时,默认true
hystrix.command.default.execution.timeout.enabled=false
### 是否开启熔断,默认true
hystrix.command.default.circuitBreaker.enabled=true
### 是否开启fallback,默认true
hystrix.command.default.fallback.enabled=true
##################非default配置项##################
hystrix.command.demoUserRestService_selectAll.circuitBreaker.forceOpen=false
hystrix.command.demoUserRestService_selectByPrimaryKey.circuitBreaker.forceOpen=false