Spring cloud monitor 自定义通知方式

Spring cloud monitor 服务监控自定义通知

近段时间发现注册到eureka的服务监控还不算完美,服务架构中之用到了邮件通知管理员的机制,但是如果管理员不在电脑边或手机没有开网络,那么就没有办法及时得到通知,这个时候“古老“的方式:短信通知就浮出水面,但是通过阅读官方文档,并没有发现有提相关的配置方式,据我了解,只有一下几种方式


好废话不多说,下面直接上Demo:

首先通过monitor的启动日志发现监控注册到eureka服务的健康状况是通过一个抽象方法来实现的(AbstractStatusChangeNotifier),这个方法,一看就是用来继承的

创建NotifyAdmin类并且继承AbstractStatusChangeNotifier抽象类,重写其doNotify方法

上代码

/**
 * Created by newbee on 2017/4/14.
 */
public class NotifierAdmin extends AbstractStatusChangeNotifier  {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private String[] phones = {"18519303003"};

    @Autowired
    private AliSmsSenderImpl smsSender;
    private static final String DEFAULT_TEXT = "#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}";
    private final SpelExpressionParser parser = new SpelExpressionParser();
    private String[] ignoreChanges = new String[]{"UNKNOWN:DOWN"};
    @Override
    protected void doNotify(ClientApplicationEvent event) throws Exception {
        super.setIgnoreChanges(ignoreChanges);
        String smsContent = getSmsContent(event);
        String status = event.getApplication().getStatusInfo().getStatus();
        String name = event.getApplication().getName();
        if(!check(phones,smsContent)){
            return;
        }
        boolean b = smsSender.sendSMS(phones, smsContent);
        logger.info(name + " status changed to " + status);
        logger.info(" send sms is " + b );
        logger.info("send sms to"+phones.length+" administrators");
        logger.info(" there are "+getPhoneNumbers(phones));
    }
       /**
     * 封装信息消息体
     * */
    private String getSmsContent(ClientApplicationEvent event){
        Expression expression = parser.parseExpression(DEFAULT_TEXT, ParserContext.TEMPLATE_EXPRESSION);
        EvaluationContext context = new StandardEvaluationContext(event);
        String content = expression.getValue(context, String.class);
        return content + "\n CurrentDateTime is \n" + DateFormatUtils.format(new Date(), DateUtil.PAY_TIME_FMT);
    }

总结

善于重写已有的方法,注意阅读官方文档,通过细节找到突破点,就可以解决一些看似困难的问题,仅此作为一次笔记,其实也是第一次接触spring cloud。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Cloud Bus是一个用于在分布式系统中传播状态变化的消息总线。它基于Spring Cloud Stream和Spring Cloud Config构建,可以将消息广播到整个系统中的所有服务实例。通过使用Spring Cloud Bus,可以实现配置的动态刷新、事件的传播和集群中的状态同步。 下面是使用Spring Cloud Bus自定义消息总线的步骤: 1. 添加依赖:在项目的pom.xml文件中添加Spring Cloud Bus的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> ``` 2. 配置消息代理:在应用的配置文件中配置消息代理,例如使用RabbitMQ作为消息代理: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest ``` 3. 发送自定义消息:在需要发送自定义消息的地方,使用Spring Cloud Bus提供的API发送消息。例如,可以使用`/actuator/bus-refresh`端点发送刷新配置的消息: ```shell curl -X POST http://localhost:8080/actuator/bus-refresh ``` 4. 接收自定义消息:在需要接收自定义消息的地方,使用Spring Cloud Bus提供的注解和监听器来接收消息。例如,可以使用`@RefreshScope`注解来刷新配置: ```java @RefreshScope @RestController public class ConfigController { // ... } ``` 通过以上步骤,您可以使用Spring Cloud Bus自定义消息总线来实现配置的动态刷新、事件的传播和集群中的状态同步。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值