消费

<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 实现断路器进行服务容错保护的更多相关文章

  1. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  2. 白话SpringCloud | 第五章:服务容错保护(Hystrix)

    前言 前一章节,我们知道了如何利用RestTemplate+Ribbon和Feign的方式进行服务的调用.在微服务架构中,一个服务可能会调用很多的其他微服务应用,虽然做了多集群部署,但可能还会存在诸如 ...

  3. Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】

    Spring Cloud(四):服务容错保护 Hystrix[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  分布式系统中经常会出现某个基础服务不可用 ...

  4. Spring Cloud (8) 服务容错保护-Hystrix依赖隔离

    依赖隔离 docker使用舱壁模式来实现进程的隔离,使容器与容器之间不会互相影响.而Hystrix则使用该模式实现线程池的隔离,它会为每一个Hystrix命令创建一个独立的线程池,这样就算在某个Hys ...

  5. 笔记:Spring Cloud Hystrix 服务容错保护

    由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身问题出现调用故障或延迟,而这些问题会直接导致调用方的对外服务也出现延迟,若此时调用方的请求不断增加 ...

  6. SpringCloud系列之服务容错保护Netflix Hystrix

    1. 什么是雪崩效应? 微服务环境,各服务之间是经常相互依赖的,如果某个不可用,很容易引起连锁效应,造成整个系统的不可用,这种现象称为服务雪崩效应. 如图,引用国外网站的图例:https://www. ...

  7. Spring Cloud Hystrix 服务容错保护

    目录 一.Hystrix 是什么 二.Hystrix断路器搭建 三.断路器优化 一.Hystrix 是什么 ​ 在微服务架构中,我们将系统拆分成了若干弱小的单元,单元与单元之间通过HTTP或者TCP等 ...

  8. SpringCloud开发学习总结(五)—— 服务容错保护Hystrix

    在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式相互依赖.但由于每个单元都在不同的进程中运行,一来通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身 ...

  9. 服务容错保护hystrix

    灾难性雪崩效应 如何解决灾难性雪崩效应 降级 超时降级.资源不足时(线程或信号量)降级,降级后可以配合降级接口返回托底数据.实现一个 fallback 方法, 当请求后端服务出现异常的时候, 可以使用 ...

随机推荐

  1. 个人永久性免费-Excel催化剂功能第73波-数据转换:单行多项目转多行单项目

    数据分析的前半部分数据处理.转换等工作是一个又脏又累的活,默默地干着,却又不出彩让人看到过程的艰辛和成果.如何让这个过程可以更加轻松一点,是Excel催化剂为大家所想的,今天带来一大刚需的数据转换功能 ...

  2. Spring管理事物两种方式

    Spring管理事物两种方式 1. 编程式事物管理(在开发中不经常使用) 使用步骤 1. 配置数据库事物管理 DataSourceTransactionManager <!--配置事物管理器-- ...

  3. SpringBoot第二十二篇:应用监控之Actuator

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11226397.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   很多文章都 ...

  4. WSASocket()创建套接字不成功解决方法

    这几天我在写一个模仿windows自带的ping程序,可是套接字总是创建不成功,在网上找了一些资料最后总算把问题解决了,现在总结一下. 解决方法:以管理员运行VS就行了我的是vs2013,vs2010 ...

  5. bitset的简单用法

    1.头文件 #include<bitset> 2.基本操作 bitset<n> b; b有n位,每位都为0. 参数n可以为一个表达式.如bitset<5> b, 则 ...

  6. 机器学习经典算法之AdaBoost

    一.引言 在数据挖掘中,分类算法可以说是核心算法,其中 AdaBoost 算法与随机森林算法一样都属于分类算法中的集成算法. /*请尊重作者劳动成果,转载请标明原文链接:*/ /* https://w ...

  7. Java代码计算运行时间

    突然想准确的测试一下Java代码的执行时间,在网上找了一会.发现基本有以下两种方法:第一种是以毫秒为单位计算的. Java代码 //伪代码 long startTime=System.currentT ...

  8. 整理用Java实现数字转化成字符串左边自动补零方法

    Java 中给数字左边补0 (1)方法一 import java.text.NumberFormat; public class NumberFormatTest { public static vo ...

  9. 数据结构-二叉搜索树和二叉树排序算法(python实现)

    今天我们要介绍的是一种特殊的二叉树--二叉搜索树,同时我们也会讲到一种排序算法--二叉树排序算法.这两者之间有什么联系呢,我们一起来看一下吧. 开始之前呢,我们先来介绍一下如何创建一颗二叉搜索树. 假 ...

  10. 简洁明了的Noip考场策略 / 平时做题也适用

    1.选择策略: 评估的标准得分的难度不是AC的难度 2.思考问题: 怀疑的眼光审视自己 3.写代码前: 想想可不可以换一种代码实现会好写很多 把自己的思路再理一遍,可以写到纸上,记下来大致关键顺序 4 ...