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/

相关推荐
ShooterJ2 分钟前
MySQL基因分片设计方案详解
后端
PetterHillWater3 分钟前
ANOVA在软件工程中的应用
后端
cxyxiaokui0015 分钟前
还在用 @Autowired 字段注入?你可能正在写出“脆弱”的 Java 代码
java·后端·spring
回家路上绕了弯5 分钟前
深入 Zookeeper 数据模型:树形 ZNode 结构的设计与实践
后端·zookeeper
GeekAGI10 分钟前
Redis 不同架构下的故障发现和自动切换机制
后端
程序员蜗牛11 分钟前
瞧高手如何用flatMap简化代码!
后端
珹洺12 分钟前
Java-Spring入门指南(二十二)SSM整合前置基础
java·开发语言·spring
天天摸鱼的java工程师13 分钟前
Java IO 流 + MinIO:游戏玩家自定义头像上传(格式校验、压缩处理、存储管理)
java·后端
间彧13 分钟前
SpringBoot中结合SimplePropertyPreFilter排除JSON敏感属性
后端
Cache技术分享13 分钟前
207. Java 异常 - 访问堆栈跟踪信息
前端·后端