简介: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. 【BZOJ2671】Calc(莫比乌斯反演)

    [BZOJ2671]Calc 题面 BZOJ 给出N,统计满足下面条件的数对(a,b)的个数: 1.\(1\le a\lt b\le N\) 2.\(a+b\)整除\(a*b\) 我竟然粘了题面!!! ...

  2. [luogu3979][bzoj3083]遥远的国度

    [luogu传送门] [bzoj传送门] 题目描述 zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcww ...

  3. JS循环中使用bind函数的参数传递问题

    JS循环中使用bind函数的参数传递问题,问题代码如下: for (var sc in result) { var tempp = '<div class="sidebar_todo_ ...

  4. [poj3046][Ant counting数蚂蚁]

    题目链接 http://noi.openjudge.cn/ch0206/9289/ 描述 Bessie was poking around the ant hill one day watching ...

  5. git branch 分支管理

    在多人协作的情况下,master通常是稳定的分支.可以再建一些"develop","testing"等名称的分支.主管master的人做开发的话最好也建立自己的 ...

  6. 利用千人基因组数据库查看SNP在不同地区、国家、洲的频率及个数

    首先,进入千人基因组数据库的网站:https://www.ncbi.nlm.nih.gov/variation/tools/1000genomes/ 如下图所示,在数据库的框框里输入我们感兴趣的SNP ...

  7. 修改select下拉选的默认选中值

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. Stream基础知识

    Stream API Stream是Java8中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,但是将执行操作的时间交给具体实现来决定.例如,如果你希望计算某个方法的平均值,你可以在每个元素 ...

  9. 警告: No data sources are configured to run this SQL and provide advanced code assistance. Disable this inspection via problem menu (Alt+Enter). more... (Ctrl+F1) SQL dialect is not configured. Postgr

    python3出现问题: 警告: No data sources are configured to run this SQL and provide advanced code assistance ...

  10. 串口、COM口、TTL、RS-232、RS-485区别详解

    1.串口.COM口是指的物理接口形式(硬件).而TTL.RS-232.RS-485是指的电平标准(电信号).    2.接设备的时候,一般只接GND RX TX.不会接Vcc或者+3.3v的电源线,避 ...