本文主要介紹了Spring Cloud Admin的使用,分享給大家,具體如下:
源碼地址:https://github.com/muxiaonong/Spring-Cloud/tree/master/cloudadmin
Admin 簡介
官方文檔:What is Spring Boot Admin?
SpringBootAdmin是一個用于管理和監控SpringBoot微服務的社區項目,可以使用客戶端注冊或者Eureka服務發現向服務端提供監控信息。
注意,服務端相當于提供UI界面,實際的監控信息由客戶端Actuator提供
通過SpringBootAdmin,你可以通過華麗大氣的界面訪問到整個微服務需要的監控信息,例如服務健康檢查信息、CPU、內存、操作系統信息等等
本篇文章使用SpringBoot 2.3.3.RELEASE、SpringCloud Hoxton.SR6、SpringBoot Admin 2.2.3版本,此外,服務注冊中心采用eureka
一、SpringCloud使用SpringBoot Admin
1.1 創建一個SpringBoot項目,命名為admin-test,引入如下依賴
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- Admin 服務 --> < dependency > < groupId >de.codecentric</ groupId > < artifactId >spring-boot-admin-starter-server</ artifactId > < version >2.2.1</ version > </ dependency > <!-- Admin 界面 --> < dependency > < groupId >de.codecentric</ groupId > < artifactId >spring-boot-admin-server-ui</ artifactId > < version >2.2.1</ version > </ dependency > |
1.2 啟動類
1
2
3
4
5
6
7
8
9
|
@SpringBootApplication @EnableAdminServer public class AdminTestApplication { public static void main(String[] args) { SpringApplication.run(AdminTestApplication. class , args); } } |
1.3 配置文件
1
2
3
4
5
6
7
8
9
|
spring.application.name=admin-test management.endpoints.jmx.exposure.include=* management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always # spring cloud access&secret config alibaba.cloud.access-key=**** alibaba.cloud.secret-key=**** |
1.4 啟動項目
輸入項目地址:http://localhost:8080/applications
二、配置郵件通知
2.1 pom
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-mail</ artifactId > </ dependency > |
2.2 郵件配置
1
2
3
4
5
6
7
8
9
10
11
|
spring.mail.host=smtp.qq.com spring.mail.username=單純QQ號 spring.mail.password=授權碼 spring.mail.properties.mail.smpt.auth=true spring.mail.properties.mail.smpt.starttls.enable=true spring.mail.properties.mail.smpt.starttls.required=true #收件郵箱 # 發件郵箱 spring.boot.admin.notify.mail.from= [email protected] |
2.3 QQ郵箱設置
找到自己的QQ郵箱
QQ郵箱 》 設置 》 賬戶 》紅框處獲取 授權碼
我們將 consumer 服務下線后,
接著我們就收到了郵件通知,告訴我們服務關閉了
三、發送釘釘群通知
找到群里面的 群設置 》 智能群助手 》 添加機器人
注意:這里的自定義關鍵詞一定要和項目的關鍵字匹配
獲取 Webhook 到項目中,這個是后面要使用到的
啟動類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import de.codecentric.boot.admin.server.config.EnableAdminServer; import de.codecentric.boot.admin.server.domain.entities.InstanceRepository; @SpringBootApplication @EnableAdminServer public class AdminApplication { public static void main(String[] args) { SpringApplication.run(AdminApplication. class , args); } @Bean public DingDingNotifier dingDingNotifier(InstanceRepository repository) { return new DingDingNotifier(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
|
import java.util.Map; import com.alibaba.fastjson.JSONObject; 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 reactor.core.publisher.Mono; public class DingDingNotifier extends AbstractStatusChangeNotifier { public DingDingNotifier(InstanceRepository repository) { super (repository); } @Override protected Mono<Void> doNotify(InstanceEvent event, Instance instance) { String serviceName = instance.getRegistration().getName(); String serviceUrl = instance.getRegistration().getServiceUrl(); String status = instance.getStatusInfo().getStatus(); Map<String, Object> details = instance.getStatusInfo().getDetails(); StringBuilder str = new StringBuilder(); str.append( "服務預警 : 【" + serviceName + "】" ); str.append( "【服務地址】" + serviceUrl); str.append( "【狀態】" + status); str.append( "【詳情】" + JSONObject.toJSONString(details)); return Mono.fromRunnable(() -> { DingDingMessageUtil.sendTextMessage(str.toString()); }); } } |
發送工具類
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
|
import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import com.alibaba.fastjson.JSONObject; public class DingDingMessageUtil { public static String access_token = "Token" ; public static void sendTextMessage(String msg) { try { Message message = new Message(); message.setMsgtype( "text" ); message.setText( new MessageInfo(msg)); URL url = new URL( "https://oapi.dingtalk.com/robot/send?access_token=" + access_token); // 建立 http 連接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput( true ); conn.setDoInput( true ); conn.setUseCaches( false ); conn.setRequestMethod( "POST" ); conn.setRequestProperty( "Charset" , "UTF-8" ); conn.setRequestProperty( "Content-Type" , "application/Json; charset=UTF-8" ); conn.connect(); OutputStream out = conn.getOutputStream(); String textMessage = JSONObject.toJSONString(message); byte [] data = textMessage.getBytes(); out.write(data); out.flush(); out.close(); InputStream in = conn.getInputStream(); byte [] data1 = new byte [in.available()]; in.read(data1); System.out.println( new String(data1)); } catch (Exception e) { e.printStackTrace(); } } } |
消息類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Message { private String msgtype; private MessageInfo text; public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this .msgtype = msgtype; } public MessageInfo getText() { return text; } public void setText(MessageInfo text) { this .text = text; } } |
1
2
3
4
5
6
7
8
9
10
11
12
|
public class MessageInfo { private String content; public MessageInfo(String content) { this .content = content; } public String getContent() { return content; } public void setContent(String content) { this .content = content; } } |
我們下線一個服務后,就可以看到釘釘群就發了消息的通知
同時,當我們啟動服務的時候,也會有消息通知我們服務啟動了
四 總結
上面就是我們對admin 健康檢查的實際應用,在企業中一般會有短信通知+釘釘群通知和郵件,感興趣的小伙伴可以去試試看,還是挺好玩的,還有一個就是微信通知,在服務號 模板消息感興趣的小伙伴可以自行去研究看看,大家加油~
到此這篇關于Spring Cloud Admin健康檢查 郵件、釘釘群通知的實現的文章就介紹到這了,更多相關Spring Cloud Admin 通知內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_14996421/article/details/108165739