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/

相关推荐
Q_Q51100828515 分钟前
python的校园研招网系统
开发语言·spring boot·python·django·flask·node.js·php
舒一笑17 分钟前
如何优雅统计知识库文件个数与子集下不同文件夹文件个数
后端·mysql·程序员
IT果果日记18 分钟前
flink+dolphinscheduler+dinky打造自动化数仓平台
大数据·后端·flink
Java技术小馆30 分钟前
InheritableThreadLoca90%开发者踩过的坑
后端·面试·github
寒士obj39 分钟前
Spring容器Bean的创建流程
java·后端·spring
掉鱼的猫1 小时前
Spring AOP 与 Solon AOP 有什么区别?
java·spring
不是光头 强1 小时前
axure chrome 浏览器插件的使用
java·chrome
笨蛋不要掉眼泪2 小时前
Spring Boot集成腾讯云人脸识别实现智能小区门禁系统
java·数据库·spring boot
桃源学社(接毕设)2 小时前
云计算下数据隐私保护系统的设计与实现(LW+源码+讲解+部署)
java·云计算·毕业设计·swing·隐私保护
数字人直播2 小时前
视频号数字人直播带货,青否数字人提供全套解决方案!
前端·javascript·后端