spingcloud--hystrix(断路器)
hystrix由来:服务器宕机或者依赖关系失败。
hystrix:
server:
port: 6003
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka
instance:
instance-id: consumer6003
prefer-ip-address: true
spring:
application:
name: consumer3
3:启动类(App):
@SpringBootApplication
@EnableEurekaClient
@Configuration
@EnableHystrix
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
4:控制器(HelloController):
@SuppressWarnings("unchecked")
@RestController
public class HelloController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/hi")
@HystrixCommand(fallbackMethod="helloFallback")
public Map<String,Object> hello(){
return restTemplate.getForObject("http://provider-one/hello", Map.class);
}
public Map<String,String> helloFallback(){
Map<String,String> map = new HashMap<String, String>();
map.put("info", "失败");
return map;
}
}
比较ribbo:
在方法上添加注解@HystrixCommand(fallbackMethod="helloFallback")
属性fallbackMethod的值是你要调用的方法
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
2:修改配置文件(application.yml):
server:
port: 6004
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka
instance:
instance-id: consumer6004
prefer-ip-address: true
spring:
application:
name: consumer4
3:启动类(App):
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@Configuration
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
4:接口
@FeignClient(value = "provider-one",fallback=HelloServiceFallBach.class)
public interface HelloService {
@GetMapping("hello")
public Map<String,Object> h();
}
5:控制器
@RestController
public class HelloController {
@Autowired
private HelloService service;
@RequestMapping("jj")
public Map<String,Object> hello(){
return service.h();
}
}
6:错误返回控制器:
@Component
public class HelloServiceFallBach implements HelloService{
@Override
public Map<String, Object> h() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("info", "错误");
return map;
}
}
@Component别忘了加
spingcloud--hystrix(断路器)的更多相关文章
- SpringCloud 进阶之Hystrix(断路器)
1. Hystrix 断路器 Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败, 比如超时,异常等,Hystrix能够保证在一个依赖出问题的情况 ...
- SpringCloud的入门学习之概念理解、Hystrix断路器
1.分布式系统面临的问题,复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败. 2.什么是服务雪崩? 答:多个微服务之间调用的时候,假设微服务A调用微服务B和微服务 ...
- SpringCloud(五)之Spring Cloud 中 Feign结合Hystrix断路器开发实战
1.先讲hystrx(断路器) 在springcloub 中的使用 1.1 加入依赖 注意:网上新旧版本问题,所以要以官网为主,不然部分注解会丢失最新版本 2.0 <dependency> ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下
笔记 4.Feign结合Hystrix断路器开发实战<下> 简介:讲解SpringCloud整合断路器的使用,用户服务异常情况 1.feign结合Hystrix ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上
笔记 3.Feign结合Hystrix断路器开发实战<上> 简介:讲解SpringCloud整合断路器的使用,用户服务异常情况 1.加入依赖 注意:网上新旧版本问 ...
- SpringCloud学习笔记(七):Hystrix断路器
概述 什么时候需要断路器?熔断? 举个简单的例子:小明喜欢小美,可是小美没有电话,小美给了小明家里的座机,小明打给座机,这个时候小美的妈妈接到了,小明怕妈妈知道自己喜欢小美,就跟小美妈妈说让小美哥接电 ...
- Springcloud 整合Hystrix 断路器,支持Feign客户端调用
1,在这篇博文中,已经大致说过了Springcloud服务保护框架 Hystrix在服务隔离,服务降级,以及服务熔断中的使用 https://www.cnblogs.com/pickKnow/p/11 ...
- SpringCloud(三)Hystrix断路器
Hystrix断路器 概述 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败 服务雪崩 多个微服务之间调用的时候,假设微服务A调用微服务B和 ...
- Hystrix断路器中的服务熔断与服务降级
一.Hystrix断路器 微服务架构特点就是多服务,多数据源,支撑系统应用.这样导致微服务之间存在依赖关系.如果其中一个服务故障,可能导致系统宕机,这就是所谓的雪崩效应. 1.为什么需要断路器 服务雪 ...
- 微服务架构 | 5.1 使用 Netflix Hystrix 断路器
目录 前言 1. Hystrix 基础知识 1.1 Hystrix 断路器强调调用 1.2 两大类别的 Hystrix 实现 1.3 舱壁策略 1.4 Hystrix 在远程资源调用失败时的决策过程 ...
随机推荐
- Java8 HashMap详解
Java8 HashMap Java8 对 HashMap 进行了一些修改,最大的不同就是利用了红黑树,所以其由 数组+链表+红黑树 组成. 根据 Java7 HashMap 的介绍,我们知道,查找的 ...
- excel2003 颜色筛选问题
"excel2003中,添加辅助列,用定义名称的方法得到对应的颜色号,然后对辅助列进行排序: 颜色单元格在A列,选中B1,插入->名称->定义,输入a,下面输入公式 =get.c ...
- token和session的区别
session和token都是用来保持会话,功能相同 一.session机制,原理 session是服务端存储的一个对象,主要用来存储所有访问过该服务端的客户端的用户信息(也可以存储其他信息),从而实 ...
- 关于Redis 分布式 微服务 集群Cluster
一:Redis 1,redis是一个高性能的键值对存储方式的数据库,同时还提供list,set,zset,hash等数据结构的存储. 2,Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集 ...
- 越南FCK批量拿站
关键词:inurl:detail_product.asp?lang= /FCKeditor/_samples/asp/sample01.asp/FCKeditor/_samples/asp/sampl ...
- java并发:interrupt进程终止
interrupt进程终止 interrupt()源码 /** * Interrupts this thread. * * <p> Unless the current thread is ...
- linux修改键盘按键
我的电脑:Fedora-19 $ uname -a Linux localhost.localdomain 3.11.10-200.fc19.i686 #1 SMP Mon Dec 2 20:48:2 ...
- SIM800L AT command
/*********************************************************** AT+ICF==<format> ,<parity> ...
- 30 整数中1出现的次数(从1到n整数中1出现的次数)这题很难要多看*
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- JavaWeb开发:从购买服务器到简单demo运行
写这篇文章的目的: 一个是为了记录实施过程,方便自己日后查阅: 另一个是给项目组成员提供一个参考,方便他们以后搭建自己的项目环境: 当然若能帮助到更多的朋友,那就再好不过了:D 需要注意: 我本身也是 ...