Spring Cloud Admin健康檢查 郵件、釘釘群通知的實(shí)現(xiàn)
本文主要介紹了Spring Cloud Admin的使用,分享給大家,具體如下:
源碼地址:https://github.com/muxiaonong/Spring-Cloud/tree/master/cloudadmin
Admin 簡介
官方文檔:What is Spring Boot Admin?
SpringBootAdmin是一個用于管理和監(jiān)控SpringBoot微服務(wù)的社區(qū)項目,可以使用客戶端注冊或者Eureka服務(wù)發(fā)現(xiàn)向服務(wù)端提供監(jiān)控信息。
注意,服務(wù)端相當(dāng)于提供UI界面,實(shí)際的監(jiān)控信息由客戶端Actuator提供
通過SpringBootAdmin,你可以通過華麗大氣的界面訪問到整個微服務(wù)需要的監(jiān)控信息,例如服務(wù)健康檢查信息、CPU、內(nèi)存、操作系統(tǒng)信息等等
本篇文章使用SpringBoot 2.3.3.RELEASE、SpringCloud Hoxton.SR6、SpringBoot Admin 2.2.3版本,此外,服務(wù)注冊中心采用eureka
一、SpringCloud使用SpringBoot Admin
1.1 創(chuàng)建一個SpringBoot項目,命名為admin-test,引入如下依賴
<!-- Admin 服務(wù) --> <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 啟動類
@SpringBootApplication @EnableAdminServer public class AdminTestApplication { public static void main(String[] args) { SpringApplication.run(AdminTestApplication.class, args); } }
1.3 配置文件
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
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.2 郵件配置
spring.mail.host=smtp.qq.com spring.mail.username=單純QQ號 spring.mail.password=授權(quán)碼 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.to=xxxx@qq.com # 發(fā)件郵箱 spring.boot.admin.notify.mail.from= xxxx@qq.com
2.3 QQ郵箱設(shè)置
找到自己的QQ郵箱
QQ郵箱 》 設(shè)置 》 賬戶 》紅框處獲取 授權(quán)碼
我們將 consumer 服務(wù)下線后,
接著我們就收到了郵件通知,告訴我們服務(wù)關(guān)閉了
三、發(fā)送釘釘群通知
找到群里面的 群設(shè)置 》 智能群助手 》 添加機(jī)器人
注意:這里的自定義關(guān)鍵詞一定要和項目的關(guān)鍵字匹配
獲取 Webhook 到項目中,這個是后面要使用到的
啟動類:
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); } }
通知類:
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("服務(wù)預(yù)警 : 【" + serviceName + "】"); str.append("【服務(wù)地址】" + serviceUrl); str.append("【狀態(tài)】" + status); str.append("【詳情】" + JSONObject.toJSONString(details)); return Mono.fromRunnable(() -> { DingDingMessageUtil.sendTextMessage(str.toString()); }); } }
發(fā)送工具類
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(); } } }
消息類:
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; } }
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; } }
我們下線一個服務(wù)后,就可以看到釘釘群就發(fā)了消息的通知
同時,當(dāng)我們啟動服務(wù)的時候,也會有消息通知我們服務(wù)啟動了
四 總結(jié)
上面就是我們對admin 健康檢查的實(shí)際應(yīng)用,在企業(yè)中一般會有短信通知+釘釘群通知和郵件,感興趣的小伙伴可以去試試看,還是挺好玩的,還有一個就是微信通知,在服務(wù)號 模板消息感興趣的小伙伴可以自行去研究看看,大家加油~
到此這篇關(guān)于Spring Cloud Admin健康檢查 郵件、釘釘群通知的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring Cloud Admin 通知內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot實(shí)現(xiàn)執(zhí)行sql語句打印到控制臺
這篇文章主要介紹了springboot實(shí)現(xiàn)執(zhí)行sql語句打印到控制臺的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Seata?AT獲取數(shù)據(jù)表元數(shù)據(jù)源碼詳解
這篇文章主要為大家介紹了Seata?AT獲取數(shù)據(jù)表元數(shù)據(jù)源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11java?數(shù)組越界判斷和獲取數(shù)組長度的實(shí)現(xiàn)方式
這篇文章主要介紹了java?數(shù)組越界判斷和獲取數(shù)組長度的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot學(xué)習(xí)之全局異常處理設(shè)置(返回JSON)
本篇文章主要介紹了SpringBoot學(xué)習(xí)之全局異常處理設(shè)置(返回JSON),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02Java中關(guān)于char類型變量能夠輸出中文的問題
這篇文章主要介紹了Java中關(guān)于char類型變量能夠輸出中文的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12SpringSecurity添加圖形驗證碼認(rèn)證實(shí)現(xiàn)
本文主要介紹了SpringSecurity添加圖形驗證碼認(rèn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08AsyncHttpClient exception異常源碼流程解析
這篇文章主要為大家介紹了AsyncHttpClient的exception源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12