当前位置:   article > 正文

springboot2.x系列十四(分布式微服务监控中心SpringBootAdmin)

springboot2.x系列十四(分布式微服务监控中心SpringBootAdmin)

前面我们讲了actuator,但是Spring Boot Actuator只是提供了一个个的接口,需要我们自行集成到监控程序中。Admin-UI基于actuator实现,能够返回界面展示监控信息

SpringBootAdmin用来管理和监控SpringBoot应用程序,它利用spring-boot-starter-actuator提供的功能,将各个微服务的状态整合到一起,并提供良好的界面查看支持,并且能够动态的修改实例日志级别。SpringBootAdmin分为server端和client端,server端可查看各个微服务的状态,client端将微服务注册到server端。github源码地址:https://github.com/codecentric/spring-boot-admin

Admin-UI-Server

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. <groupId>com.example</groupId>
  6. <artifactId>admin-ui-server</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>admin-ui-server</name>
  9. <description>Demo project for Spring Boot</description>
  10. <properties>
  11. <java.version>1.8</java.version>
  12. </properties>
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>2.2.1.RELEASE</version>
  17. </parent>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. <exclusions>
  23. <exclusion>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-tomcat</artifactId>
  26. </exclusion>
  27. </exclusions>
  28. </dependency>
  29. <dependency>
  30. <groupId>de.codecentric</groupId>
  31. <artifactId>spring-boot-admin-server</artifactId>
  32. <version>2.2.1</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>de.codecentric</groupId>
  36. <artifactId>spring-boot-admin-server-ui</artifactId>
  37. <version>2.2.1</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-security</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-jetty</artifactId>
  46. </dependency>
  47. </dependencies>
  48. <build>
  49. <plugins>
  50. <plugin>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-maven-plugin</artifactId>
  53. </plugin>
  54. </plugins>
  55. </build>
  56. </project>

application.properties

  1. server.port=8081
  2. spring.application.name=admin-server
  3. #监控中心账号密码
  4. spring.security.user.name=admin
  5. spring.security.user.password=admin

配置security验证

  1. package com.example.adminuiserver;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  6. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  7. @Configuration
  8. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  9. private final String contextPath;
  10. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  11. this.contextPath = adminServerProperties.getContextPath();
  12. }
  13. @Override
  14. protected void configure(HttpSecurity http) throws Exception {
  15. // 跨域设置,SpringBootAdmin客户端通过instances注册,见InstancesController
  16. http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  17. .ignoringAntMatchers(contextPath + "/instances");
  18. http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 静态资源
  19. http.authorizeRequests().anyRequest().authenticated(); // 所有请求必须通过认证
  20. // 整合spring-boot-admin-server-ui
  21. http.formLogin().loginPage("/login").permitAll();
  22. http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
  23. // 启用basic认证,SpringBootAdmin客户端使用的是basic认证
  24. http.httpBasic();
  25. }
  26. }

启动类 

  1. package com.example.adminuiserver;
  2. import de.codecentric.boot.admin.server.config.EnableAdminServer;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.context.annotation.Configuration;
  7. @EnableAdminServer
  8. @SpringBootApplication
  9. public class AdminUiServerApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(AdminUiServerApplication.class, args);
  12. }
  13. }

 

Admin-UI-Client

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. <groupId>com.example</groupId>
  6. <artifactId>admin-ui-client</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>admin-ui-client</name>
  9. <description>Demo project for Spring Boot</description>
  10. <properties>
  11. <java.version>1.8</java.version>
  12. </properties>
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>2.0.0.RELEASE</version>
  17. </parent>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>de.codecentric</groupId>
  25. <artifactId>spring-boot-admin-starter-client</artifactId>
  26. <version>2.0.0</version>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <plugins>
  31. <plugin>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-maven-plugin</artifactId>
  34. </plugin>
  35. </plugins>
  36. </build>
  37. </project>

 application.properties

  1. server.port=8080
  2. spring.application.name=admin-client
  3. management.endpoints.web.exposure.include=*
  4. management.endpoint.health.show-details=always
  5. spring.boot.admin.client.url=http://127.0.0.1:8081
  6. #spring.boot.admin.client.instance.metadata.user.name=admin
  7. #spring.boot.admin.client.instance.metadata.user.password=admin
  8. spring.boot.admin.client.username=admin
  9. spring.boot.admin.client.password=admin

启动类

  1. package com.example.adminuiclient;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class AdminUiClientApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(AdminUiClientApplication.class, args);
  8. }
  9. }

运行

两个项目都进行启动后  

访问server服务 http://localhost:8081/login进行登录

上面我们配置的账号和密码都是 admin

登录后 

 展示了服务的运行信息

点击应用墙进去后 可以看到各类运行参数信息

通知提醒

SpringBootAdmin提供了多种通知功能,也可以自定义通知提醒。这里我们验证一下邮件通知功能

  1)服务端poxm.xml增加配置

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

  2)增加配置

  1. spring.mail.host=smtp.qq.com
  2. spring.mail.username=xxxx@foxmail.com
  3. spring.mail.password=xxxx
  4. spring.boot.admin.notify.mail.from=xxxx@foxmail.com
  5. spring.boot.admin.notify.mail.to=yyyy@foxmail.com

  3)效果

当客户端实例停掉后,我们会收到邮件提醒。

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/125598
推荐阅读
相关标签
  

闽ICP备14008679号