springboot整合admin

1. 添加依赖

首先,在你的admin服务端pom.xml文件中添加Spring Boot Admin的依赖:

XML 复制代码
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.5.4</version> <!-- 请根据实际情况选择最新版本 -->
</dependency>

2. 配置Spring Boot Admin Server

在你的Spring Boot应用中,启用Spring Boot Admin Server:

java 复制代码
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

3. 配置Spring Boot Admin Client

如果你想要将你的应用注册为Spring Boot Admin的客户端,还需要添加以下依赖:

XML 复制代码
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.5.4</version> <!-- 请根据实际情况选择最新版本 -->
</dependency>

如果你想要将你的应用注册为Spring Boot Admin的客户端,需要在application.yml或application.properties文件中进行配置:

javascript 复制代码
​
spring:
  boot:
    admin:
      client:
        url: http://localhost:8080  # Spring Boot Admin Server的地址

​

4. 启动应用

启动Spring Boot Admin Server和客户端应用后,访问Spring Boot Admin Server的地址(例如:http://localhost:8080),你将看到注册的客户端应用及其监控信息。

5. 安全配置(可选)

如果你想要保护Spring Boot Admin Server的访问,可以添加Spring Security依赖并进行配置:

XML 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

然后在application.yml中配置用户名和密码:

javascript 复制代码
spring:
  security:
    user:
      name: admin
      password: password

最后,创建一个Spring Security配置类来保护Admin Server的端点:

java 复制代码
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
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

6.页面访问

http://localhost:port/

相关推荐
Flynn Liang4 分钟前
Spring AI 2.0 深度解析:ChatClient、Advisor 链与 MCP——Java 工程师的 AI 落地最优解
java·人工智能·spring·大模型·架构设计·spring ai
灿飞10 分钟前
Kotlin与Java虚拟机六:基础与类型系统:一切皆对象,但对象有代价
java·kotlin
计算机毕设定制辅导-无忧学长20 分钟前
《基于微信小程序健康习惯激励系统设计与实现》
java·spring boot·mysql·微信小程序·uniapp·健康习惯激励系统
掘金者阿豪26 分钟前
SQL Server数据迁移完成后,凌晨的数据库告警该怎么查?
前端·后端
鶴哥只手遮天37 分钟前
AFSIM 2.9.0 深度解析·进阶卷第 1 章 插件源码精读:16 个 WSF 插件的架构与实现
后端
愚农搬码44 分钟前
低代码表单与在线问卷不是一回事:产品定位和技术边界
后端
武子康1 小时前
固定时间步已经开了,为什么游戏回放还会走出不同位置?
人工智能·后端·agent
70后写java的老码农1 小时前
换模型只改三行配置:LangChain4j + Spring Boot 3.5 实战,一次分别接入 9 家模型服务
后端·ai编程
愚农搬码1 小时前
2026 年开源表单设计器选型:哪个更好用?
后端
雪芽蓝域zzs1 小时前
第 1 章:IDEA 创建 SpringBoot3.5.16 Maven 项目
java·maven·intellij-idea