美文网首页Java
SpringCloud进阶:一文通透RabbitMQ服务监控

SpringCloud进阶:一文通透RabbitMQ服务监控

作者: 马小莫QAQ | 来源:发表于2020-12-24 16:12 被阅读0次

前面我们介绍了通过turbine直接聚合多个服务的监控信息,实现了服务的监控,但是这种方式有个不太好的地方就是turbine和服务的耦合性太强了,针对这个问题,我们可以将服务的监控消息发送到RabbitMQ中,然后turbine中RabbitMQ中获取获取监控消息,这样就实现类服务和turbine的解耦。

我们通过案例来演示下如何实现该效果

一、启动RabbitMQ服务

显然我们需要安装启动一个RabbitMQ服务

二、创建consumer服务

创建一个consumer服务,同时要将dashboard的监控信息发送到RabbitMQ服务中。

1.创建项目

2.添加依赖

此处的核心依赖如下,完整依赖参考git完整代码

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    <version>1.3.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

3.修改配置

添加对RabbitMQ的配置信息

spring.application.name=shop-product-consumer-hystrix-dashboard
server.port=9090
#设置服务注册中心地址,指向另一个注册中心
eureka.client.serviceUrl.defaultZone=http://dpb:123456@eureka1:8761/eureka/,http://dpb:123456@eureka2:8761/eureka/

#Feign 默认是不开启 Hystrix 的。默认为:false
feign.hystrix.enabled=true

spring.rabbitmq.host=192.168.88.150
spring.rabbitmq.port=5672
spring.rabbitmq.username=dpb
spring.rabbitmq.password=123
spring.rabbitmq.virtualHost=/

4.修改启动类

启动服务,并访问测试

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard
public class SpringcloudFeignProdcutConsumerApplication {

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

}

访问请求:http://localhost:9090/list

获取监控数据:http://localhost:9090/hystrix.stream

同时注意RabbitMQ的控制台

能够看到是有变化的

三、创建turbine服务

1.创建项目

2.添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-turbine-stream</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>
</dependencies>

3.修改配置

spring.application.name=shop-product-consumer-turbine
server.port=1002
#设置服务注册中心地址,指向另一个注册中心
eureka.client.serviceUrl.defaultZone=http://dpb:123456@eureka1:8761/eureka/,http://dpb:123456@eureka2:8761/eureka/

spring.rabbitmq.host=192.168.88.150
spring.rabbitmq.port=5672
spring.rabbitmq.username=dpb
spring.rabbitmq.password=123
spring.rabbitmq.virtualHost=/

4.启动类

注意注解

@SpringBootApplication
@EnableTurbineStream
public class SpringcloudFeignProdcutConsumerApplication {

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

}

启动服务,访问:http://localhost:1002/turbine.stream

说明从RabbitMQ中获取到了监控数据

四、创建dashboard可视服务

可视化的服务可以用之前创建的即可

启动服务

看到了consumer的监控数据,案例成功!

相关文章

网友评论

    本文标题:SpringCloud进阶:一文通透RabbitMQ服务监控

    本文链接:https://www.haomeiwen.com/subject/msvmnktx.html