SpringBoot 2.0 学习(八)使用Spring Boot Admin监控服务

SBA的诞生原因

上篇文章介绍了actuator的作用,细心的朋友可能会发现通过http restful api的方式查看信息过于繁琐也不够直观,效率低下,运维人员看到JSON数据更是一脸懵逼,这时强大的SBA就出现了

SBA是什么

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

先看效果图

在这里插入图片描述

怎么实现

第一步,添加依赖

	<!-- 服务端:带UI界面 -->
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
        <version>2.0.0</version>
    </dependency>
    <!-- 客户端包 -->
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
        <version>2.0.0</version>
    </dependency>
    <!-- 使用actuator对springboot监控 -->
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- 安全认证 -->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-security</artifactId>
	</dependency>
	<!-- 在管理界面中与 JMX-beans 进行交互所需要被依赖的 JAR -->
    <dependency>
        <groupId>org.jolokia</groupId>
        <artifactId>jolokia-core</artifactId>
    </dependency>

如果要访问info接口想获取maven中的属性内容请记得添加如下内容

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

第二步:配置文件

application.properties配置

# 描述信息
info.blog-url=https://blog.csdn.net/sinat_38798245
info.author=chenxj
[email protected]@
[email protected]@

# 选择激活对应环境的配置,如果是dev则代表不用认证就能访问监控页,prod代表需要认证
spring.profiles.active=prod
# 加载所有的端点/默认只加载了 info / health
management.endpoints.web.exposure.include=*
# 比较重要,默认 /actuator spring-boot-admin 扫描不到
management.endpoints.web.base-path=/
management.endpoint.health.show-details=always

# 可以关闭制定的端点
management.endpoint.shutdown.enabled=false

# 日志文件
logging.file=./target/admin-server.log

spring.boot.admin.client.url=http://localhost:${server.port}
# 不配置老喜欢用主机名,看着不舒服....
spring.boot.admin.client.instance.prefer-ip=true

application-prod.properties配置

# 登陆所需的账号密码
spring.security.user.name=chenxj
spring.security.user.password=123456
# 便于客户端可以在受保护的服务器上注册api
spring.boot.admin.client.username=chenxj
spring.boot.admin.client.password=123456
# 便服务器可以访问受保护的客户端端点
spring.boot.admin.client.instance.metadata.user.name=chenxj
spring.boot.admin.client.instance.metadata.user.password=123456

第三步:启动监控服务端
启动类上面添加上@EnableAdminServer注解即代表是Server端,集成UI的

/**
 * prod 环境加载
 */
@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();
    }
}

猜你喜欢

转载自blog.csdn.net/sinat_38798245/article/details/87192854