springboot-admin监控环境搭建

本文介绍如何使用 Spring Boot Admin 搭建应用监控系统,包括服务端和客户端配置步骤,实现基本功能及安全认证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建两个项目来做测试,ymSpringBoot-admin-service 和 ymSpringBoot-admin-client 两个工程,在下面说明中会简称service  和 client 

配置服务端 

2、在service项目的pom.xml中加入入依赖

 

<dependency>
    <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
        <version>2.1.0</version>
    </dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3、在service中创建application.yml配置文件

server:
  port: 9090

4、在service中创建启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;

import de.codecentric.boot.admin.server.config.EnableAdminServer;

@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminApplication.class, args);
    }
}

注:到此为止可以启动项目访问9090端口可以看到管理界面,但是无内容,说明service配置成功

配置客户端

5、在client项目中的pom.xml中加入依赖

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 6、在client中创建application.yml配置文件

server:
  port: 8080
  
spring:
  boot:
    admin:
      client:
        url: "http://localhost:9090"  #服务端访问地址
management:
  endpoints:
    web:
      exposure:
        include: "*"  #公开所有端点

7、在client项目中创建启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootAdminClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootAdminClientApplication.class, args);
	}
}

注:到此一个简单的spingboot-admin监控项目己搭建完成,启动client后,可以在http://127.0.0.1:9090管理地址中看到监控内容

 

注:但是现在有一个问题,监控不需要用户名密码就能直接进去,这要是在正式环境显然不行,所以需要加入security,要输入用户名和密码才能进入监控页面。

8、修改service端新增安全依赖包

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

9、在service端新增SecuritySecureConfig类


import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

import de.codecentric.boot.admin.server.config.AdminServerProperties;

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
	private final String adminContextPath;

	public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
		this.adminContextPath = adminServerProperties.getContextPath();
	}

	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		// @formatter:off
		SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
		successHandler.setTargetUrlParameter("redirectTo");
		successHandler.setDefaultTargetUrl(adminContextPath + "/");

		http.authorizeRequests().antMatchers(adminContextPath + "/assets/**").permitAll()
				.antMatchers(adminContextPath + "/login").permitAll().anyRequest().authenticated().and().formLogin()
				.loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout()
				.logoutUrl(adminContextPath + "/logout").and().httpBasic().and().csrf()
				.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
				.ignoringAntMatchers("/instances", "/actuator/**");
		// @formatter:on
	}
}

10、重新启动服务,在控制台找到一串密码,访问http://127.0.0.1:9090/login 输入默认用户名  user  +  控制台的密码

 

至此,spring-admin监控的安全配置己完成,但是总觉得哪里不对,嗯密码总不可能每次启动随机生成后从控制台去看吧。

11、修改service中的application.yml,新增安全配置用户名和密码。

spring:
    security: #spring安全认证
      basic:
        enabled: true
      user:
        name: sl
        password: 123456
        roles:
        - USER

 然后再次访问就要输自己对应的帐号密码就OK啦,如下图

参考官方文档 : http://codecentric.github.io/spring-boot-admin/2.1.0/#discover-clients-via-spring-cloud-discovery 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值