正文
1. 概述
上文
Spring cloud系列十 使用@HystrixCommand使用Hystrix组件及@EnableCircuitBreaker原理介绍
介绍了Hystrix的基本用法。我们已知Spring Cloud的@Feign已经集成了Hystrix的功能。本文我们介绍如何内容:
-
在Spring Cloud中,在Feign中开启Hystrix功能
-
为集成在Feign中的Hystrix进行个性化配置。
-
Feign集成Hystrix的原理介绍
2. 相关的工程说明
-
1 cloud-registration-center:注册中心
-
2 cloud-service-hystrix: 作为服务方的工程
-
3 cloud-consumer-hystrix:通过Fegin+Hystrix调用cloud-service-hystrix的接口
本节使用的工程和
Spring cloud系列十 使用@HystrixCommand使用Hystrix组件及@EnableCircuitBreaker原理介绍
相同
其中cloud-registration-center和cloud-service-hystrix完全相同,请参考这篇文章
3. cloud-consumer-hystrix
功能:此工程实现在Feign中开启Hystrix功能
3.1 pom.xml
关键jar
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
3.2. 配置属性文件
bootstrap-hystrix-feign.yml
# port
server:
port: 12082
spring:
application:
# 本服务注册到注册到服务器的名称, 这个名称就是后面调用服务时的服务标识符
name: cloud-consumer-hystrix
eureka:
client:
serviceUrl:
# 服务器注册/获取服务器的zone
defaultZone: http://127.0.0.1:10761/eureka/
instance:
prefer-ip-address: true
application-Hystrix-feign.yml
默认情况下,feign是不启动hystrix功能,需要我们启动此功能
# feign配置
feign:
hystrix:
# 在feign中开启hystrix功能,默认情况下feign不开启hystrix功能
enabled: true
3.3. IMyHystrixClient和MyHystrixClientFallbackFactory
IMyHystrixClient
通过@FeignClient定义Fegin方法,并定义name和fallbackFactory。name属性可用于定义Feign方法的Hystrix的个性化配置,而不使用全局默认配置。fallbackFactory设置带有异常信息的回调方法
@FeignClient(name="cloud-hystrix-service"
, fallbackFactory = MyHystrixClientFallbackFactory.class )
public interface IMyHystrixClient {
@RequestMapping(value = "/hystrix/simple", method = RequestMethod.POST, consumes="application/json; charset=UTF-8")
String simpleHystrixClientCall(@RequestParam("time") long time);
}
MyHystrixClientFallbackFactory
定义回调方法
@Component
public class MyHystrixClientFallbackFactory implements FallbackFactory<IMyHystrixClient> {
private static final Logger log = LoggerFactory.getLogger(MyHystrixClientFallbackFactory.class);
@Override
public IMyHystrixClient create(Throwable throwable) {
return new IMyHystrixClient() {
@Override
public String simpleHystrixClientCall(long time) {
log.error("异常处理={}", throwable);
return "Execute raw fallback: access service fail , req= " + time + " reason = " + throwable;
}
};
}
}
SimpleCtl
control层,定义访问URL接口
@RestController
public class SimpleCtl {
@Autowired
private IMyHystrixClient myHystrixClient;
@RequestMapping(value="/hystrix-feign/simple")
public String simpleClientCall(){
return "rsp: " + myHystrixClient.simpleHystrixClientCall(System.currentTimeMillis());
}
}
3.4. 为集成在Feign中的Hystrix进行个性化配置
以下配置hystrix的线程池的大小、是否开启回退方法。其他的属性配置见
Spring cloud系列九 Hystrix的配置属性优先级和详解
。这个例子里的配置为所有hystrix配置默认属性,如果需要为特定的Feign方法配置Hystrix配置个性化参数,只需要修改为default为客户端名称即可(这里值和@Feign中name相同)
application-hystrix-feign.yml
# 配置hystrix的参数
hystrix:
threadpool:
# default: 默认参数,作用的所有的hystrix的客户端
default:
coreSize: 10
command:
default:
fallback:
# 是否关闭回退方法
enabled: true
……
3.5. 启动类:HystrixFeignCloudConsumerApplication
@EnableFeignClients:启动Feign
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient // 配置本应用将使用服务注册和服务发现
public class HystrixFeignCloudConsumerApplication {
public static void main(String[] args) {
args = new String[1];
args[0] = "--spring.profiles.active=hystrix-feign";
SpringApplication.run(HystrixFeignCloudConsumerApplication.class, args);
}
/**
* 使用fastjson做为json的解析器
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
4. 测试
测试服务