Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。Spring Boot Admin 提供了很多功能,如显示 name、id 和 version,显示在线状态,Loggers 的日志级别管理,Threads 线程管理,Environment 管理等。

在 Spring Boot 项目中,Spring Boot Admin 作为 Server 端,其他的微服务应用作为 Client 端。

1. 基本使用

1.1 创建Admin服务器端

创建新的spring boot项目, 并引入依赖.
maven方式

  1. <!-- Admin 服务 -->
  2. <dependency>
  3. <groupId>de.codecentric</groupId>
  4. <artifactId>spring-boot-admin-starter-server</artifactId>
  5. </dependency>
  6. <!-- Admin 界面 -->
  7. <dependency>
  8. <groupId>de.codecentric</groupId>
  9. <artifactId>spring-boot-admin-server-ui</artifactId>
  10. </dependency>

gradle方式

  1. //Admin 服务
  2. implementation 'de.codecentric:spring-boot-admin-starter-server'
  3. //Admin 界面
  4. implementation 'de.codecentric:spring-boot-admin-server-ui'

引入依赖

  1. server端:
  2. <!-- Admin 服务 -->
  3. <dependency>
  4. <groupId>de.codecentric</groupId>
  5. <artifactId>spring-boot-admin-starter-server</artifactId>
  6. </dependency>
  7. <!-- Admin 界面 -->
  8. <dependency>
  9. <groupId>de.codecentric</groupId>
  10. <artifactId>spring-boot-admin-server-ui</artifactId>
  11. </dependency>

启动类

  1. package com.mashibing.admin;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import de.codecentric.boot.admin.server.config.EnableAdminServer;
  5. @SpringBootApplication
  6. @EnableAdminServer
  7. public class AdminApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(AdminApplication.class, args);
  10. }
  11. }

微服务端

  1. 微服务端:
  2. <!-- Admin 服务 -->
  3. <dependency>
  4. <groupId>de.codecentric</groupId>
  5. <artifactId>spring-boot-admin-starter-client</artifactId>
  6. <version>2.2.1</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-actuator</artifactId>
  11. </dependency>

配置

  1. management.endpoints.web.exposure.include=*
  2. management.endpoint.health.show-details=always
  3. spring.boot.admin.client.url=http://localhost:8080

邮件通知

  1. pom

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-mail</artifactId>
    4. </dependency>
  2. yml

    1. spring:
    2. application:
    3. name: cloud-admin
    4. security:
    5. user:
    6. name: root
    7. password: root
    8. # 邮件设置
    9. mail:
    10. host: smtp.qq.com
    11. username: 单纯QQ
    12. password: xxxxxxx授权码
    13. properties:
    14. mail:
    15. smpt:
    16. auth: true
    17. starttls:
    18. enable: true
    19. required: true
    20. #收件邮箱
    21. spring.boot.admin.notify.mail.to: 2634982208@qq.com
    22. # 发件邮箱
    23. spring.boot.admin.notify.mail.from: xxxxxxx@qq.com

钉钉群通知

启动类

  1. package com.mashibing.admin;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.Bean;
  5. import de.codecentric.boot.admin.server.config.EnableAdminServer;
  6. import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
  7. @SpringBootApplication
  8. @EnableAdminServer
  9. public class AdminApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(AdminApplication.class, args);
  12. }
  13. @Bean
  14. public DingDingNotifier dingDingNotifier(InstanceRepository repository) {
  15. return new DingDingNotifier(repository);
  16. }
  17. }

通知类

  1. package com.mashibing.admin;
  2. import java.util.Map;
  3. import com.alibaba.fastjson.JSONObject;
  4. import de.codecentric.boot.admin.server.domain.entities.Instance;
  5. import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
  6. import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
  7. import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
  8. import reactor.core.publisher.Mono;
  9. public class DingDingNotifier extends AbstractStatusChangeNotifier {
  10. public DingDingNotifier(InstanceRepository repository) {
  11. super(repository);
  12. }
  13. @Override
  14. protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
  15. String serviceName = instance.getRegistration().getName();
  16. String serviceUrl = instance.getRegistration().getServiceUrl();
  17. String status = instance.getStatusInfo().getStatus();
  18. Map<String, Object> details = instance.getStatusInfo().getDetails();
  19. StringBuilder str = new StringBuilder();
  20. str.append("服务预警 : 【" + serviceName + "】");
  21. str.append("【服务地址】" + serviceUrl);
  22. str.append("【状态】" + status);
  23. str.append("【详情】" + JSONObject.toJSONString(details));
  24. return Mono.fromRunnable(() -> {
  25. DingDingMessageUtil.sendTextMessage(str.toString());
  26. });
  27. }
  28. }

发送工具类

  1. package com.mashibing.admin;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import com.alibaba.fastjson.JSONObject;
  7. public class DingDingMessageUtil {
  8. public static String access_token = "Token";
  9. public static void sendTextMessage(String msg) {
  10. try {
  11. Message message = new Message();
  12. message.setMsgtype("text");
  13. message.setText(new MessageInfo(msg));
  14. URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
  15. // 建立 http 连接
  16. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  17. conn.setDoOutput(true);
  18. conn.setDoInput(true);
  19. conn.setUseCaches(false);
  20. conn.setRequestMethod("POST");
  21. conn.setRequestProperty("Charset", "UTF-8");
  22. conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
  23. conn.connect();
  24. OutputStream out = conn.getOutputStream();
  25. String textMessage = JSONObject.toJSONString(message);
  26. byte[] data = textMessage.getBytes();
  27. out.write(data);
  28. out.flush();
  29. out.close();
  30. InputStream in = conn.getInputStream();
  31. byte[] data1 = new byte[in.available()];
  32. in.read(data1);
  33. System.out.println(new String(data1));
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }

消息类

  1. package com.mashibing.admin;
  2. public class Message {
  3. private String msgtype;
  4. private MessageInfo text;
  5. public String getMsgtype() {
  6. return msgtype;
  7. }
  8. public void setMsgtype(String msgtype) {
  9. this.msgtype = msgtype;
  10. }
  11. public MessageInfo getText() {
  12. return text;
  13. }
  14. public void setText(MessageInfo text) {
  15. this.text = text;
  16. }
  17. }
  18. package com.mashibing.admin;
  19. public class MessageInfo {
  20. private String content;
  21. public MessageInfo(String content) {
  22. this.content = content;
  23. }
  24. public String getContent() {
  25. return content;
  26. }
  27. public void setContent(String content) {
  28. this.content = content;
  29. }
  30. }

微信通知

服务号 模板消息