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/

相关推荐
一 乐6 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
Boilermaker19926 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
Cherry的跨界思维6 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
alonewolf_997 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
一嘴一个橘子7 小时前
spring-aop 的 基础使用(啥是增强类、切点、切面)- 2
java
码事漫谈7 小时前
Protocol Buffers 编码原理深度解析
后端
sheji34167 小时前
【开题答辩全过程】以 中医药文化科普系统为例,包含答辩的问题和答案
java
码事漫谈7 小时前
gRPC源码剖析:高性能RPC的实现原理与工程实践
后端
恋爱绝缘体18 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
wszy18098 小时前
新文章标签:让用户一眼发现最新内容
java·python·harmonyos