简介:Hystrix旨在通过控制那些访问远程系统、服务和第三方库的节点从而对延迟和故障提供更强大的容错能力,Hystrix具备拥有回退机制和断路器功能的线程和信号隔离、请求缓存和请求打包以及监控和配置等功能。

1)、在pom文件中导入依赖(服务提供者和服务消费者都需要导入)

         <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>

2)、在主程序启动类上添加@EnableHystrix注解开启服务容错(服务提供者和服务消费者都需要添加)

 package cn.coreqi;

 import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix; @SpringBootApplication
@EnableDubbo
@EnableHystrix //开启服务容错
public class SpringbootdubboserviceproviderApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootdubboserviceproviderApplication.class, args);
} }

3)、在服务提供者实现类中方法上添加@HystrixCommand注解

 package cn.coreqi.service.impl;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import com.alibaba.dubbo.config.annotation.Service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Component; import java.util.ArrayList;
import java.util.List; @Component //org.springframework.stereotype.Component
@Service //com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService { private static List<User> users = new ArrayList<>(); static {
users.add(new User(1,"fanqi","123456",1));
users.add(new User(2,"zhangsan","123456",1));
users.add(new User(3,"lisi","123456",1));
users.add(new User(4,"wangwu","123456",1));
} @HystrixCommand
@Override
public void addUser(User user) {
users.add(user);
} @HystrixCommand
@Override
public void delById(Integer id) {
for (User s:users){
if(s.getId() == id){
users.remove(s);
break;
}
}
} @HystrixCommand
@Override
public void modifyUser(User user) {
delById(user.getId());
addUser(user);
} @HystrixCommand
@Override
public User getById(Integer id) {
for (User s:users){
if(s.getId() == id){
return s;
}
}
return null;
} @HystrixCommand
@Override
public List<User> getList() {
return users;
}
}

4)、在服务消费者调用服务提供者的方法上添加@HystrixCommand注解并指定fallbackMethod属性,重写fallbackMethod指定的方法。

 package cn.coreqi.controller;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller
public class UserController { @Reference()
private UserService userService; @HystrixCommand(fallbackMethod = "test1")
@ResponseBody
@RequestMapping("/users")
public List<User> getUsers(){
return userService.getList();
} public List<User> test1(){
return null;
}
}

Dubbo服务容错(整合hystrix)的更多相关文章

  1. 服务容错保护断路器Hystrix之三:断路器监控(Hystrix Dashboard)-单体监控

    turbine:英 [ˈtɜ:baɪn] 美 [ˈtɜ:rbaɪn] n.汽轮机;涡轮机;透平机 一.Hystrix Dashboard简介 在微服务架构中为了保证程序的可用性,防止程序出错导致网络阻 ...

  2. 服务容错保护断路器Hystrix之七:做到自动降级

    从<高可用服务设计之二:Rate limiting 限流与降级>中的“自动降级”中,我们这边将系统遇到“危险”时采取的整套应急方案和措施统一称为降级或服务降级.想要帮助服务做到自动降级,需 ...

  3. 服务容错保护断路器Hystrix之五:配置

    接着<服务容错保护断路器Hystrix之二:Hystrix工作流程解析>中的<2.8.关于配置>再列举重要的配置如下 一.hystrix在生产中的建议 1.保持timeout的 ...

  4. 服务容错保护断路器Hystrix之二:Hystrix工作流程解析

    一.总运行流程 当你发出请求后,hystrix是这么运行的 红圈 :Hystrix 命令执行失败,执行回退逻辑.也就是大家经常在文章中看到的“服务降级”. 绿圈 :四种情况会触发失败回退逻辑( fal ...

  5. Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】

    Spring Cloud(四):服务容错保护 Hystrix[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  分布式系统中经常会出现某个基础服务不可用 ...

  6. Dubbo 服务容错Hystrix

    一.服务者 1.pom <dependency> <groupId>org.springframework.cloud</groupId> <artifact ...

  7. 白话SpringCloud | 第五章:服务容错保护(Hystrix)

    前言 前一章节,我们知道了如何利用RestTemplate+Ribbon和Feign的方式进行服务的调用.在微服务架构中,一个服务可能会调用很多的其他微服务应用,虽然做了多集群部署,但可能还会存在诸如 ...

  8. Spring Cloud (8) 服务容错保护-Hystrix依赖隔离

    依赖隔离 docker使用舱壁模式来实现进程的隔离,使容器与容器之间不会互相影响.而Hystrix则使用该模式实现线程池的隔离,它会为每一个Hystrix命令创建一个独立的线程池,这样就算在某个Hys ...

  9. Dubbo服务容错

    当一个服务调用另一个远程服务出现错误时的外观 Dubbo提供了多种容错方案,默认值为failover(重试) 1).Failover Cluster(默认) 失败自动切换,当出现失败,重试其他服务器, ...

随机推荐

  1. luogu1072 [NOIp2009]Hankson的趣味题 (数学+STL::set)

    一个JSB做法 由$\frac{x*b0}{gcd(x,b0)}=b1$,可得$\frac{x}{gcd(x,b0)}=\frac{b1}{b0}$ 设$b2=\frac{b1}{b0}$ 所以对$b ...

  2. luogu2312 [NOIp2015]解方程 (秦九韶)

    秦九韶算法:多项式$a_0+a_1x+a_2x^2+...+a_nx^n=a_0+x(a_1+x(a_2+...+(xa_n))..)$,这样对于一个x,可以在O(n)求出结果 为了避免高精度,我们同 ...

  3. CAN总线疑惑与解答

    1    CAN总线2根数据线是怎么表示数据信息1和0的? Can总线采用差分数据表示方法,平时2个数据线为2.5V,表示隐性(1).当用数据0(显性)需要发送时1跟数据线上升到3.5V另一个下降到1 ...

  4. 字符串格式化format方法

    通过位置参数传参 print('{}, {}'.format('KeithTt', 18)) # KeithTt, 18 位置参数可以通过索引调用 print('{1}, {0}'.format('K ...

  5. 离线安装.NET 3.5

    最近为系统新增一个功能,写完以后进行部署,发现在IIS7上部署没有问题,但是IIS6上部署会出现未知情况,具体表现为取不到数据,估计是IIS6和IIS7直接的差异导致程序异常退出. 为了重现异常,在本 ...

  6. Linux中禁用命令历史记录

    关闭history记录功能 set +o history 打开history记录功能 set -o history 清空记录 history -c 记录被清空,重新登录后恢复. rm -f $HOME ...

  7. javascript面向对象精要第一章原始类型和引用类型整理精要

  8. 2018.10.19浪在ACM 集训队第一次测试赛

    2018.10.19浪在ACM 集训队第一次测试赛 待参考资料: [1]:https://blog.csdn.net/XLno_name/article/details/78559973?utm_so ...

  9. 在IIS6中配置html文件以ASPX方式工作

    在IIS6中配置html文件以ASPX方式工作 由于IIS6的安全不断提高,如果你需要设置html文件以ASPX文件方式被执行.仅仅设置应用程序映射是不够的,还 需要修改一些其他设置. 如果你只修改了 ...

  10. Django xadmin引入DjangoUeditor

    Django xadmin引入DjangoUeditor 版本:python3.6.1,Django1.11.1 DjangoUeditor下载地址:https://github.com/twz915 ...