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.引用

相关推荐
许苑向上2 小时前
MVCC底层原理实现
java·数据库·mvcc原理
组合缺一2 小时前
Solon Cloud Gateway 开发:熟悉 ExContext 及相关接口
java·后端·gateway·solon
一只淡水鱼662 小时前
【spring】集成JWT实现登录验证
java·spring·jwt
忘忧人生3 小时前
docker 部署 java 项目详解
java·docker·容器
null or notnull3 小时前
idea对jar包内容进行反编译
java·ide·intellij-idea·jar
言午coding4 小时前
【性能优化专题系列】利用CompletableFuture优化多接口调用场景下的性能
java·性能优化
幸好我会魔法4 小时前
人格分裂(交互问答)-小白想懂Elasticsearch
大数据·spring boot·后端·elasticsearch·搜索引擎·全文检索
SomeB1oody5 小时前
【Rust自学】15.2. Deref trait Pt.1:什么是Deref、解引用运算符*与实现Deref trait
开发语言·后端·rust
缘友一世5 小时前
JAVA设计模式:依赖倒转原则(DIP)在Spring框架中的实践体现
java·spring·依赖倒置原则
何中应5 小时前
从管道符到Java编程
java·spring boot·后端