springcloud微服务项目下使用hystrixdashboard 服务监控

本文详细介绍了在微服务架构下如何利用Hystrix实现服务熔断,防止雪崩效应,以及如何通过Hystrix Dashboard进行实时监控,确保系统的稳定运行。文章深入解析了子项目和服务端的依赖配置,熔断机制的启用方法,以及监控端的搭建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.子项目

使用hystrixdashboard时子项目需要有相映的监控依赖和hystrix路段器依赖
并已经将provider服务注册进eureka中,在消费端访问provider端时hystrixdashboard可以监控访问数据。

  • 子项目相映依赖

<!-- hystrix 服务熔断@HystrixCommand-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
				<version>1.3.1.RELEASE</version>
		</dependency>
		<!--添加Eureka和hystrixdashboard监控的完善 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
			<version>1.5.9.RELEASE</version>
		</dependency>
  • 消费端访问提供端的方法上需要配置熔断机制:
    @HystrixCommand(fallbackMethod = “HystrixCommand_Get”)意为:
    在服务请求抛出异常时不会继续等待,而是访问fallbacMethod方法HystrixCommand_Get
@HystrixCommand(fallbackMethod = "HystrixCommand_Get")
	@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
	public Dept get(@PathVariable("id") Long id) {
		return service.get(id);
	}

public Dept HystrixCommand_Get(@PathVariable("id") Long id) {		
		return new Dept().setDeptno(id).setDname("null").setDbSource("NoSource");
	}
  • 在子项目的启动类上加入@EnableCircuitBreaker允许线路熔断机制打开
@EnableCircuitBreaker
@SpringBootApplication
public class ApplicationProvider_APP {
	public static void main(String[] args) {
		SpringApplication.run(ApplicationProvider_APP.class, args);
	}

}

2.hystrix监控端

  • 子项目Hystrixdashboard依赖

<!-- hystrix和 hystrix-dashboard相关 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
		</dependency>
  • 启动类加注解@EnableHystrixDashboard
@EnableHystrixDashboard
@SpringBootApplication
public class HystricDashboard_9001_APP {
	public static void main(String[] args) {
		SpringApplication.run(HystricDashboard_9001_APP.class, args);
	}

}

  • 启动测试
    在这里插入图片描述
  • 在框框中地址和数据
    地址加端口号/hystrix.stream
    输入刷新时间2000ms
    输入监控名字
    在这里插入图片描述
    不停的刷新添加了@HystrixCommand注解的方法url映射地址,在dashboard 上会有响应的数据。
    在这里插入图片描述
    控制台有相应的连接地址:
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值