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/

相关推荐
梨子同志8 小时前
Java 基础语法详解
后端
bcbnb8 小时前
详细教程:iOS应用中Swift代码混淆步骤与工具推荐
后端
气π8 小时前
【JavaWeb】——(若依 + AI)-基础学习笔记
java·spring boot·笔记·学习·java-ee·mybatis·ruoyi
expect7g8 小时前
Paimon源码解读 -- Compaction-8.专用压缩任务
大数据·后端·flink
开心就好20258 小时前
H5 混合应用加密 Web 资源暴露到 IPA 层防护的完整技术方案
后端
阿里云云原生8 小时前
AgentScope Java 1.0:从模型到应用,AI Agent 全生命周期管理利器!
java·云原生
bcbnb8 小时前
最新版本iOS系统设备管理功能全面指南
后端
开心就好20258 小时前
Fastlane + Appuploader 的工程组合,自动化发布中的分工
后端
running up8 小时前
Maven依赖管理和项目构建工具
java·maven
YDS8298 小时前
SpringCould —— 网关详解
后端·spring·spring cloud