服务管理与监控 Spring Boot Admin 配置

1,248 阅读2分钟

这是我参与8月更文挑战的第7天,活动详情查看:8月更文挑战

1. 前言

    Spring Boot Admin 是一个管理和监控你的 Spring Boot 应用程序的应用程序。这些应用程序通过 Spring Boot Admin Client(通过 HTTP)注册或者使用 Spring Cloud(例如 Eureka)发现。UI只是 Spring Boot Actuator 端点上的一个 AngularJs 应用程序。

    原理:Spring Boot Actuator 模块为监控Spring Boot 应用程序暴露的大量的管理端点[ENDPOINT],在Spring Boot Actuator的基础上提供简洁的可视化WEB UI,是用来管理 Spring Boot 应用程序的一个简单的界面。

2. 服务端搭建

2.1 pom依赖

<dependencies>
    <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-server</artifactId>
        <version>2.4.3</version>
    </dependency>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
        <version>2.4.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>

服务端引入spring-boot-admin-starter-client是为了监听自己的模块

配置eureka的目的是通过注册中心去fetch其他模块的地址,这样就不需要客户端告诉admin它在哪里

2.2 配置文件

server:
  port: 8764
spring:
  application:
    name: admin
  security:
    user:
      name: root
      password: root
  profiles:
    include: admin
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #needed to trigger info and endpoint update after restart
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

2.3 程序入口

package com.holland.admin;


import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }

    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().permitAll()
                    .and().csrf().disable();
        }
    }
}

核心就是@EnableAdminServer

3 客户端搭建

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.boot.admin.client.username=root
spring.boot.admin.client.password=root

spring.security.user.name=root
spring.security.user.password=root
spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}

3.1 直接通知admin

配置基本通信路径

spring.boot.admin.client.url=http://localhost:8764

3.2 admin通过注册中心去fetch

直接配置eureka

eureka:
  client:
    serviceUrl:
      defaultZone: http://root:root@localhost:8761/eureka/

3.3 客户端配置有server.servlet.context-path不能正常监听

填加以下配置,不管有没有配置server.servlet.context-path,都能兼容

#此处保证不会因为context-path导致不能监听健康状态
eureka.instance.health-check-url-path=${server.servlet.context-path:}/actuator/health
eureka.instance.status-page-url-path=${server.servlet.context-path:}/actuator/info
eureka.instance.metadata-map.management.context-path=${server.servlet.context-path:}/actuator
management.endpoints.web.base-path=${server.servlet.context-path:}/actuator

4. Web端效果

image.png

image.png