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 }
作者:奇
出处:https://www.cnblogs.com/fanqisoft/p/10362989.html
版权:本作品采用「本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!