Spring Cloud 3.x 集成admin快速入门Demo

1.什么是Spring Boot Admin?

Spring Boot Admin(SBA)是一个社区开源项目,用于管理和监视Spring Boot 应用程序 ,它提供详细的健康(Health)信息、内存信息、JVM 系统和环境属性、垃圾回收信息、日志设置和查看、定时任务查看、Spring Boot 缓存查看和管理等功能 Spring Boot Admin 分为服务端(spring-boot-admin-server)和客户端(spring-boot-admin-client),服务端和客户端之间采用 http 通讯方式实现数据交互;单体项目中需要整合 spring-boot-admin-client 才能让应用被监控。

2.代码工程

实验目的

搭建server服务,并将client端的信息注册到server上

服务端

pom.xml

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">
    <parent>
        <artifactId>admin</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>admin-server</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>3.0.4</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

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


    </dependencies>
</project>

config

kotlin 复制代码
package com.demo;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.web.SecurityFilterChain;

import static org.springframework.security.config.Customizer.withDefaults;


@Configuration
public class BootAdminMonitorSecurityConfig {

    private final String adminContextPath;

    public BootAdminMonitorSecurityConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }


    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests((requestMatcherRegistry) -> requestMatcherRegistry.anyRequest().authenticated())
                .httpBasic(withDefaults());
        return http.build();
    }

   
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().requestMatchers(adminContextPath + "/instances", adminContextPath + "/actuator/**");
    }
}

启动类

typescript 复制代码
package com.demo;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAdminServer
@SpringBootApplication
public class BootMonitorApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(BootMonitorApplication.class, args);
    }

}

application.yaml

yaml 复制代码
server:
  port: 8081
management:
  endpoint:
    health:
      show-details: always
      enabled: true
    beans:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"
    enabled-by-default: true
spring:
  application:
    name: admin-server
  security:
    user:
      name: admin
      password: admin

客户端

pom.xml

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">
    <parent>
        <artifactId>admin</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>admin-client</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>3.0.4</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>


    </dependencies>
</project>

controller

kotlin 复制代码
package com.demo;


import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
public class HelloController {

    @GetMapping("/hello")
   @ResponseBody
    public String findAll(){
        return "hello world";
    }

}

config

kotlin 复制代码
package com.demo;


import org.springframework.boot.actuate.web.exchanges.HttpExchangeRepository;
import org.springframework.boot.actuate.web.exchanges.InMemoryHttpExchangeRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ActuatorConfiguration {

   @Bean
   public HttpExchangeRepository httpTraceRepository() {
       InMemoryHttpExchangeRepository repository = new InMemoryHttpExchangeRepository();
       // save 1000 http record
       repository.setCapacity(1000);
       return repository;
   }
}



package com.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;


@Configuration
public class BootAdminMonitorSecurityConfig {


    @Bean
    protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http.authorizeHttpRequests((authorizeRequests) -> authorizeRequests.anyRequest().permitAll())
                .csrf().disable().build();
    }
}

启动类

typescript 复制代码
package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

}

application.yaml 必须在客户端配置boot.admin.client.instance.service-url属性,让Spring Boot Admin服务端可以通过网络获取客户端的数据(否则默认会通过主机名去获取)

yaml 复制代码
server:
  port: 8082
management:
  endpoint:
    health:
      show-details: always
      enabled: true
    beans:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "*"
    enabled-by-default: true
spring:
  application:
    name: admin-client
    ## spring boot admin
  boot:
    admin:
      client:
        #server
        url: http://127.0.0.1:8081/
        username: admin
        password: admin

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

3.测试

启动服务器

访问http://127.0.0.1:8081/

启动客户端

访问http://localhost:8082/hello

4.引用

相关推荐
暗夜绿几秒前
【GoF23种设计模式】01_建造者模式
java·设计模式·建造者模式
I_Am_Me_2 分钟前
【JavaEE进阶】SpringBoot 快速上⼿
java·spring boot·java-ee
200不是二百4 分钟前
短信发送业务
java·开发语言·junit
drebander8 分钟前
建造者模式Builder——优雅的使用姿势
java·算法·建造者模式
HuaLuLemon9 分钟前
03-微服务搭建
java·微服务·gulimall
Tech Synapse17 分钟前
Java中使用FFmpeg拉取RTSP流
java·开发语言·ffmpeg
水w19 分钟前
详细介绍HTTP与RPC:为什么有了HTTP,还需要RPC?
java·开发语言·后端·http·rpc·1024程序员节
我的运维人生23 分钟前
Spring Boot应用开发深度解析与实践案例
java·spring boot·后端·运维开发·技术共享
Peter_chq25 分钟前
【计算机网络】数据链路层
linux·c语言·开发语言·网络·c++·后端·网络协议
zlt200031 分钟前
SpringAI:Java 开发的智能新利器
java·后端·spring ai