SpringBoot应用监控(带邮件警报)

x33g5p2x  于2022-02-24 转载在 Spring  
字(9.1k)|赞(0)|评价(0)|浏览(432)

1.actor(client)

1.1 pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.3</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.yl</groupId>
  12. <artifactId>ac_client</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>ac_client</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>11</java.version>
  18. <spring-boot-admin.version>2.6.2</spring-boot-admin.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-actuator</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-security</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>de.codecentric</groupId>
  35. <artifactId>spring-boot-admin-starter-client</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. </dependency>
  42. </dependencies>
  43. <dependencyManagement>
  44. <dependencies>
  45. <dependency>
  46. <groupId>de.codecentric</groupId>
  47. <artifactId>spring-boot-admin-dependencies</artifactId>
  48. <version>${spring-boot-admin.version}</version>
  49. <type>pom</type>
  50. <scope>import</scope>
  51. </dependency>
  52. </dependencies>
  53. </dependencyManagement>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. <!--查看项目构建信息的插件-->
  60. <executions>
  61. <execution>
  62. <goals>
  63. <goal>build-info</goal>
  64. </goals>
  65. </execution>
  66. </executions>
  67. </plugin>
  68. <!--查看git提交信息插件-->
  69. <plugin>
  70. <groupId>pl.project13.maven</groupId>
  71. <artifactId>git-commit-id-plugin</artifactId>
  72. </plugin>
  73. </plugins>
  74. </build>
  75. </project>

1.2 application.properties

  1. # 暴露所有的端点(端点实际上就是接口)
  2. management.endpoints.web.exposure.include=*
  3. # 开启shutdown端点
  4. management.endpoint.shutdown.enabled=true
  5. # 修改所有端点的前缀
  6. management.endpoints.web.base-path=/
  7. # 修改某个端点的名称
  8. management.endpoints.web.path-mapping.beans=bs
  9. # 开启跨域
  10. management.endpoints.web.cors.allowed-origins=*
  11. # 允许某个请求跨域
  12. management.endpoints.web.cors.allowed-methods=GET,POST
  13. # 显示健康信息细节
  14. management.endpoint.health.show-details=when_authorized
  15. # 指定哪个角色可以看健康信息
  16. management.endpoint.health.roles=ADMIN
  17. # 添加自定义状态FAIL
  18. management.endpoint.health.status.order=FAIL,DOM,OUT_OF_SERVICE,UP,UNKNOWN
  19. # 自定状态码
  20. management.endpoint.health.status.http-mapping.FAIL=500
  21. # security的配置
  22. spring.security.user.name=root
  23. spring.security.user.password=123456
  24. spring.security.user.roles=ADMIN
  25. # 自定义应用信息(通过配置文件的方式)
  26. info.app.encoding=@project.build.sourceEncoding@
  27. info.app.java.source=@java.version@
  28. info.app.java.target=@java.version@
  29. info.author.name=admin
  30. info.author.eamil=111@163.com
  31. # 通过info查看git的所有信息
  32. management.info.git.mode=full
  33. # 指定server的url
  34. spring.boot.admin.client.url=http://localhost:8081

1.3 security配置

  1. package com.yl.actuator.config;
  2. import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. @Configuration
  7. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  8. @Override
  9. protected void configure(HttpSecurity http) throws Exception {
  10. http.requestMatcher(EndpointRequest.toAnyEndpoint())
  11. .authorizeRequests()
  12. .anyRequest().hasRole("ADMIN")
  13. .and()
  14. .httpBasic().and().csrf().disable();
  15. }
  16. }

1.4 自定义健康信息

  1. package com.yl.actuator.config;
  2. import org.springframework.boot.actuate.health.Health;
  3. import org.springframework.boot.actuate.health.HealthIndicator;
  4. import org.springframework.stereotype.Component;
  5. //自定义健康信息
  6. @Component
  7. public class MyHealth implements HealthIndicator {
  8. @Override
  9. public Health health() {
  10. // return Health.status("FAIL").withDetail("message","服务器异常").build();
  11. return Health.up().withDetail("message","一切正常..").build();
  12. }
  13. }

1.5 自定义应用信息

  1. package com.yl.actuator.config;
  2. import org.springframework.boot.actuate.info.Info;
  3. import org.springframework.boot.actuate.info.InfoContributor;
  4. import org.springframework.context.annotation.Configuration;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. // 自定义应用信息
  8. @Configuration
  9. public class MyInfo implements InfoContributor {
  10. @Override
  11. public void contribute(Info.Builder builder) {
  12. Map<String,Object> map = new HashMap<>();
  13. map.put("site1","http://www.baidu.com");
  14. map.put("site2","http://www.baidu.com");
  15. builder.withDetails(map).build();
  16. }
  17. }

2.admin(server)

2.1 pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.3</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.yl</groupId>
  12. <artifactId>ac_server</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>ac_server</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>11</java.version>
  18. <spring-boot-admin.version>2.6.2</spring-boot-admin.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-mail</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>de.codecentric</groupId>
  31. <artifactId>spring-boot-admin-starter-server</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. </dependencies>
  39. <dependencyManagement>
  40. <dependencies>
  41. <dependency>
  42. <groupId>de.codecentric</groupId>
  43. <artifactId>spring-boot-admin-dependencies</artifactId>
  44. <version>${spring-boot-admin.version}</version>
  45. <type>pom</type>
  46. <scope>import</scope>
  47. </dependency>
  48. </dependencies>
  49. </dependencyManagement>
  50. <build>
  51. <plugins>
  52. <plugin>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-maven-plugin</artifactId>
  55. </plugin>
  56. </plugins>
  57. </build>
  58. </project>

2.2 application.properties

  1. server.port=8081
  2. # security的用户名和密码要和在client那边配的一致
  3. spring.boot.admin.instance-auth.default-user-name=root
  4. spring.boot.admin.instance-auth.default-password=123456
  5. # 邮件配置
  6. spring.mail.host=smtp.qq.com
  7. spring.mail.port=465
  8. # 邮件发送人
  9. spring.mail.username=xxx@qq.com
  10. # 授权码
  11. spring.mail.password=ontqrqxpjkysfhbb
  12. spring.mail.default-encoding=utf-8
  13. spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
  14. spring.mail.properties.mail.debug=true
  15. spring.mail.protocol=smtp
  16. spring.mail.properties.mail.smtp.auth=true
  17. spring.mail.properties.mail.smtp.ssl.enable=true
  18. spring.mail.properties.mail.smtp.starttls.enable=true
  19. spring.mail.test-connection=true
  20. # 邮件发送人
  21. spring.boot.admin.notify.mail.from=xxx@qq.com
  22. # 邮件接收人
  23. spring.boot.admin.notify.mail.to=xxxx@163.com
  24. # 忽略什么情况才不发邮件,不填代表有变化都会发邮件
  25. spring.boot.admin.notify.discord.ignore-changes=

2.3 主程序,开启admin server功能

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

3.测试

3.1 同时启动两个界面

3.2 访问health端点

3.3 访问info端点

3.4 ui监控页面,访问server的端口号

3.3 关掉actuator服务,邮件警报

相关文章