SpringCloud极简入门(十五)SpringBoot Admin

阅读次数:3    时间:2018-09-13     作者:陈刚

一.什么是SpringBoot Admin

SpringBoot Admin 它是在 Spring Boot Actuator 的基础上用于监控 Spring Boot 的应用,且提供简洁的可视化 WEB UI界面。Spring Boot Admin 提供了很多功能,可以监控如:spring-boot项目的基本信息,详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改logger的level。

二.集成

SpringBoot Admin 包括服务端可客户端组成 ,我们需要在Eureka环境中来使用SpringBootAdmin ,我们需要使用到三个应用: EurekaSever 注册中心,SpringBootAdmin 服务端 ,eureka-client 客户端 ,基于SpringBoot Admin 2.0 来构建 。而EurekaServer注册中心的搭建之前已经介绍过了这里不在多说,我的EurekaServer端口为 1111

1.搭建SpringBoot应用 (SpringBootAdmin ),pom如下
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
        <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


    <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>

因为同时 SpringBootAdmin 也要作为Eurake的客户端,所以我这里引入了Eurake的依赖 ,还引入了 security 安全依赖, actuator监控相关依赖,和spring-boot-admin 相关依赖,包括spring-boot-admin-server-ui 界面相关依赖

2.主程序配置类开启 SpringBootAdmin功能,同时也开启EurekaClient功能 如下:
//EnableAdminServer 开启 springboot admin
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class SpringbootadminApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootadminApplication.class, args);
    }

    @Profile("insecure")
    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().permitAll()//
                    .and().csrf().disable();
        }
    }

    @Profile("secure")
    @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");

            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().disable();
            // @formatter:on
        }
    }
}

SecurityPermitAllConfig和SecuritySecureConfig的配置是 Spring Boot Admin 官方给的配置,是对 url 进行安全认证等配置,照着配置即可,官方配置:https://github.com/codecentric/spring-boot-admin/blob/master/spring-boot-admin-samples/spring-boot-admin-sample-eureka/src/main/java/de/codecentric/boot/admin/SpringBootAdminApplication.java

3.添加配置文件 application.yml 如下
  spring:
  application:
    name: admin-server
  profiles:
    active:
      - secure


# tag::configuration-eureka[]
eureka:   #<1>
  instance:
    health-check-url-path: /actuator/health   #应用实例的相对运行状况检查URL路径默认为 /info/health ,2.0后actuator的地址发生了变化
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/  #注册到 EurekaServer

# 2.0开始,actuator默认不开放,所以要设置为开放
management:
  endpoints:
    web:
      exposure:
        include: "*"  #暴露所有的端点我们可以看到更多应用相关信息
  endpoint:
    health:
      show-details: ALWAYS  #health endpoint是否必须显示全部细节
server:
  port: 2222
# end::configuration-eureka[]

---
spring:
  profiles: insecure

---
# admin登录的用户名和密码
spring:
  profiles: secure
  security:
    user:
      name: "user"
      password: "password"

# 注册给eureka的时候告诉eureka自己的密码
eureka:
  instance:
    metadata-map:
      "user.name": ${spring.security.user.name}         #These two are needed so that the server
      "user.password": ${spring.security.user.password} #can access the protected client endpoints

上面配置官方提供,修改时根据情况修改 server.port 和 eureka . client. serviceUrl .defaultZone ,和serviceUrl.usesr.name 、password即可 ,配置地址:https://github.com/codecentric/spring-boot-admin/blob/master/spring-boot-admin-samples/spring-boot-admin-sample-eureka/src/main/resources/application.yml
到这里SpringBootAdmin应用构建完成

4. 构建EurekaClient应用(也是SpringBoot Admin的客户端),pom如下

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</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>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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>

5.应用主程序配置类如下

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

6.添加EurekaClient配置文件 application.yml如下

eureka:
  instance:
    health-check-url-path: /actuator/health   #应用实例的相对运行状况检查URL路径默认为 /info/health
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/  #注册中心地址

management:
  endpoints:
    web:
      exposure:
        include: "*"   #暴露所有的端点我们可以看到更多的服务实例相关信息,
  endpoint:
    health:
      show-details: ALWAYS   #health endpoint是否必须显示全部细节

server:
  port: 3333

spring:
  application:
    name: provider
  boot:
    admin:
      client:
        url: "http://localhost:2222"   #指定SpringBootAdmin服务端地址
        password: password
        username: user
        instance:
          prefer-ip: true

在客户端应用的配置中,我们指定了EurekaServer注册中心的地址,和配置了SpringBootAdmin服务端地址并设置用户名和密码,和应用监控的端点的暴露规则

7.测试,

依次启动 EurekaServer ,SpringBootAdmin,EurekaClient客户端 ,访问 SpringBootAdmin 的地址 http://localhost:2222可以看到登录界面:
image.png
使用配置的用户和密码登录进去:image.png
可以看到SpringBootAdmin和EurekaClient这两个应用的服务已经被挂载上去,点击 PROVIDER进去 可以看到很多应用监控相关信息 image.png
image.png