当前位置:   article > 正文

spring boot admin,springcloud集成dingtalk_notifierautoconfiguration jvmalarm

notifierautoconfiguration jvmalarm

1.首先创建一个spring cloud 的maven项目
2.笔者创建三个项目一个是注册中心,一个是spring boot admin 客户端,还有一个是spring boot admin 服务端

admin的搭建就不多说了,参考底部源码

3.重头戏来了
首先需要在admin server端创建两个配置文件DingTalkNotifier和DingTalkNotifierConfiguration
#DingTalkNotifier代码如下:

package cn.lcc.admin.dingtalk;

import de.codecentric.boot.admin.server.domain.entities.Instance;

import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;

import de.codecentric.boot.admin.server.domain.events.InstanceEvent;

import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;

import org.springframework.context.expression.MapAccessor;

import org.springframework.expression.Expression;

import org.springframework.expression.ParserContext;

import org.springframework.expression.spel.standard.SpelExpressionParser;

import org.springframework.expression.spel.support.StandardEvaluationContext;

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

import org.springframework.http.MediaType;

import org.springframework.web.client.RestTemplate;

import reactor.core.publisher.Mono;

import java.util.HashMap;

import java.util.Map;

/**

* @Auther: liuchunchi

* @Date: 2019/9/18 09:54

* @Description:

*/

public class DingTalkNotifierextends AbstractStatusChangeNotifier {

private static final StringDEFAULT_MESSAGE ="*#{instance.registration.name}* (#{instance.id}) is *#{event.statusInfo.status}**";

    private final SpelExpressionParserparser =new SpelExpressionParser();

    private RestTemplaterestTemplate =new RestTemplate();

    private StringwebhookToken;

    private StringatMobiles;

    private Stringmsgtype ="markdown";

    private Stringtitle ="服务告警";

    private Expressionmessage;

    public DingTalkNotifier(InstanceRepository repository) {

super(repository);

        this.message =parser.parseExpression(DEFAULT_MESSAGE, ParserContext.TEMPLATE_EXPRESSION);

    }

@Override

    protected Mono doNotify(InstanceEvent event, Instance instance) {

return Mono.fromRunnable(() ->restTemplate.postForEntity(webhookToken, createMessage(event, instance),Void.class));

    }

private HttpEntity>createMessage(InstanceEvent event,Instance instance) {

Map messageJson =new HashMap<>();

        HashMap params =new HashMap<>();

        params.put("text", this.getMessage(event, instance));

        params.put("title", this.title);

        messageJson.put("atMobiles", this.atMobiles);

        messageJson.put("msgtype", this.msgtype);

        messageJson.put(this.msgtype, params);

        HttpHeaders headers =new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        return new HttpEntity<>(messageJson, headers);

    }

private StringgetAtMobilesString(String s) {

StringBuilder atMobiles =new StringBuilder();

        String[] mobiles = s.split(",");

        for (String mobile : mobiles) {

atMobiles.append("@").append(mobile);

        }

return atMobiles.toString();

    }

private StringgetMessage(InstanceEvent event,Instance instance) {

Map root =new HashMap<>();

        root.put("event", event);

        root.put("instance", instance);

        root.put("lastStatus", getLastStatus(event.getInstance()));

        StandardEvaluationContext context =new StandardEvaluationContext(root);

        context.addPropertyAccessor(new MapAccessor());

        return message.getValue(context, String.class);

    }

public void setRestTemplate(RestTemplate restTemplate) {

this.restTemplate = restTemplate;

    }

public StringgetWebhookToken() {

return webhookToken;

    }

public void setWebhookToken(String webhookToken) {

this.webhookToken = webhookToken;

    }

public StringgetAtMobiles() {

return atMobiles;

    }

public void setAtMobiles(String atMobiles) {

this.atMobiles = atMobiles;

    }

public StringgetMsgtype() {

return msgtype;

    }

public void setMsgtype(String msgtype) {

this.msgtype = msgtype;

    }

public ExpressiongetMessage() {

return message;

    }

public void setMessage(String message) {

this.message = (Expression)this.parser.parseExpression(message, ParserContext.TEMPLATE_EXPRESSION);

    }

public StringgetTitle() {

return title;

    }

public void setTitle(String title) {

this.title = title;

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203

#DingTalkNotifierConfiguration:代码如下

package cn.lcc.admin.dingtalk;

import de.codecentric.boot.admin.server.config.AdminServerNotifierAutoConfiguration;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @Auther: liuchunchi
 * @Date: 2019/9/18 09:58
 * @Description:
 */
@Configuration
@ConditionalOnProperty(
        prefix = "spring.boot.admin.notify.dingtalk",
        name = {"webhook-token"}
)
@AutoConfigureBefore({AdminServerNotifierAutoConfiguration.NotifierTriggerConfiguration.class, AdminServerNotifierAutoConfiguration.CompositeNotifierConfiguration.class})
public class DingTalkNotifierConfiguration {
    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = "spring.boot.admin.notify.dingtalk")
    public DingTalkNotifier dingTalkNotifier(InstanceRepository repository) {
        return new DingTalkNotifier(repository);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

#这里需要注意的是spring boot admin 版本不同

@AutoConfigureBefore({AdminServerNotifierAutoConfiguration.NotifierTriggerConfiguration.class, AdminServerNotifierAutoConfiguration.CompositeNotifierConfiguration.class})
  • 1

#这个地方就可能会不同
4.配置文件都已经配置好下面就需要dingtalk了,这个机器人通知从哪里来,
image.png

image.png
image.png
添加之后重命名会有
image.png

复制这个webhook
5.最后在admin-server中增加通知配置

spring:
    application:
        name: admin-server
    security:
      user:
        name: "admin"
        password: "admin"
    boot:
      admin:
        notify:
          dingtalk:
            enabled: true
            webhook-token: #刚刚复制的webhook
server:
    port: 8769

eureka:
    client:
        service-url:
            defaultZone: http://localhost:8761/eureka/
    instance:
        lease-renewal-interval-in-seconds: 10
        health-check-url-path: /actuator/health
        metadata-map:
          user.name: ${spring.security.user.name}
          user.password: ${spring.security.user.password}

management:
    endpoints:
        web:
            exposure:
                include: "*"

    endpoint:
        health:
            show-details: always


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

大功告成
git源码地址

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/125576
推荐阅读
相关标签
  

闽ICP备14008679号