Spring Cloud Hystrix是一个容错库,它实现了断路器模式,使得当服务发生异常时,会自动切断连接,并将请求引导至预设的回调方法。

服务端

在Spring Tool Suite的文件菜单中,点击新建Spring Starter Project。建立一个普通的Restful风格的服务。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@SpringBootApplication
public class SpringcloudHystrixServerApplication { public static void main(String[] args) {
SpringApplication.run(SpringcloudHystrixServerApplication.class, args);
} @RequestMapping(value = "/message")
public String getMessage() {
return "Hello World!";
}
}

application.properties文件中配置服务的端口,server.port=8200

服务启动后,可以在浏览器查看相应接口。

客户端

再建立一个客户端应用程序,在创建时选择Hystrix,Hystrix Dashboard,Actuator和Web模块。

项目创建完成后,添加一个Service,其中包括调用服务端接口的方法及一个回调方法。注意这里@HystrixCommand的用法。

import java.net.URI;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service
public class MessageService {
private final RestTemplate restTemplate; public MessageService(RestTemplate rest) {
this.restTemplate = rest;
} @HystrixCommand(fallbackMethod = "reliable")
public String getMessage() {
URI uri = URI.create("http://localhost:8200/message"); return this.restTemplate.getForObject(uri, String.class);
} public String reliable() {
return "Woo, something wrong!";
}
}

在客户端的入口方法加上@EnableCircuitBreaker标记,并把它的端口设为server.port=8300

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @EnableHystrixDashboard
@EnableCircuitBreaker
@RestController
@SpringBootApplication
public class SpringcloudHystrixClientApplication { @Autowired
private MessageService messageService; @Bean
public RestTemplate rest(RestTemplateBuilder builder) {
return builder.build();
} public static void main(String[] args) {
SpringApplication.run(SpringcloudHystrixClientApplication.class, args);
} @RequestMapping("/message")
public String getMessge() {
return messageService.getMessage();
}
}

启动客户端后,如果在浏览器里看到页面能正常获取服务端的数据,说明当前客户端与服务端运作都是正常的。

然后,停止服务端,让情况出现异常。

刷新页面,可以看到这次的结果也在预料之内,当客户端调用服务端失败后,通过Hystrix的作用,自动切换至调用预设的回调方法。

仪表盘

Hystrix自带可视化仪表盘,在上面的客户端代码中,入口方法除了增加了@EnableCircuitBreaker标记外,还有@EnableHystrixDashboard。这样的设置便可以启用Hystrix的仪表盘。

不过在application.properties文件还需要加上以下配置,以避免“Unable to connect to Command Metric Stream”错误。

management.endpoints.web.exposure.include=hystrix.stream
management.endpoints.web.base-path=/

当客户端被启动后,使用http://localhost:8300/hystrix路径可以直接访问仪表盘。

之后在Hystrix Dashboard下面的地址栏内填上http://localhost:8300/hystrix.stream,再点击Monitor Stream按钮,监控结果一览无遗。

试水Spring Cloud Hystrix的更多相关文章

  1. spring cloud Hystrix监控面板Hystrix Dashboard和Turbine

    我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的.而这些请求情况的指标信息都是HystrixCommand和HystrixObservableCommand实例在执行过程 ...

  2. Spring Cloud Hystrix 1(熔断器简介)

    在分布式框架中当某个服务单元发生故障之后通过断路器的故障监控向调用方返回一个错误响应,而不是长期等待这样就不会使得线程因调用故障服务被长时间占用不放,避免了故障在分布式系统中的蔓延 针对上述问题,Sp ...

  3. Spring Cloud Hystrix 服务容错保护 5.1

    Spring Cloud Hystrix介绍 在微服务架构中,通常会存在多个服务层调用的情况,如果基础服务出现故障可能会发生级联传递,导致整个服务链上的服务不可用为了解决服务级联失败这种问题,在分布式 ...

  4. 笔记:Spring Cloud Hystrix 异常处理、缓存和请求合并

    异常处理 在 HystrixCommand 实现的run方法中抛出异常,除了 HystrixBadRequestException之外,其他异常均会被Hystrix 认为命令执行失败并触发服务降级处理 ...

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

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

  6. Spring Cloud 微服务笔记(六)Spring Cloud Hystrix

    Spring Cloud Hystrix Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力. 一.快速入门 1)依赖: <dep ...

  7. 第五章 服务容错保护:Spring Cloud Hystrix

    在微服务架构中,我们将系统拆分为很多个服务,各个服务之间通过注册与订阅的方式相互依赖,由于各个服务都是在各自的进程中运行,就有可能由于网络原因或者服务自身的问题导致调用故障或延迟,随着服务的积压,可能 ...

  8. spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix

    spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix 首先application.yml / applicatoin.propreties的配置项:fe ...

  9. spring cloud: Hystrix(四):feign类似于hystrix的断容器功能:fallback

    spring cloud: Hystrix(四):feign使用hystrix @FeignClient支持回退的概念:fallback方法,这里有点类似于:@HystrixCommand(fallb ...

随机推荐

  1. php Date()函数输出中文年月日时分秒

    当然了,PHP的DATE函数是不可能直接输出中文的年月日的,但可以用下面这种方法自己写一个函数. 代码如下 复制代码 function today(){ date_default_timezone_s ...

  2. mysql 编译安装 window篇

    传送门 # mysql下载地址 https://www.mysql.com/downloads/ # 找到MySQL Community Edition (GPL) https://dev.mysql ...

  3. Lua Linux环境下安装

    首先下载Lua包 解压缩 进入执行make linux 如果碰到lua.c:67:31: fatal error: readline/readline.h: No such file or direc ...

  4. 地图组件上的自定义区域叠加层显示 ArcGis + GeoJson

    最近参与了一个IOT环境项目,需要对某个城市的某几个区域做环境监控与治理,其中就用到了地图叠加层的功能,粗看很复杂,其实很简单,先来看一下效果,然后再来讲一下如何实现的: 中间的黄色轮廓线包括的几块区 ...

  5. Django-Signals信号量

    信号量最为Django的一个核心知识点,在项目中很少有使用到,所以很多人都不了解或者没听过过(包括我).简单来说就是在进行一些操作的前后我们可以发出一个信号来获得特定的操作,这些操作包括(信息来自:h ...

  6. elast alert

    参考文档:<elast alert> 假设报错的内容为: ceph-rest-api service down At least 1 events occurred between 201 ...

  7. linux每日命令(31):tar命令

    tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁带上创建档案,现在,用户可以在 ...

  8. 【6集iCore3_ADP触摸屏驱动讲解视频】6-4 底层驱动之SDRAM读写(上)

    源视频包下载地址: 链接:http://pan.baidu.com/s/1i5lzzj3 密码:bwoe   银杏科技优酷视频发布区: http://i.youku.com/gingko8  

  9. git-ftp代码部署方式

    虽然如今ci方法已经在很多团队使用了,但对于一些个人性的基于PHP的跑在虚拟主机的小项目,既没有服务端的Git环境,又不想时刻跑一个Genkins,就只能回到原始的FTP上传了. 所幸有了git-ft ...

  10. [工具类] 读取解析json文件

    读取json文件并转换为字符串 /** * 通过本地文件访问json并读取 * * @param path:json文件路径 * @return:json文件的内容 */ public static ...