使用spring boot admin监控spring boot程序

3 篇文章 0 订阅
1 篇文章 0 订阅

一.配置server端

1.pom.xml添加依赖

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

2.application.yml配置

spring:
  application:
    name: admin-server
  security:
    user:
      name: "admin"
      password: "admin"
server:
  port: 64000
  

3.配置程序

package com.wsk.admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
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;
import de.codecentric.boot.admin.server.config.EnableAdminServer;

@SpringBootApplication
@EnableScheduling
@EnableAdminServer
public class SpringbootAdminApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootAdminApplication.class, args);
	}
	
	@Configuration
	public static 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()//Grants public access to all static assets and the login page.
	                .antMatchers(adminContextPath + "/login").permitAll()
	                .anyRequest().authenticated()//	Every other request must be authenticated.
	                .and()
	                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()//Configures login and logout.
	                .logout().logoutUrl(adminContextPath + "/logout").and()
	                .httpBasic().and()//Enables HTTP-Basic support. This is needed for the Spring Boot Admin Client to register.
	                .csrf()
	                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())//	Enables CSRF-Protection using Cookies
	                .ignoringAntMatchers(
	                        adminContextPath + "/instances",//	Disables CRSF-Protection the endpoint the Spring Boot Admin Client uses to register.
	                        adminContextPath + "/actuator/**"//Disables CRSF-Protection for the actuator endpoints.
	                );
	    }
	}
	
}

4.运行http://localhost:64000/login

输入用户名admin和密码admin登入

二. 配置需要监控的spring boot程序

1.添加依赖

		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>2.1.4</version>
		</dependency>

2.修改application.yml配置

spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:64000
        username: admin
        password: admin
server:
  port: 64001

management:
  endpoints:
    web:
      exposure:
        include: '*'
        exclude: configprops
  endpoint:
    health:
      show-details: ALWAYS

3.执行应该能注册成功,如发现问题可增加debug打印,配置为application.yml同级目录增加logback.xml

配置为

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
    <jmxConfigurator/>
</configuration>

可显示debug信息,正常关闭即可

4.正常显示结果

 

参考博客文档 

https://blog.csdn.net/lv4961382/article/details/85129144

https://blog.csdn.net/weixin_34067049/article/details/88677682

https://blog.csdn.net/ClementAD/article/details/70613209

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值