简介: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. MT【12】三点坐标求面积

    $L_1,L_2$是O发出的两条射线,C是一个常数,一条动直线$l$分别与$L_1,L_2$交于A,B两点.$S_{\Delta ABC}=C$,求A,B的中点D的轨迹方程.(2012北大自主招生) ...

  2. NOIp2017D2T1(luogu3968) 奶酪 (并查集)

    并查集. 判相切或相交的时候可以两边同时平方,就不需要double和开根号了. #include<cstdio> #include<cstring> #include<a ...

  3. C# 推箱子游戏&对战游戏

    推箱子游戏提纲,只有向右向上的操作,向左向下同理,后期需完善. namespace 推箱子 { class Program { static void Main(string[] args) { // ...

  4. Red Hat 6.3安装gcc gc++

    首先安装gcc需要相应的rpm依赖包,在安装系统的镜像文件中就有这些rpm包 首先在光驱中选择系统的安装包载入 如果桌面显示有如下的光驱 说明是已经载入了镜像,这时候,需要挂载一下镜像到mnt目录 先 ...

  5. 解码(ByteBuffer): CharsetDecoder.decode() 与 Charset.decode() 的不同

    今天测试的时候发现一个问题: ByteBuffer inputBuffer = ByteBuffer.allocate(1024); StringBuilder inputData = new Str ...

  6. mysql.user表中Host为%的含义

    百度搜: MySQL之权限管理(mysql.user表详解) 连接:http://blog.csdn.net/zmx729618/article/details/78026497 mysql.user ...

  7. 走进JVM【二】理解JVM内存区域

    引言 对于C++程序员,内存分配与回收的处理一直是令人头疼的问题.Java由于自身的自动内存管理机制,使得管理内存变得非常轻松,不容易出现内存泄漏,溢出的问题. 不容易不代表不会出现问题,一旦内存泄漏 ...

  8. 也谈同步异步I/O

    也谈同步异步I/O [转自: http://www.smithfox.com/?e=191 ] I/O Model 是一个很大的话题, 也是一个实践性很强的事情, 网上有各种说法和资料, 我们必须用辩 ...

  9. ECharts图表引用json数据

    来讲两个图表,一个折线图,一个饼图. 先来看看效果图: 现在来看看代码,先来折线图,后台: (这里的后台太麻烦了,写的太多.可以使用Linq的方式,Linq比较简单写的也少.参考我的这篇文章的2018 ...

  10. IOS 获取系统通讯录中的联系人信息

    - (IBAction)getAllContactFromSystem { ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NUL ...