springboot-admin对springboot项目监控

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wsgsm/article/details/85341838

简介

springboot-admin实在Spring Boot Actuator的基础上提供简洁的可视化WEB UI,是用来管理SpringBoot应用程序的一个简单的界面,功能如下:

  1. 显示 name/id 和版本号
  2. 显示在线状态
  3. Logging日志级别管理
  4. JMX beans管理
  5. Threads会话和线程管理
  6. Trace应用请求跟踪
  7. 应用运行参数信息,如:
    • Java 系统属性
    • Java 环境变量属性
    • 内存信息
    • Spring 环境属性

应用

1.新建springboot-admin服务端

引入服务端pom依赖:

        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
			<version>2.0.1</version>
		</dependency>

		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-server-ui</artifactId>
			<version>2.0.1</version>
		</dependency>

		<!-- spring-security安全认证 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

		<dependency>
			<groupId>org.jolokia</groupId>
			<artifactId>jolokia-core</artifactId>
		</dependency>

application.yml配置

server:
  port: 8080

#admin登录的用户名和密码
spring:
  application:
    name: admin-server

  #security用户配置
  security:
    user:
      name: admin
      password: admin

  boot:
    admin:
      context-path: /admin  
      discovery: true

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

新建SecuritySecureConfig配置:

import de.codecentric.boot.admin.server.config.AdminServerProperties;
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;

/**
 * @Description: TODO(Springboot-Admin登录配置)
 * @Author: 爱飘de小子
 */
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter{

    /**
     * -private
     * 定义Admin上下文路径
     */
    private final String adminContextPath;

    /**
     * 由于解决分布式Web应用程序中的身份验证和授权有多种方法,因此Spring Boot Admin不提供默认方法。默认情况下,spring-boot-admin-server-ui提供登录页面和注销按钮。
     * Spring Security配置
     * @param adminServerProperties
     */
    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        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().disable();
    }
}

启动类添加@EnableAdminServer注解:

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author 爱飘de小子
 */
@EnableAutoConfiguration
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

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

}

启动springboot项目即可访问: ip:port/admin

2.配置客户端(被监控的springboot项目)

pom文件添加依赖:

		<!-- springboot admin 客户端 -->
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>2.0.1</version>
		</dependency>

		<dependency>
			<groupId>org.jolokia</groupId>
			<artifactId>jolokia-core</artifactId>
		</dependency>
		<!-- web-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

application.yml配置:

spring:
  boot:
    admin:
      client:
        url: http://localhost:8080/
        username: admin
        password: admin
        instance:
          prefer-ip: true  #使用IP注册
          
#项目健康监控
management:
  endpoints:
    web:
      exposure:
        include: "*"
      base-path: /admin  #服务端配置的context-path
  endpoint:
    health:
      show-details: always

#项目信息 
info:
  app:
    #自定义显示信息
    name: "xxx"
    description: "xxx"
    version: "xx"
    environment: "xx"
    spring-boot-version: "2.0.1.RELEASE"
    spring-admin-version: "2.0.1"
  version: "1.0"

猜你喜欢

转载自blog.csdn.net/wsgsm/article/details/85341838