试水Spring Cloud Hystrix
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的更多相关文章
- spring cloud Hystrix监控面板Hystrix Dashboard和Turbine
我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的.而这些请求情况的指标信息都是HystrixCommand和HystrixObservableCommand实例在执行过程 ...
- Spring Cloud Hystrix 1(熔断器简介)
在分布式框架中当某个服务单元发生故障之后通过断路器的故障监控向调用方返回一个错误响应,而不是长期等待这样就不会使得线程因调用故障服务被长时间占用不放,避免了故障在分布式系统中的蔓延 针对上述问题,Sp ...
- Spring Cloud Hystrix 服务容错保护 5.1
Spring Cloud Hystrix介绍 在微服务架构中,通常会存在多个服务层调用的情况,如果基础服务出现故障可能会发生级联传递,导致整个服务链上的服务不可用为了解决服务级联失败这种问题,在分布式 ...
- 笔记:Spring Cloud Hystrix 异常处理、缓存和请求合并
异常处理 在 HystrixCommand 实现的run方法中抛出异常,除了 HystrixBadRequestException之外,其他异常均会被Hystrix 认为命令执行失败并触发服务降级处理 ...
- 笔记:Spring Cloud Hystrix 服务容错保护
由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身问题出现调用故障或延迟,而这些问题会直接导致调用方的对外服务也出现延迟,若此时调用方的请求不断增加 ...
- Spring Cloud 微服务笔记(六)Spring Cloud Hystrix
Spring Cloud Hystrix Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力. 一.快速入门 1)依赖: <dep ...
- 第五章 服务容错保护:Spring Cloud Hystrix
在微服务架构中,我们将系统拆分为很多个服务,各个服务之间通过注册与订阅的方式相互依赖,由于各个服务都是在各自的进程中运行,就有可能由于网络原因或者服务自身的问题导致调用故障或延迟,随着服务的积压,可能 ...
- spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix
spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix 首先application.yml / applicatoin.propreties的配置项:fe ...
- spring cloud: Hystrix(四):feign类似于hystrix的断容器功能:fallback
spring cloud: Hystrix(四):feign使用hystrix @FeignClient支持回退的概念:fallback方法,这里有点类似于:@HystrixCommand(fallb ...
随机推荐
- ORA-16447 Redo apply was not active at the target standby database
Cause ALTER SYSTEM FLUSH REDO TO STANDBY failed because redo apply is not active at the target datab ...
- Notes中几个处理多值域的通用函数
1.查找出查找内容在多值域中的索引值 getItemIndex(域名,域值,文档) Public Function getItemIndex(ByVal fieldName As String, By ...
- 基于Ubuntu搭建Seafile专属网盘
系统要求: Ubuntu 16.04.1 LTS 64 位操作系统 安装 Seafile 服务器 安装依赖环境 在 Debian/Ubuntu 系统下,可以使用以下命令安装 MySQL: sudo a ...
- [转载]js正则表达式/replace替换变量方法
原文地址:http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html JavaScript正则实战(会根据最近写的不断更新) 1.j ...
- imp-oracle10g数据库dmp导入到11g数据库提示IMP-00058,表或试图不存在
拿了一份从10g数据库导出的dmp,本机安装的是11.2g的oracle数据库,执行imp导入后提示: IMP-00058: 遇到 ORACLE 错误 942ORA-00942: 表或视图不存在 开始 ...
- python uuid 介绍
1. 背景知识: UUID: 通用唯一标识符 ( Universally Unique Identifier ), 对于所有的UUID它可以保证在空间和时间上的唯一性. 它是通过MAC地址, 时间戳, ...
- 当 Visual Studio 扩展遇到错误时
我是遇到了 Github 扩展经常在 Visual Studio 启动时报错,找了一下可以尝试以下方法: 首先卸载插件 然后删除 %LocalAppData%\Microsoft\VisualStud ...
- Features Download Pricing Mind Maps Blog XMind的快捷键
XMind提供很多快捷键.使用XMind时,在操作的过程中结合快捷键,双手同时操作,将给你带来很大的便利.例如下面一些常用的快捷键: 编辑主题:F2 添加标签:F3 创建一个新的空白工作簿:Ctrl+ ...
- lua -- 在弹框中显示物品列表
-- 宝箱打开后,显示里面的物品列表 -- 显示方式是:一个一个显示,每三个一行,一行显示完就移动到下一行, -- 接下来的一样要显示框的中间位置,方便玩家看 function UIBagContro ...
- IOS开发之Storyboard应用
制作一个Tab类型的应用 制作一个表格视图 原型表格单元 设计自定义的原型单元格 为原型单元格设置子类 故事版(Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明 ...