• Spring Boot Admin


    一、简介

    官方文档  http://codecentric.github.io/spring-boot-admin/2.0.2/

    Spring Boot Admin是一个管理和监控Spring Boot应用程序的开源软件。每个应用都认为是一个客户端,通过HTTP或者服务注册发现Spring Cloud(Eureka、Consul等等)注册到admin server中进行展示,Spring Boot Admin UI部分使用AngularJs将数据展示在前端。

    Spring Boot Admin是一个针对spring-boot的actuator接口进行UI美化封装的监控工具。它可以:在列表中浏览所有被监控spring-boot项目的基本信息,详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改logger的level。

    二、环境

    一个eureka项目

    一个Spring Boot Admin项目

    三、创建eureka项目

    (1)创建项目

    创建一个spring boot项目

    (2)依赖

    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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>com.abc</groupId>
    6. <artifactId>00-eurekaserver-8000</artifactId>
    7. <version>0.0.1-SNAPSHOT</version>
    8. <packaging>jar</packaging>
    9. <parent>
    10. <groupId>org.springframework.boot</groupId>
    11. <artifactId>spring-boot-starter-parent</artifactId>
    12. <version>2.1.7.RELEASE</version>
    13. <relativePath/> <!-- lookup parent from repository -->
    14. </parent>
    15. <properties>
    16. <java.version>1.8</java.version>
    17. <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    18. </properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.cloud</groupId>
    22. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    23. </dependency>
    24. <dependency>
    25. <groupId>org.springframework.boot</groupId>
    26. <artifactId>spring-boot-starter-test</artifactId>
    27. <scope>test</scope>
    28. </dependency>
    29. <!--热部署依赖-->
    30. <dependency>
    31. <groupId>org.springframework.boot</groupId>
    32. <artifactId>spring-boot-devtools</artifactId>
    33. <optional>true</optional>
    34. </dependency>
    35. </dependencies>
    36. <dependencyManagement>
    37. <dependencies>
    38. <dependency>
    39. <groupId>org.springframework.cloud</groupId>
    40. <artifactId>spring-cloud-dependencies</artifactId>
    41. <version>${spring-cloud.version}</version>
    42. <type>pom</type>
    43. <scope>import</scope>
    44. </dependency>
    45. </dependencies>
    46. </dependencyManagement>
    47. <build>
    48. <plugins>
    49. <plugin>
    50. <groupId>org.springframework.boot</groupId>
    51. <artifactId>spring-boot-maven-plugin</artifactId>
    52. </plugin>
    53. </plugins>
    54. </build>
    55. </project>

    (3)application.yml配置

    1. server:
    2. port: 8000
    3. eureka:
    4. instance:
    5. hostname: localhost # 指定Eureka主机
    6. client:
    7. register-with-eureka: false # 指定当前主机是否向Eureka服务器进行注册
    8. fetch-registry: false # 指定当前主机是否要从Eurka服务器下载服务注册列表
    9. service-url: # 服务暴露地址
    10. defaultZone: http://localhost:8000/eureka
    11. # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
    12. # server:
    13. # enable-self-preservation: false # 关闭自我保护

    (4)启动类

    1. package com.abc.eureka;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    5. @SpringBootApplication
    6. @EnableEurekaServer // 开启Eureka服务
    7. public class EurekaServerApplication {
    8. public static void main(String[] args) {
    9. SpringApplication.run(EurekaServerApplication.class, args);
    10. }
    11. }

    四、创建Spring Boot Admin项目

    (1)创建项目

    创建一个spring boot项目

    (2)依赖

    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.1.7.RELEASE</version>
    9. <relativePath/> <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>com.zzq</groupId>
    12. <artifactId>spring-boot-admin</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. <name>spring-boot-admin</name>
    15. <description>Demo project for Spring Boot</description>
    16. <properties>
    17. <java.version>1.8</java.version>
    18. <spring-boot-admin.version>2.1.5</spring-boot-admin.version>
    19. </properties>
    20. <dependencies>
    21. <dependency>
    22. <groupId>de.codecentric</groupId>
    23. <artifactId>spring-boot-admin-starter-server</artifactId>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.springframework.cloud</groupId>
    27. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    28. </dependency>
    29. <dependency>
    30. <groupId>de.codecentric</groupId>
    31. <artifactId>spring-boot-admin-starter-client</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. <dependency>
    39. <groupId>org.springframework.cloud</groupId>
    40. <artifactId>spring-cloud-commons</artifactId>
    41. <version>2.1.2.RELEASE</version>
    42. </dependency>
    43. <!--eureka客户端依赖-->
    44. <dependency>
    45. <groupId>org.springframework.cloud</groupId>
    46. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    47. <version>2.1.2.RELEASE</version>
    48. </dependency>
    49. <!--热部署依赖-->
    50. <dependency>
    51. <groupId>org.springframework.boot</groupId>
    52. <artifactId>spring-boot-devtools</artifactId>
    53. <optional>true</optional>
    54. </dependency>
    55. <dependency>
    56. <groupId>org.springframework.boot</groupId>
    57. <artifactId>spring-boot-starter-security</artifactId>
    58. </dependency>
    59. <dependency>
    60. <groupId>org.springframework.boot</groupId>
    61. <artifactId>spring-boot-starter-web</artifactId>
    62. <exclusions>
    63. <exclusion>
    64. <groupId>org.springframework.boot</groupId>
    65. <artifactId>spring-boot-starter-tomcat</artifactId>
    66. </exclusion>
    67. </exclusions>
    68. </dependency>
    69. <dependency>
    70. <groupId>org.springframework.boot</groupId>
    71. <artifactId>spring-boot-starter-jetty</artifactId>
    72. </dependency>
    73. </dependencies>
    74. <dependencyManagement>
    75. <dependencies>
    76. <dependency>
    77. <groupId>de.codecentric</groupId>
    78. <artifactId>spring-boot-admin-dependencies</artifactId>
    79. <version>${spring-boot-admin.version}</version>
    80. <type>pom</type>
    81. <scope>import</scope>
    82. </dependency>
    83. </dependencies>
    84. </dependencyManagement>
    85. <build>
    86. <plugins>
    87. <plugin>
    88. <groupId>org.springframework.boot</groupId>
    89. <artifactId>spring-boot-maven-plugin</artifactId>
    90. </plugin>
    91. </plugins>
    92. </build>
    93. </project>

    (3)application.yml配置

    1. server:
    2. port: 9006
    3. spring:
    4. application:
    5. name: admin-server
    6. security:
    7. user:
    8. name: "admin"
    9. password: "admin"
    10. eureka:
    11. client:
    12. service-url:
    13. defaultZone: http://localhost:8000/eureka
    14. instance:
    15. prefer-ip-address: true
    16. ip-address: 127.0.0.1
    17. instance-id: ${eureka.instance.ip-address}.${server.port}
    18. lease-renewal-interval-in-seconds: 3
    19. lease-expiration-duration-in-seconds: 10
    20. health-check-url-path: /actuator/health
    21. metadata-map:
    22. user.name: "admin"
    23. user.password: "admin"
    24. management:
    25. endpoints:
    26. web:
    27. exposure:
    28. include: "*" #监控自身
    29. endpoint:
    30. health:
    31. show-details: ALWAYS

    (3)启动类

    1. package com.zzq.springbootadmin;
    2. import de.codecentric.boot.admin.server.config.AdminServerProperties;
    3. import de.codecentric.boot.admin.server.config.EnableAdminServer;
    4. import org.springframework.boot.SpringApplication;
    5. import org.springframework.boot.autoconfigure.SpringBootApplication;
    6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    7. import org.springframework.context.annotation.Configuration;
    8. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    9. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    10. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
    11. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
    12. @SpringBootApplication
    13. @EnableAdminServer
    14. @EnableEurekaClient
    15. public class SpringBootAdminApplication {
    16. public static void main(String[] args) {
    17. SpringApplication.run(SpringBootAdminApplication.class, args);
    18. }
    19. @Configuration
    20. public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    21. private final String adminContextPath;
    22. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
    23. this.adminContextPath = adminServerProperties.getContextPath();
    24. }
    25. @Override
    26. protected void configure(HttpSecurity http) throws Exception {
    27. // @formatter:off
    28. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
    29. successHandler.setTargetUrlParameter("redirectTo");
    30. successHandler.setDefaultTargetUrl(adminContextPath + "/");
    31. http.authorizeRequests()
    32. .antMatchers(adminContextPath + "/assets/**").permitAll()
    33. .antMatchers(adminContextPath + "/login").permitAll()
    34. .anyRequest().authenticated()
    35. .and()
    36. .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
    37. .logout().logoutUrl(adminContextPath + "/logout").and()
    38. .httpBasic().and()
    39. .csrf()
    40. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
    41. .ignoringAntMatchers(
    42. adminContextPath + "/instances",
    43. adminContextPath + "/**"
    44. );
    45. // @formatter:on
    46. }
    47. }
    48. }

    五、效果

    (1)启动eureka

    (2)启动Spring Boot Admin项目

    (3)效果

    访问: http://localhost:9006/login

    用户名/密码   admin/admin

  • 相关阅读:
    用户需求报告
    结队开发项目——七巧板NABC需求分析
    梦断代码读书笔记3
    课堂练习之环形二维数组
    结对开发之求最大子数组的溢出问题
    《代码之美》第二章读后感(一)
    软件工程项目冲刺阶段二:第五天
    软件工程项目冲刺阶段二:第四天(06-06)
    软件工程项目冲刺阶段二:第三天
    课程评价
  • 原文地址:https://www.cnblogs.com/edda/p/13261682.html
Copyright © 2020-2023  润新知