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 按钮, 监控结果一览无遗.
来源: https://www.cnblogs.com/kenwoo/p/9693980.html