SpringBoot管理和监控: Spring Boot Admin

文章目录

  • SBA
  • 应用
    • pom.xml
    • application.yml

通过restful api的方式查看信息过于繁琐,也不直观,效率低下。当服务过多的时候看起来就过于麻烦,每个服务都需要调用不同的接口来查看监控信息。

SBA

SBA全称spring boot admin, 是一个管理和监控spring boot 应用程序的开源项目,分为admin-server与admin-client两个组件,admin-server通过采集actuator端点数据,显示在spring -boot-admin-ui上,已知的端点几乎都有进行采集,通过spring-boot-admin可以动态切换日志级别、导出日志、导出heapdump、监控各项指标等等

spring boot admin在对单一服务监控的同时也提供了集群监控方案,支持通过eureka、consul、zookeeper等注册中心的方式实现多服务监控与管理

应用

pom.xml

在pom.xml中添加对spring-boot-admin的相关依赖


<dependency>
	<groupId>de.codecentricgroupId>
	<artifactId>spring-boot-admin-starter-serverartifactId>
	<version>2.0.0version>
dependency>

<dependency>
	<groupId>de.codecentricgroupId>
	<artifactId>spring-boot-admin-starter-clientartifactId>
	<version>2.0.0version>
dependency>

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-securityartifactId>
dependency>

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-actuatorartifactId>
dependency>

<dependency>
	<groupId>org.jolokiagroupId>
	<artifactId>jolokia-coreartifactId>
dependency>

如果想访问info的信息需要如下配置

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-maven-pluginartifactId>
			<executions>
				<execution>
					<goals>
						<goal>build-infogoal>
					goals>
				execution>
			executions>
		plugin>
	plugins>
build>

application.yml

在application.yml中配置actuator的相关配置,其中info开头的属性,就是访问info端点中显示的相关内容,值得注意的十spring boot2.x中,默认只开放了info、health两个端点,剩余的需要自己通过配置management.endpoints.web.exposure.include属性来加载,这个management.endpoints.web.base-path属性比较重要,因为spring boot2.x后每个端点默认的路径是/actuator/endpointId这样依赖spring boot admin是无法正常采集的

application.yml

spring:
  profiles:
    active: prod # 指定读取哪个文件
  boot:
    admin:
      client:
        url: http://localhost:8080
        instance:
          prefer-ip: true
info:
  head: head
  body: body
management:
  endpoints:
    web:
      exposure:
        # 加载所有的端点,默认只加载了info、health
        include: '*'
      # 不配置SBA 扫描不到
      base-path: /
  endpoint:
    health:
      show-details: always
    # 可以关闭指定的端点
    shutdown:
      enabled: false

application-prod.yml

server:
  servlet:
    context-path: /

spring:
  boot:
    admin:
      client:
        username: jack
        password: jack
        instance:
          metadata:
            user:
              name: jack
              password: jack
  security:
    user:
      name: jack
      password: jack

在启动方法中增加@EnableAdminServer注解 代表是Server端

@EnableAdminServer
@SpringBootApplication
public class BootApplication{

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

    @Profile("dev")
    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter{
        @Override
        protected void configure(HttpSecurity http) throws Exception{
            http.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
        }
    }

    @Profile("prod")
    @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{
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");

            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();
        }
    }
}

测试, 启动项目 访问 http://localhost:8080/login

SpringBoot管理和监控: Spring Boot Admin_第1张图片

你可能感兴趣的:(SpringBoot)