Dubbo服务容错(整合hystrix)
简介:Hystrix旨在通过控制那些访问远程系统、服务和第三方库的节点从而对延迟和故障提供更强大的容错能力,Hystrix具备拥有回退机制和断路器功能的线程和信号隔离、请求缓存和请求打包以及监控和配置等功能。
1)、在pom文件中导入依赖(服务提供者和服务消费者都需要导入)
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 4 <version>1.4.4.RELEASE</version> 5 </dependency>
2)、在主程序启动类上添加@EnableHystrix注解开启服务容错(服务提供者和服务消费者都需要添加)
1 package cn.coreqi; 2 3 import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.cloud.netflix.hystrix.EnableHystrix; 7 8 @SpringBootApplication 9 @EnableDubbo 10 @EnableHystrix //开启服务容错 11 public class SpringbootdubboserviceproviderApplication { 12 13 public static void main(String[] args) { 14 SpringApplication.run(SpringbootdubboserviceproviderApplication.class, args); 15 } 16 17 }
3)、在服务提供者实现类中方法上添加@HystrixCommand注解
1 package cn.coreqi.service.impl; 2 3 import cn.coreqi.entities.User; 4 import cn.coreqi.service.UserService; 5 import com.alibaba.dubbo.config.annotation.Service; 6 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 7 import org.springframework.stereotype.Component; 8 9 import java.util.ArrayList; 10 import java.util.List; 11 12 @Component //org.springframework.stereotype.Component 13 @Service //com.alibaba.dubbo.config.annotation.Service 14 public class UserServiceImpl implements UserService { 15 16 private static List<User> users = new ArrayList<>(); 17 18 static { 19 users.add(new User(1,"fanqi","123456",1)); 20 users.add(new User(2,"zhangsan","123456",1)); 21 users.add(new User(3,"lisi","123456",1)); 22 users.add(new User(4,"wangwu","123456",1)); 23 } 24 25 @HystrixCommand 26 @Override 27 public void addUser(User user) { 28 users.add(user); 29 } 30 31 @HystrixCommand 32 @Override 33 public void delById(Integer id) { 34 for (User s:users){ 35 if(s.getId() == id){ 36 users.remove(s); 37 break; 38 } 39 } 40 } 41 42 @HystrixCommand 43 @Override 44 public void modifyUser(User user) { 45 delById(user.getId()); 46 addUser(user); 47 } 48 49 @HystrixCommand 50 @Override 51 public User getById(Integer id) { 52 for (User s:users){ 53 if(s.getId() == id){ 54 return s; 55 } 56 } 57 return null; 58 } 59 60 @HystrixCommand 61 @Override 62 public List<User> getList() { 63 return users; 64 } 65 }
4)、在服务消费者调用服务提供者的方法上添加@HystrixCommand注解并指定fallbackMethod属性,重写fallbackMethod指定的方法。
1 package cn.coreqi.controller; 2 3 import cn.coreqi.entities.User; 4 import cn.coreqi.service.UserService; 5 import com.alibaba.dubbo.config.annotation.Reference; 6 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.ResponseBody; 10 11 import java.util.List; 12 13 @Controller 14 public class UserController { 15 16 @Reference() 17 private UserService userService; 18 19 @HystrixCommand(fallbackMethod = "test1") 20 @ResponseBody 21 @RequestMapping("/users") 22 public List<User> getUsers(){ 23 return userService.getList(); 24 } 25 26 public List<User> test1(){ 27 return null; 28 } 29 }