试水Spring Cloud Hystrix

2021/4/14 10:30:19

本文主要是介绍试水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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程