springboot-admin 2.0 监控

我们知道spring-boot-actuator暴露了大量统计和监控信息的端点,spring-boot-admin
就是为此提供的携带页面的监控

Spring Boot Admin Server

设置对应的pom.xml文件

<?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.pingan</groupId>
   <artifactId>monitor-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>monitor-server</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.2.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>
   </properties>

   <dependencies>
      <!-- 服务端:带UI界面 -->
      <dependency>
         <groupId>de.codecentric</groupId>
         <artifactId>spring-boot-admin-starter-server</artifactId>
         <version>2.0.0</version>
      </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-security</artifactId>
      </dependency>
      <!-- 端点 -->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</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>
      <!-- 在管理界面中与 JMX-beans 进行交互所需要被依赖的 JAR -->
      <dependency>
         <groupId>org.jolokia</groupId>
         <artifactId>jolokia-core</artifactId>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
               <execution>
                  <goals>
                     <goal>build-info</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>

</project>

2 开启一个简单的spring boot 项目

@SpringBootApplication
@EnableAdminServer
public class MonitorServerApplication {
   public static void main(String[] args) {

      SpringApplication.run(MonitorServerApplication.class, args);
   }

   /**
    * dev 环境加载
    */
   @Profile("dev")
   @Configuration
   public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
      @Override
      protected void configure(HttpSecurity http) throws Exception {
         http.authorizeRequests().anyRequest().permitAll()
               .and().csrf().disable();
      }
   }

/**
 * prod 环境加载
 */
@Profile("prod")
@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 {
      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();
   }
}

3 引入下面的属性文件

# 描述信息
info.blog-url=https://blog.csdn.net/str0708
info.author=疯信子to
# 如果 Maven 插件没配置此处请注释掉
[email protected]@
[email protected]@
# 选择激活对应环境的配置,如果是dev则代表不用认证就能访问监控页,prod代表需要认证
spring.profiles.active=dev

# 日志文件
logging.file=./logs/monitor-server.log
logging.level.root=debug

# 加载所有的端点/默认只加载了 info / health
management.endpoints.web.exposure.include=*
# 比较重要,默认 /actuator spring-boot-admin 扫描不到
management.endpoints.web.base-path=/
management.endpoint.health.show-details=always

#spring.boot.admin.client.url=http://localhost:8080
#spring.boot.admin.client.name=monitor-server
# 不配置老喜欢用主机名,看着不舒服....
#spring.boot.admin.client.instance.prefer-ip=true
#spring.boot.admin.client.url=http://localhost:8080
#设置监控的名称
#spring.application.name=monitor-server
以上就完成了spring-boot-admin 的Service 

Spring Boot Admin Client

在项目的pom.xml中引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.0.0</version>
</dependency>

<dependency>
    <groupId>org.jolokia</groupId>
    <artifactId>jolokia-core</artifactId>
</dependency>

2 在属性文件中加入下面的配伍

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
# 描述信息
info:
  blog-url: https://blog.csdn.net/str0708
  author: 疯信子
# 如果 Maven 插件没配置此处请注释掉
  version: @project.version@
  name: @project.artifactId@
spring:
    application:
       name: company-admin
    boot:
        admin:
          client:
            #对应的Spring Boot Admin Server的地址
            url: http://localhost:8080  
            instance:
              prefer-ip: true

猜你喜欢

转载自blog.csdn.net/str0708/article/details/81092536