Dubbo整合hystrix
整合hystrix
Dubbo提供了,集群容错机制
可以通过配置,进行使用
在实际开发中
一般,都是通过整合hystrix,进行集群容错
Hystrix
是Spring cloud中,默认整合的服务容错解决方案
通过,控制那些访问远程系统、服务和第三方库的节点
从而,对延迟和故障提供更强大的容错能力
Hystrix
具备拥有回退机制和断路器功能的线程和信号隔离
请求缓存和请求打包,以及监控和配置等功能
使用方式
服务提供者、消费者
导入Hystrix场景启动器
配置spring-cloud-starter-netflix-hystrix
spring boot官方提供了对hystrix的集成
可以直接在pom.xml里加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
启用Hystrix
通过注解,启动Hystrix功能
在Application类上增加@EnableHystrix
启用hystrix starter
@EnableHystrix //开启服务容错
@SpringBootApplication
public class BootUserServiceProviderApplication {
public static void main(String[] args) {
服务提供者
在Dubbo的Provider上,增加@HystrixCommand配置
这样,调用就会经过Hystrix代理
在方法上,通过@HystrixCommand注解
开启Hystrix异常
@Service//暴露服务
@Component
public class UserServiceImpl implements UserService {
@HystrixCommand
@Override
public List<UserAddress> getUserAddressList(String userId) {
服务消费者
在Dubbo的Provider上,增加@HystrixCommand配置
这样,调用就会经过Hystrix代理
在方法上,通过@HystrixCommand注解
开启Hystrix异常
通过fallbackMethod属性
配置发生异常,执行的方法
@Service
public class OrderServiceImpl implements OrderService {
//@Autowired
@Reference(loadbalance="roundrobin")
UserService userService;
@HystrixCommand(fallbackMethod="hello")
@Override
public List<UserAddress> initOrder(String userId) {
// TODO Auto-generated method stub
System.out.println("用户id:"+userId);
//1、查询用户的收货地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
return addressList;
}
public List<UserAddress> hello(String userId) {
// TODO Auto-generated method stub
return Arrays.asList(new UserAddress(10, "测试地址", "1", "测试", "测试", "Y"));
}
}
还没有评论,来说两句吧...