一、简介
官方文档 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)依赖
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.abc</groupId>
- <artifactId>00-eurekaserver-8000</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.7.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
-
- <!--热部署依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- </dependency>
- </dependencies>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.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>
(3)application.yml配置
- server:
- port: 8000
-
- eureka:
- instance:
- hostname: localhost # 指定Eureka主机
- client:
- register-with-eureka: false # 指定当前主机是否向Eureka服务器进行注册
- fetch-registry: false # 指定当前主机是否要从Eurka服务器下载服务注册列表
- service-url: # 服务暴露地址
- defaultZone: http://localhost:8000/eureka
- # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
-
- # server:
- # enable-self-preservation: false # 关闭自我保护
(4)启动类
- package com.abc.eureka;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
-
- @SpringBootApplication
- @EnableEurekaServer // 开启Eureka服务
- public class EurekaServerApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekaServerApplication.class, args);
- }
- }
四、创建Spring Boot Admin项目
(1)创建项目
创建一个spring boot项目
(2)依赖
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.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.1.7.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.zzq</groupId>
- <artifactId>spring-boot-admin</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>spring-boot-admin</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-boot-admin.version>2.1.5</spring-boot-admin.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-server</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</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>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-commons</artifactId>
- <version>2.1.2.RELEASE</version>
- </dependency>
- <!--eureka客户端依赖-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- <version>2.1.2.RELEASE</version>
- </dependency>
- <!--热部署依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-security</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jetty</artifactId>
- </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>
(3)application.yml配置
- server:
- port: 9006
- spring:
- application:
- name: admin-server
- security:
- user:
- name: "admin"
- password: "admin"
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8000/eureka
- instance:
- prefer-ip-address: true
- ip-address: 127.0.0.1
- instance-id: ${eureka.instance.ip-address}.${server.port}
- lease-renewal-interval-in-seconds: 3
- lease-expiration-duration-in-seconds: 10
- health-check-url-path: /actuator/health
- metadata-map:
- user.name: "admin"
- user.password: "admin"
- management:
- endpoints:
- web:
- exposure:
- include: "*" #监控自身
- endpoint:
- health:
- show-details: ALWAYS
(3)启动类
- package com.zzq.springbootadmin;
-
- import de.codecentric.boot.admin.server.config.AdminServerProperties;
- import de.codecentric.boot.admin.server.config.EnableAdminServer;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
- import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
-
- @SpringBootApplication
- @EnableAdminServer
- @EnableEurekaClient
- public class SpringBootAdminApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(SpringBootAdminApplication.class, args);
- }
-
- @Configuration
- public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
- private final String adminContextPath;
-
- public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
- this.adminContextPath = adminServerProperties.getContextPath();
- }
-
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- // @formatter:off
- SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
- successHandler.setTargetUrlParameter("redirectTo");
- successHandler.setDefaultTargetUrl(adminContextPath + "/");
-
- http.authorizeRequests()
- .antMatchers(adminContextPath + "/assets/**").permitAll()
- .antMatchers(adminContextPath + "/login").permitAll()
- .anyRequest().authenticated()
- .and()
- .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
- .logout().logoutUrl(adminContextPath + "/logout").and()
- .httpBasic().and()
- .csrf()
- .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
- .ignoringAntMatchers(
- adminContextPath + "/instances",
- adminContextPath + "/**"
- );
- // @formatter:on
- }
- }
-
- }
五、效果
(1)启动eureka
(2)启动Spring Boot Admin项目
(3)效果
访问: http://localhost:9006/login
用户名/密码 admin/admin