SpringCould-------使用Hystrix 实现断路器进行服务容错保护
消费:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sam</groupId>
<artifactId>hello-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent> <properties>
<javaVersion>1.8</javaVersion>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies> </dependencyManagement> <dependencies>
<!-- 引入eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 引入ribbon 依赖 ,用来实现负载均衡,我们这里只是使用,先不作其他介绍-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入hystrix 依赖 ,用来实现服务容错保护-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
</dependencies>
</project>
启动类:
/*@SpringBootApplication +
@EnableDiscoveryClient+
@EnableCircuitBreaker
= @SpringCloudApplication */ @SpringCloudApplication
public class CustomerApplication {
//@Bean 应用在方法上,用来将方法返回值设为为bean
@Bean
@LoadBalanced //@LoadBalanced实现负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
} }
service:
@Service
public class ConsumerService { @Autowired
RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "errorMsg")
public String consumer() { return restTemplate.getForObject("http://hello-service/hello", String.class);
} /**
* 由 @HystrixCommand(fallbackMethod = "errorMsg")
* 可知 在起多个服务时,如果其中一个服务宕机,则会调用 errorMsg()
* 方法; 相比在不加@EnableCircuitBreaker断路器,的包500d的错
*/ public String errorMsg() {
return "error!!!";
}
}
controller:
@RestController
public class ConsumerController { //这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例
/*@Autowired
RestTemplate restTemplate;*/ @Autowired
ConsumerService consumerService; @RequestMapping("/consumer")
public String helloConsumer() {
//调用hello-service服务,注意这里用的是服务名,而不是具体的ip+port
// return restTemplate.getForObject("http://hello-service/hello", String.class);
return consumerService.consumer();
}
}
SpringCould-------使用Hystrix 实现断路器进行服务容错保护的更多相关文章
- spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护
在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...
- 白话SpringCloud | 第五章:服务容错保护(Hystrix)
前言 前一章节,我们知道了如何利用RestTemplate+Ribbon和Feign的方式进行服务的调用.在微服务架构中,一个服务可能会调用很多的其他微服务应用,虽然做了多集群部署,但可能还会存在诸如 ...
- Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】
Spring Cloud(四):服务容错保护 Hystrix[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 分布式系统中经常会出现某个基础服务不可用 ...
- Spring Cloud (8) 服务容错保护-Hystrix依赖隔离
依赖隔离 docker使用舱壁模式来实现进程的隔离,使容器与容器之间不会互相影响.而Hystrix则使用该模式实现线程池的隔离,它会为每一个Hystrix命令创建一个独立的线程池,这样就算在某个Hys ...
- 笔记:Spring Cloud Hystrix 服务容错保护
由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身问题出现调用故障或延迟,而这些问题会直接导致调用方的对外服务也出现延迟,若此时调用方的请求不断增加 ...
- SpringCloud系列之服务容错保护Netflix Hystrix
1. 什么是雪崩效应? 微服务环境,各服务之间是经常相互依赖的,如果某个不可用,很容易引起连锁效应,造成整个系统的不可用,这种现象称为服务雪崩效应. 如图,引用国外网站的图例:https://www. ...
- Spring Cloud Hystrix 服务容错保护
目录 一.Hystrix 是什么 二.Hystrix断路器搭建 三.断路器优化 一.Hystrix 是什么 在微服务架构中,我们将系统拆分成了若干弱小的单元,单元与单元之间通过HTTP或者TCP等 ...
- SpringCloud开发学习总结(五)—— 服务容错保护Hystrix
在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式相互依赖.但由于每个单元都在不同的进程中运行,一来通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身 ...
- 服务容错保护hystrix
灾难性雪崩效应 如何解决灾难性雪崩效应 降级 超时降级.资源不足时(线程或信号量)降级,降级后可以配合降级接口返回托底数据.实现一个 fallback 方法, 当请求后端服务出现异常的时候, 可以使用 ...
随机推荐
- markdown浅谈
markdown是啥? markdown就是一种修饰网页/博客的方法,他能使网页变得更美观. 我们先解释一下代码框: 这个没法保留,就是把键盘左上角的⋅·⋅ 切换成英文变成`. 然后``` 在隔一行` ...
- 关于ftp响应码的分析【转载】
转载地址: http://www.jb51.net/article/26649.htm 1开头-成功 2开头-成功 3开头-权限问题 4开头-文件问题 5开头-服务器问题 150 FILE: %s 1 ...
- python检测变量名
python检测变量名 变量在编程中的用途非常广,在python中,变量的名称只能以字母或者下划线“_”开头,变量名只能由字母.数字.下划线组成. 编写python,使得其实现以下功能: 1.输入一个 ...
- 给定一个IP地址,转化为二进制32位,再转化为十进制,写出一个方法让其十进制转为IP地址
十进制是已知的数值 第一种方法: <script type="text/javascript"> var num=2148140545; var str=num.toS ...
- web设计_8_数据表格内容样式分离
1.页面需要用到table的时候,样式重置CSS要设置: table{ border-collapse: collapse; border-spacing:; } 2. HTML结构 <tabl ...
- MOCTF-Crypt-writeup
MOctf Crypt Writeup记录 都不难,就随便记录记录下. MOCTF平台地址:http://www.moctf.com 0x01 数据库密码 hint:20岁的小刚,自幼热爱信息安全,一 ...
- JavaFX 集成 Sqlite 和 Hibernate 开发爬虫应用
目录 [隐藏] 0.1 前言: 0.2 界面 0.3 Maven 环境 0.4 项目结构 0.5 整合 Hibernate 0.5.1 SQLiteDialect.java 数据库方言代码 0.5.2 ...
- JAVA面向对象面试题带答案(墙裂推荐)
1) 在Java中,如果父类中的某些方法不包含任何逻辑,并且需要有子类重写,应该使用(c)关键字来申明父类的这些方法. a) Finalc b) Static c) Abstract d) Void2 ...
- Eclipse 连接不上 hadoop 的解决办法
先说一下我的情况,集群的 hadoop 是 1.0.4 ,之后在虚拟机上搭建了最新稳定版 1.2.1 之后,Eclipse 插件始终连接不上. 出现 Error: Call to 192.168.1. ...
- .netcore持续集成测试篇之开篇简介及Xunit基本使用
系列目录 为了支持跨平台,微软为.net平台提供了.net core test sdk,这样第三方测试框架诸如Nunit,Xunit等只需要按照sdk提供的api规范进行开发便可以被dotnet cl ...