Dubbo服务容错(整合hystrix)
简介: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)的更多相关文章
- 服务容错保护断路器Hystrix之三:断路器监控(Hystrix Dashboard)-单体监控
turbine:英 [ˈtɜ:baɪn] 美 [ˈtɜ:rbaɪn] n.汽轮机;涡轮机;透平机 一.Hystrix Dashboard简介 在微服务架构中为了保证程序的可用性,防止程序出错导致网络阻 ...
- 服务容错保护断路器Hystrix之七:做到自动降级
从<高可用服务设计之二:Rate limiting 限流与降级>中的“自动降级”中,我们这边将系统遇到“危险”时采取的整套应急方案和措施统一称为降级或服务降级.想要帮助服务做到自动降级,需 ...
- 服务容错保护断路器Hystrix之五:配置
接着<服务容错保护断路器Hystrix之二:Hystrix工作流程解析>中的<2.8.关于配置>再列举重要的配置如下 一.hystrix在生产中的建议 1.保持timeout的 ...
- 服务容错保护断路器Hystrix之二:Hystrix工作流程解析
一.总运行流程 当你发出请求后,hystrix是这么运行的 红圈 :Hystrix 命令执行失败,执行回退逻辑.也就是大家经常在文章中看到的“服务降级”. 绿圈 :四种情况会触发失败回退逻辑( fal ...
- Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】
Spring Cloud(四):服务容错保护 Hystrix[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 分布式系统中经常会出现某个基础服务不可用 ...
- Dubbo 服务容错Hystrix
一.服务者 1.pom <dependency> <groupId>org.springframework.cloud</groupId> <artifact ...
- 白话SpringCloud | 第五章:服务容错保护(Hystrix)
前言 前一章节,我们知道了如何利用RestTemplate+Ribbon和Feign的方式进行服务的调用.在微服务架构中,一个服务可能会调用很多的其他微服务应用,虽然做了多集群部署,但可能还会存在诸如 ...
- Spring Cloud (8) 服务容错保护-Hystrix依赖隔离
依赖隔离 docker使用舱壁模式来实现进程的隔离,使容器与容器之间不会互相影响.而Hystrix则使用该模式实现线程池的隔离,它会为每一个Hystrix命令创建一个独立的线程池,这样就算在某个Hys ...
- Dubbo服务容错
当一个服务调用另一个远程服务出现错误时的外观 Dubbo提供了多种容错方案,默认值为failover(重试) 1).Failover Cluster(默认) 失败自动切换,当出现失败,重试其他服务器, ...
随机推荐
- BZOJ 3622 : 已经没有什么好害怕的了(dp + 广义容斥原理)
今天没听懂 h10 的讲课 但已经没有什么好害怕的了 题意 给你两个序列 \(a,b\) 每个序列共 \(n\) 个数 , 数之间两两不同 问 \(a\) 与 \(b\) 之间有多少配对方案 使得 \ ...
- mysql中存储过程
存储过程procedure 存储过程,其本质还是函数——但其规定:不能有返回值: 定义形式: 说明: 1,in:用于设定该变量是用来“接收实参数据”的,即“传入”:默认不写,就是in 2,out:用于 ...
- BZOJ 4754 [JSOI2016]独特的树叶 | 树哈希判同构
题目链接 这道题是一道判断无根树同构的模板题,判断同构主要的思路就是哈希. 一遇到哈希题,一百个人能有一百零一种哈希方式,这篇题解随便选用了一种--类似杨弋<Hash在信息学竞赛中的一类应用&g ...
- 倒置输入的整数(C、Python)
C语言: # include <stdio.h> void f(int num) { , j=, yu; printf("转置后:"); ) { yu = num ...
- bzoj4336 骑士的旅行 (树链剖分+multiset)
首先大概有一个树剖+树套树的做法,但我哪会写啊 然后发现k很小,如果用线段树记每个区间前k大的的话,可以O(k)地合并 而且一个点还有可能有好多个骑士,所以要用multiset维护一下 然后树剖就好啦 ...
- CF603E Pastoral Oddities
CF603E Pastoral Oddities 度数不好处理.转化题意:不存在连通块为奇数时候就成功了(自底向上调整法证明) 暴力:从小到大排序加入.并查集维护.全局变量记录奇数连通块的个数 答案单 ...
- Segment 李超线段树
题目大意: 要求在平面直角坐标系下维护两个操作: 1.在平面上加入一条线段.记第 i 条被插入的线段的标号为 i 2.给定一个数 k,询问与直线 x = k 相交的线段中,交点最靠上的线段的编号. 若 ...
- 几个简单常用的Sql语句
'; --查Cids为2的Gnumber列的和,列名为Ids select Cids,Plevel from People; select * from Salary; select * from S ...
- C# String类&Math类&DateTime类
String类: String a = "abcdefghijklmnopqrstuvwxyz"; int length = a.length; //获取字符串的长度: a = ...
- 猜数字小游戏,很naive......
这里用到了随机数生成器以及ctime #include <cstdio> #include <cstdlib> #include <ctime> #include ...