SpringBoot應(yīng)用監(jiān)控帶郵件警報(bào)的實(shí)現(xiàn)示例
1.actor(client)
1.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.yl</groupId> <artifactId>ac_client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ac_client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> <spring-boot-admin.version>2.6.2</spring-boot-admin.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-dependencies</artifactId> <version>${spring-boot-admin.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--查看項(xiàng)目構(gòu)建信息的插件--> <executions> <execution> <goals> <goal>build-info</goal> </goals> </execution> </executions> </plugin> <!--查看git提交信息插件--> <plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> </plugin> </plugins> </build> </project>
1.2 application.properties
# 暴露所有的端點(diǎn)(端點(diǎn)實(shí)際上就是接口) management.endpoints.web.exposure.include=* # 開啟shutdown端點(diǎn) management.endpoint.shutdown.enabled=true # 修改所有端點(diǎn)的前綴 management.endpoints.web.base-path=/ # 修改某個(gè)端點(diǎn)的名稱 management.endpoints.web.path-mapping.beans=bs # 開啟跨域 management.endpoints.web.cors.allowed-origins=* # 允許某個(gè)請(qǐng)求跨域 management.endpoints.web.cors.allowed-methods=GET,POST # 顯示健康信息細(xì)節(jié) management.endpoint.health.show-details=when_authorized # 指定哪個(gè)角色可以看健康信息 management.endpoint.health.roles=ADMIN # 添加自定義狀態(tài)FAIL management.endpoint.health.status.order=FAIL,DOM,OUT_OF_SERVICE,UP,UNKNOWN # 自定狀態(tài)碼 management.endpoint.health.status.http-mapping.FAIL=500 # security的配置 spring.security.user.name=root spring.security.user.password=123456 spring.security.user.roles=ADMIN # 自定義應(yīng)用信息(通過(guò)配置文件的方式) info.app.encoding=@project.build.sourceEncoding@ info.app.java.source=@java.version@ info.app.java.target=@java.version@ info.author.name=admin info.author.eamil=111@163.com # 通過(guò)info查看git的所有信息 management.info.git.mode=full # 指定server的url spring.boot.admin.client.url=http://localhost:8081
1.3 security配置
package com.yl.actuator.config; import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.requestMatcher(EndpointRequest.toAnyEndpoint()) .authorizeRequests() .anyRequest().hasRole("ADMIN") .and() .httpBasic().and().csrf().disable(); } }
1.4 自定義健康信息
package com.yl.actuator.config; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; //自定義健康信息 @Component public class MyHealth implements HealthIndicator { @Override public Health health() { // return Health.status("FAIL").withDetail("message","服務(wù)器異常").build(); return Health.up().withDetail("message","一切正常..").build(); } }
1.5 自定義應(yīng)用信息
package com.yl.actuator.config; import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.context.annotation.Configuration; import java.util.HashMap; import java.util.Map; // 自定義應(yīng)用信息 @Configuration public class MyInfo implements InfoContributor { @Override public void contribute(Info.Builder builder) { Map<String,Object> map = new HashMap<>(); map.put("site1","http://www.baidu.com"); map.put("site2","http://www.baidu.com"); builder.withDetails(map).build(); } }
2.admin(server)
2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.yl</groupId> <artifactId>ac_server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ac_server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> <spring-boot-admin.version>2.6.2</spring-boot-admin.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-dependencies</artifactId> <version>${spring-boot-admin.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2 application.properties
server.port=8081 # security的用戶名和密碼要和在client那邊配的一致 spring.boot.admin.instance-auth.default-user-name=root spring.boot.admin.instance-auth.default-password=123456 # 郵件配置 spring.mail.host=smtp.qq.com spring.mail.port=465 # 郵件發(fā)送人 spring.mail.username=xxx@qq.com # 授權(quán)碼 spring.mail.password=ontqrqxpjkysfhbb spring.mail.default-encoding=utf-8 spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.debug=true spring.mail.protocol=smtp spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.test-connection=true # 郵件發(fā)送人 spring.boot.admin.notify.mail.from=xxx@qq.com # 郵件接收人 spring.boot.admin.notify.mail.to=xxxx@163.com # 忽略什么情況才不發(fā)郵件,不填代表有變化都會(huì)發(fā)郵件 spring.boot.admin.notify.discord.ignore-changes=
2.3 主程序,開啟admin server功能
package com.yl.adminserver; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAdminServer //開啟AdminServer public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } }
3.測(cè)試
3.1 同時(shí)啟動(dòng)兩個(gè)界面
3.2 訪問(wèn)health端點(diǎn)
3.3 訪問(wèn)info端點(diǎn)
3.4 ui監(jiān)控頁(yè)面,訪問(wèn)server的端口號(hào)
3.3 關(guān)掉actuator服務(wù),郵件警報(bào)
到此這篇關(guān)于SpringBoot應(yīng)用監(jiān)控帶郵件警報(bào)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot應(yīng)用監(jiān)控內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot新建項(xiàng)目pom.xml文件第一行報(bào)錯(cuò)的解決
這篇文章主要介紹了springboot新建項(xiàng)目pom.xml文件第一行報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Kotlin開發(fā)Android應(yīng)用實(shí)例詳解
這篇文章主要介紹了Kotlin開發(fā)Android應(yīng)用實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05Idea啟動(dòng)多個(gè)SpringBoot項(xiàng)目的3種最新方案
SpringBoot自帶Tomcat,直接運(yùn)行main方法里面的SpringApplication.run即可,并且訪問(wèn)時(shí)不需要帶項(xiàng)目名,這篇文章主要介紹了Idea啟動(dòng)多個(gè)SpringBoot項(xiàng)目的3種方案,需要的朋友可以參考下2023-02-02關(guān)于java中可變長(zhǎng)參數(shù)的定義及使用方法詳解
下面小編就為大家?guī)?lái)一篇關(guān)于java中可變長(zhǎng)參數(shù)的定義及使用方法詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12SpringBoot實(shí)現(xiàn)反向代理的示例代碼
本文主要介紹了SpringBoot實(shí)現(xiàn)反向代理的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06任何Bean通過(guò)實(shí)現(xiàn)ProxyableBeanAccessor接口即可獲得動(dòng)態(tài)靈活的獲取代理對(duì)象或原生對(duì)象的能力(最新推
這篇文章主要介紹了任何Bean通過(guò)實(shí)現(xiàn)ProxyableBeanAccessor接口即可獲得動(dòng)態(tài)靈活的獲取代理對(duì)象或原生對(duì)象的能力,通過(guò)示例代碼看到,借助ProxyableBeanAccessor接口默認(rèn)實(shí)現(xiàn)的getReal、getProxy、selfAs方法,很靈活的按需獲取代理或非代理對(duì)象,需要的朋友可以參考下2024-02-02深入理解Java設(shè)計(jì)模式之職責(zé)鏈模式
這篇文章主要介紹了JAVA設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解2021-11-11