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.ymlapplication.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/

相关推荐
云技纵横5 分钟前
@Transactional 里套 REQUIRES_NEW,为什么会把连接池耗尽?
后端·面试
tcdos11 分钟前
不止扫码 — 微信生态深度融合(登录 + 支付 + 消息)
后端·微信小程序
程序员cxuan14 分钟前
Anthropic 大面积封号,连大 V 都忍不了开喷了。
人工智能·后端·程序员
MacroZheng25 分钟前
短短几天,暴涨2.8万Star!又一款编程神器开源!
java·人工智能·后端
PinkSun28 分钟前
平台线程池用了3年很顺手,换成虚拟线程后我后悔了
后端
达达尼昂32 分钟前
Claude : 如何设计可控的agent-loops
前端·人工智能·后端
lambdax34 分钟前
Celery 心跳任务内存膨胀排查与修复全记录
后端
SamDeepThinking37 分钟前
函数式编程:用BiFunction消除多类型分支的代码重复
java·后端·面试
IT_陈寒1 小时前
Redis持久化丢失数据的坑,这次终于被我填平了
前端·人工智能·后端
葫芦和十三2 小时前
图解 MongoDB 24|分片为什么存在:垂直扩容的天花板
后端·mongodb·agent