Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 304|回复: 0

Spring Cloud Alibaba学习笔记(13) - Spring Cloud Stream的监控与异常处理

[复制链接]
  • TA的每日心情
    奋斗
    前天 11:25
  • 签到天数: 792 天

    [LV.10]以坛为家III

    2049

    主题

    2107

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    723136
    发表于 2021-6-24 16:41:16 | 显示全部楼层 |阅读模式

    Spring Cloud Stream监控

    Spring Boot Actuator组件用于暴露监控端点,很多监控工具都需要依赖该组件的监控端点实现监控。而项目集成了Stream及Actuator后也会暴露相应的监控端点.

    首先需要在项目里集成Actuator,添加依赖如下:

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

    添加配置,暴露所有监控端点,并显示健康检测详情

    management:
      endpoint:
        health:
          # 显示健康检测详情
          show-details: always
      endpoints:
        web:
          exposure:
            # 暴露所有监控端点
            include: '*'
    

    访问http://localhost:端口号/actuator可以获取所有暴露出来的监控端点,Stream的相关监控端点也在其中

    /actuator/bindings端点可以用于查看bindings相关信息:

    /actuator/channels端点用于查看channels的相关信息,“input”和“output”就是channel,可以认为这些channel是topic的抽象:

    /actuator/health端点中可以查看binder及RocketMQ的状态,主要是用于查看MQ的连接情况,如果连接不上其status则为DOWN:

    Spring Cloud Stream异常处理

    局部处理

    配置文件

    spring:
      cloud:
        stream:
          bindings:
            input:
              destination: test-destination
              group: test-group
            output:
              destination: test-destination
    

    代码实现

    @Slf4j
    @SpringBootApplication
    @EnableBinding({Processor.class})
    @EnableScheduling
    public class Study01Application {
        public static void main(String[] args) {
            SpringApplication.run(Study01Application.class, args);
        }
    
        @StreamListener(value = Processor.INPUT)
        public void handle(String body) {
            throw new RuntimeException("运行时错误");
        }
    
        @ServiceActivator(inputChannel = "test-destination.test-group.errors")
        public void handleError(ErrorMessage message) {
            Throwable throwable = message.getPayload();
            log.error("截获异常", throwable);
    
            Message<?> originalMessage = message.getOriginalMessage();
            assert originalMessage != null;
    
            log.info("原始消息体 = {}", new String((byte[]) originalMessage.getPayload()));
        }
    
        @Bean
        @InboundChannelAdapter(value = Processor.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
        public MessageSource<String> test() {
            return () -> new GenericMessage<>("qwer");
        }
    }
    

    全局处理

    代码实现

    @StreamListener(value = Processor.INPUT)
    public void handle(String body) {
        throw new RuntimeException("运行时错误");
    }
    
    @StreamListener("errorChannel")
    public void error(Message<?> message) {
        ErrorMessage errorMessage = (ErrorMessage) message;
        log.warn("Handling ERROR = {} " + errorMessage);
    }
    
    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-9-16 21:55 , Processed in 0.058219 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表