spring boot admin ,spring cloud 集成dingtalk

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

admin的搭建就不多说了,参考底部源码 https://github.com/liuchunchiuse/springCloud

3.重头戏来了
首先需要创建两个配置文件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;

    }

}

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);
    }

}

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

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

这个地方就可能会不同

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


大功告成
源码地址 https://github.com/liuchunchiuse/springCloud

你可能感兴趣的:(spring boot admin ,spring cloud 集成dingtalk)