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 在远程资源调用失败时的决策过程 ...
随机推荐
- R语言 table()函数
table函数 用 table() 函数统计因子各水平的出现次数(称为频数或频率).也可以对一般的向量统计每个不同元素的出现次数.如 sex = c("女","女&quo ...
- 郁闷的 IE6/7/8 所遇兼容问题
IE6,7只支持inline元素设置为inline-block,但不支持block元素转换成inline-block,所以非inline元素在IE6,7下要转换成inline-block,需先转换成i ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:上下文类
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- C# 中[DllImport("user32.dll")]和extern用法和示例----转载
原文:https://blog.csdn.net/michellehsiao/article/details/7629746 extern 修饰符用于声明在外部实现的方法.extern ...
- HDU 5525:Product 欧拉定理
Product Accepts: 21 Submissions: 171 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072 ...
- H5禁止底部横向滚动条,使一个元素居中
1.禁止底部横向滚动条 选择元素设置样式 { overflow-y:auto; overflow-x:hidden } 2.元素居中 { margin-left:auto ; margin-right ...
- flutter JSON序列化出现冲突
[SEVERE] Conflicting outputs were detected and the build is unable to prompt for permission to remov ...
- Thymeleaf--起步
Spring官方支持的服务的渲染模板中,并不包含jsp.而是Thymeleaf和Freemarker等,而Thymeleaf与SpringMVC的视图技术,及SpringBoot的自动化配置集成非常完 ...
- 一 Hibernate入门
Hibernate环境搭建 Hibernate的API Hibernate的CRUD EE三层结构: web层 业务逻辑层 持久层 jdbc,DBUTils,Hibernate Hib ...
- 二、Linux目录结构&常用指令
Linux目录结构: ps -ef:任务管理器 ifconfig: 查看ip ping : 测试与目标主机的连通性,ctrl+c停止 目录指令: ll:列出当前目录下的文件信息 ls -al : 列 ...