spring-boot-admin笔记

╰+哭是因爲堅強的太久メ 2024-03-23 23:25 134阅读 0赞

spring-boot-admin笔记

  1. 本篇教程是基于springboot 2.3.8.RELEASE版本
  2. spring-boot-admin-dependencies 2.3.0版本

搭建spring-boot-admin的server端app

pom配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <name>dftjk</name>
  7. <description>Demo project for Spring Boot</description>
  8. <groupId>cn.mt</groupId>
  9. <artifactId>dftjk</artifactId>
  10. <version>1.0-SNAPSHOT</version>
  11. <properties>
  12. <java.version>1.8</java.version>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  15. <spring-boot.version>2.3.8.RELEASE</spring-boot.version>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>de.codecentric</groupId>
  24. <artifactId>spring-boot-admin-starter-server</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-security</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-configuration-processor</artifactId>
  33. <optional>true</optional>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.projectlombok</groupId>
  37. <artifactId>lombok</artifactId>
  38. <optional>true</optional>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-test</artifactId>
  43. <scope>test</scope>
  44. </dependency>
  45. </dependencies>
  46. <dependencyManagement>
  47. <dependencies>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-dependencies</artifactId>
  51. <version>${spring-boot.version}</version>
  52. <type>pom</type>
  53. <scope>import</scope>
  54. </dependency>
  55. <dependency>
  56. <groupId>de.codecentric</groupId>
  57. <artifactId>spring-boot-admin-dependencies</artifactId>
  58. <version>2.3.0</version>
  59. <type>pom</type>
  60. <scope>import</scope>
  61. </dependency>
  62. </dependencies>
  63. </dependencyManagement>
  64. <build>
  65. <plugins>
  66. <plugin>
  67. <groupId>org.apache.maven.plugins</groupId>
  68. <artifactId>maven-compiler-plugin</artifactId>
  69. <version>3.8.1</version>
  70. <configuration>
  71. <source>1.8</source>
  72. <target>1.8</target>
  73. <encoding>UTF-8</encoding>
  74. </configuration>
  75. </plugin>
  76. <plugin>
  77. <groupId>org.springframework.boot</groupId>
  78. <artifactId>spring-boot-maven-plugin</artifactId>
  79. <version>${spring-boot.version}</version>
  80. <configuration>
  81. <mainClass>cn.mt.dft.App</mainClass>
  82. </configuration>
  83. <executions>
  84. <execution>
  85. <id>repackage</id>
  86. <goals>
  87. <goal>repackage</goal>
  88. </goals>
  89. </execution>
  90. </executions>
  91. </plugin>
  92. </plugins>
  93. </build>
  94. </project>

启动类配置

  1. package cn.mt.dft;
  2. import de.codecentric.boot.admin.server.config.EnableAdminServer;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.scheduling.annotation.EnableScheduling;
  6. @EnableAdminServer
  7. @SpringBootApplication
  8. public class App {
  9. public static void main(String[] args) {
  10. SpringApplication.run(App.class, args);
  11. }
  12. }

内置访问认证配置

  1. package cn.mt.dft;
  2. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.security.config.Customizer;
  6. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  7. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  8. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  9. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  10. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  11. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  12. import java.util.UUID;
  13. @Configuration
  14. @EnableWebSecurity
  15. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  16. private final AdminServerProperties adminServerProperties;
  17. public SecurityConfig(AdminServerProperties adminServerProperties) {
  18. this.adminServerProperties = adminServerProperties;
  19. }
  20. @Override
  21. protected void configure(HttpSecurity http) throws Exception {
  22. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  23. successHandler.setTargetUrlParameter("redirectTo");
  24. successHandler.setDefaultTargetUrl(this.adminServerProperties.path("/"));
  25. http.authorizeRequests(
  26. (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServerProperties.path("/assets/**")).permitAll()
  27. .antMatchers(this.adminServerProperties.path("/login")).permitAll().anyRequest().authenticated()
  28. ).formLogin(
  29. (formLogin) -> formLogin.loginPage(this.adminServerProperties.path("/login")).successHandler(successHandler).and()
  30. ).logout((logout) -> logout.logoutUrl(this.adminServerProperties.path("/logout"))).httpBasic(Customizer.withDefaults())
  31. .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  32. .ignoringRequestMatchers(
  33. new AntPathRequestMatcher(this.adminServerProperties.path("/instances"),
  34. HttpMethod.POST.toString()),
  35. new AntPathRequestMatcher(this.adminServerProperties.path("/instances/*"),
  36. HttpMethod.DELETE.toString()),
  37. new AntPathRequestMatcher(this.adminServerProperties.path("/actuator/**"))
  38. ))
  39. .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
  40. }
  41. }

application.yml配置

  1. server:
  2. port: 8814
  3. compression:
  4. enabled: true # 开启Gzip压缩,响应数据超过1kb时触发gzip自动压缩,以提高前端响应时间
  5. min-response-size: 1KB
  6. servlet:
  7. session:
  8. timeout: PT30M #默认会话过期时间30分钟
  9. encoding:
  10. enabled: true
  11. charset: UTF-8
  12. force: true
  13. tomcat:
  14. uri-encoding: UTF-8
  15. # http://localhost:8811/actuator/info 查看info端点会返还自定义的info信息
  16. info:
  17. appname: dftjk
  18. ver: 1.0
  19. # http://localhost:8811/actuator 查看暴露的所有可访问的端点
  20. # http://localhost:8811/actuator/health 查看health端点
  21. management:
  22. endpoints:
  23. web:
  24. exposure:
  25. include: [health,info,mappings] #开启health,info,mappings端点, 若配置*表示暴露所有端点
  26. endpoint:
  27. health:
  28. show-details: always # health端点展示详细信息
  29. spring:
  30. security:
  31. user:
  32. name: root
  33. password: root
  34. application:
  35. name: dftjk
  36. servlet:
  37. multipart:
  38. max-file-size: 50MB #单个文件的最大上限
  39. max-request-size: 200MB #单个请求的文件总大小限制
  40. location: ${
  41. user.home}/.${
  42. spring.application.name}/tempDir
  43. logging:
  44. file:
  45. #最终的存储路径是: 系统用户目录/.应用名称/logs/端口号/spring.log
  46. path: ${
  47. user.home}/.${
  48. spring.application.name}/logs/${
  49. server.port}
  50. max-history: 7
  51. max-size: 10MB
  52. pattern:
  53. console: "%date %clr(%level) [${PID}] [%thread] [%magenta(%X{traceId})] %cyan(%logger{10}) [%file : %line] %msg%n"
  54. file: "%date %level [${PID}] [%thread] [%X{traceId}] %logger{10} [%file : %line] %msg%n"

测试启动后的访问效果

  1. 访问localhost:8814
    请添加图片描述

登录密码是 root / root

  1. 登录进入主页
    请添加图片描述

因为此时还没有服务注册到spring-boot-admin-server,所以这里暂时没有应用记录

搭建spring-boot-admin的client端app

部分pom配置

  1. <dependencies>
  2. <dependency>
  3. <groupId>de.codecentric</groupId>
  4. <artifactId>spring-boot-admin-starter-client</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-actuator</artifactId>
  9. </dependency>
  10. </dependencies>
  11. <dependencyManagement>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-dependencies</artifactId>
  16. <version>${spring-boot.version}</version>
  17. <type>pom</type>
  18. <scope>import</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>de.codecentric</groupId>
  22. <artifactId>spring-boot-admin-dependencies</artifactId>
  23. <version>2.3.0</version>
  24. <type>pom</type>
  25. <scope>import</scope>
  26. </dependency>
  27. </dependencies>
  28. </dependencyManagement>

部分application.yml配置

  1. # http://localhost:8811/actuator/info 查看info端点会返还自定义的info信息
  2. info:
  3. appname: dft
  4. ver: 1.0
  5. # http://localhost:8811/actuator 查看暴露的所有可访问的端点
  6. # http://localhost:8811/actuator/health 查看health端点
  7. management:
  8. endpoints:
  9. web:
  10. exposure:
  11. include: '*'
  12. # include: [health,info,mappings] #开启health,info,mappings端点, 若配置*表示暴露所有端点
  13. endpoint:
  14. health:
  15. show-details: always # health端点展示详细信息
  16. shutdown:
  17. enabled: true #允许优雅停机
  18. logging:
  19. file:
  20. #最终的存储路径是: 系统用户目录/.应用名称/logs/端口号/spring.log
  21. path: ${
  22. user.home}/.${
  23. spring.application.name}/logs/${
  24. server.port}
  25. max-history: 7
  26. max-size: 10MB
  27. pattern:
  28. console: "%date %clr(%level) [${PID}] [%thread] [%magenta(%X{traceId})] %cyan(%logger{10}) [%file : %line] %msg%n"
  29. file: "%date %level [${PID}] [%thread] [%X{traceId}] %logger{10} [%file : %line] %msg%n"
  30. spring:
  31. application:
  32. name: dft
  33. boot:
  34. admin:
  35. client:
  36. enabled: true #启用spring-boot-admin
  37. url: http://127.0.0.1:8814 #服务器端的访问地址
  38. username: root #服务器端的访问账号
  39. password: root #服务器端的访问密码

测试启动后的spring-boot-admin效果

  1. 启动刚刚建的client服务,然后访问spring-boot-admin服务器端-访问localhost:8814
    请添加图片描述
  2. 查看应用列表
    请添加图片描述
  3. 查看应用详情
    请添加图片描述
  4. 查看实时日志
    请添加图片描述

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读