springcloud hystrix 熔断器

本贴最后更新于 2022 天前,其中的信息可能已经物是人非

hystrix 熔断器

1.Maven 导入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2.开启熔断

@EnableHystrix 激活 : 单纯的功能

@EnableCircuitBreaker 激活 : @ EnableHystrix+springcloud 功能

3.对应 controller 增加

@RestController
public class SpringCloudHystrixApplication {
	public static final Random random = new Random();
	public static void main(String[] args) {
		SpringApplication.run(SpringCloudHystrixApplication.class, args);
	}
	@GetMapping("/hello")
	@HystrixCommand(fallbackMethod="fail",commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100")
	})
	public String hello() throws InterruptedException {
		int anInt = random.nextInt(200);
		System.out.println("随机数睡眠时间 : "+anInt);
		Thread.sleep(anInt);
		return "hello";
	}
	public String fail(){
		return "fail";
	}
}

4.开启监控

开启启动器访问权限
  1. application.properties
management.endpoints.web.exposure.include: hystrix.stream
  1. 访问 : http://localhost:8080/actuator/hystrix.stream 即可

注意在 Application.java 中使用的是 @EnableCircuitBreaker 注解

hystrix dashboard 监控器

1.Maven 导入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

2.Application.java 开启注解 @EnableHystrixDashboard

3.访问配置

  • 访问 http://localhost:7070/hystrix
  • 配置 url : http://localhost:8080/actuator/hystrix.stream

Turbine 监控

在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix 提供了一个开源项目(Turbine)来提供把多个 hystrix.stream 的内容聚合为一个数据源供 Dashboard 展示。

1.Maven 导入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>

2.Application.java 增加注解

@SpringBootApplication
@EnableTurbine
@EnableHystrixDashboard
public class SpringCloudHystrixDashboardApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudHystrixDashboardApplication.class, args);
	}
}

3.application.properties 配置

server.port = 7070
spring.application.name=hystrix-dashboard-turbine
##消费方的名称
turbine.appConfig=eureka.client.consumer  
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
### 因为它需要去寻找服务,所以也需要注册进eureka server中
eureka.client.serviceUrl.defaultZone=http://localhost:9090/eureka/
  • turbine.appConfig :配置 Eureka 中的 serviceId 列表,表明监控哪些服务
  • turbine.aggregator.clusterConfig :指定聚合哪些集群,多个使用”,”分割,默认为 default。可使用 http://.../turbine.stream?cluster={clusterConfig之一} 访问
  • turbine.clusterNameExpression : 1. clusterNameExpression 指定集群名称,默认表达式 appName;此时:turbine.aggregator.clusterConfig 需要配置想要监控的应用名称;2. 当 clusterNameExpression: default 时,turbine.aggregator.clusterConfig 可以不写,因为默认就是 default;3. 当 clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了 eureka.instance.metadata-map.cluster: ABC,则需要配置,同时 turbine.aggregator.clusterConfig: ABC

4.分别启动项目,访问 http://localhost:7070/turbine.stream .

  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:SymSoloVditor思源笔记

    1090 引用 • 3467 回帖 • 297 关注
  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    938 引用 • 1456 回帖 • 163 关注
  • Hystrix
    7 引用

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...