1.actor(client)
1.1 pom.xml
|
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://HdhCmsTestw3.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 > <!--查看项目构建信息的插件--> < 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
|
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 |
# 暴露所有的端点(端点实际上就是接口) management.endpoints.web.exposure.include=* # 开启shutdown端点 management.endpoint.shutdown.enabled=true # 修改所有端点的前缀 management.endpoints.web.base-path=/ # 修改某个端点的名称 management.endpoints.web.path-mapping.beans=bs # 开启跨域 management.endpoints.web.cors.allowed-origins=* # 允许某个请求跨域 management.endpoints.web.cors.allowed-methods=GET,POST # 显示健康信息细节 management.endpoint.health.show-details=when_authorized # 指定哪个角色可以看健康信息 management.endpoint.health.roles=ADMIN # 添加自定义状态FAIL management.endpoint.health.status.order=FAIL,DOM,OUT_OF_SERVICE,UP,UNKNOWN # 自定状态码 management.endpoint.health.status.http-mapping.FAIL=500
# security的配置 spring.security.user.name=root spring.security.user.password=123456 spring.security.user.roles=ADMIN
# 自定义应用信息(通过配置文件的方式) 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测试数据
# 通过info查看git的所有信息 management.info.git.mode=full
# 指定server的url spring.boot.admin.client.url=http://localhost:8081 |
1.3 security配置
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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 自定义健康信息
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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","服务器异常").build(); return Health.up().withDetail( "message" , "一切正常.." ).build(); } } |
1.5 自定义应用信息
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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;
// 自定义应用信息 @Configuration public class MyInfo implements InfoContributor { @Override public void contribute(Info.Builder builder) { Map<String,Object> map = new HashMap<>(); map.put( "site1" , "http://HdhCmsTestbaidu测试数据" ); map.put( "site2" , "http://HdhCmsTestbaidu测试数据" ); builder.withDetails(map).build(); } } |
2.admin(server)
2.1 pom.xml
|
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://HdhCmsTestw3.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
|
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 |
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测试数据 spring.mail.port=465 # 邮件发送人 spring.mail.username=xxx@qq测试数据 # 授权码 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
# 邮件发送人 spring.boot.admin.notify.mail.from=xxx@qq测试数据 # 邮件接收人 spring.boot.admin.notify.mail.to=xxxx@163测试数据 # 忽略什么情况才不发邮件,不填代表有变化都会发邮件 spring.boot.admin.notify.discord.ignore-changes= |
2.3 主程序,开启admin server功能
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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.测试
3.1 同时启动两个界面
3.2 访问health端点
3.3 访问info端点
3.4 ui监控页面,访问server的端口号
3.3 关掉actuator服务,邮件警报
到此这篇关于SpringBoot应用监控带邮件警报的实现示例的文章就介绍到这了,更多相关SpringBoot应用监控内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/weixin_41359273/article/details/123079450
查看更多关于SpringBoot应用监控带邮件警报的实现示例的详细内容...