一起来学SpringBoot | 第十五篇:actuator与spring-boot-admin

SpringBoot是为了简化Spring应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程

一起来学SpringBoot | 第十四篇:强大的 actuator 服务监控与管理中介绍了actuator的作用,细心的朋友可能会发现通过http restful api的方式查看信息过于繁琐也不够直观,效率低下,运维人员看到JSON数据更是一脸懵逼,当服务过多的时候查看起来就过于操蛋了,每个服务都需要调用不同的接口来查看监控信息,备受各种困扰因素的我默默翻了下全球最大男性交友平台找到了spring-boot-admin

什么是SBA

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

Spring Boot Admin在对单一应用服务监控的同时也提供了集群监控方案,支持通过eurekaconsulzookeeper等注册中心的方式实现多服务监控与管理…

导入依赖

pom.xml中添加spring-boot-admin的相关依赖,这里只演示单机版本的,因此就自己监控自己了

<dependencies>
    <!-- 服务端:带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>
    <!-- 安全认证 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <!-- 端点 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- 在管理界面中与 JMX-beans 进行交互所需要被依赖的 JAR -->
    <dependency>
        <groupId>org.jolokia</groupId>
        <artifactId>jolokia-core</artifactId>
    </dependency>
</dependencies>

注意事项

如果要访问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文件中配置actuator的相关配置,其中info开头的属性,就是访问info端点中显示的相关内容,值得注意的是Spring Boot2.x中,默认只开放了info、health两个端点,剩余的需要自己通过配置management.endpoints.web.exposure.include属性来加载(有include自然就有exclude,不做详细概述了)。这个management.endpoints.web.base-path属性比较重要,因为Spring Boot2.x后每个端点默认的路径是/actuator/endpointId这样一来Spring Boot Admin是无法正常采集的

application.properties

# 描述信息
info.blog-url=https://winterchen.com
info.author=Luis
info.version=@project.version@
info.name=@project.artifactId@
# 选择激活对应环境的配置,如果是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-dev.properties - 空

application-prod.properties

为了安全起见,应采用认证的方式

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

主函数

添加上@EnableAdminServer注解即代表是Server端,集成UI的

package com.winterchen;

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

@EnableAdminServer
@SpringBootApplication
public class SpringBootActuatorAdminApplication {

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

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

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

测试

完成准备事项后,启动SpringBootActuatorAdminApplication访问http://localhost:8080/login看到登陆页面则代表一切正常,接着输入账号密码点击登陆即可…

首页

由于篇幅原因大图就不放太多了,有兴趣的朋友可以直接fork代码运行即可

总结

参考文档:http://codecentric.github.io/spring-boot-admin/2.0.0/

目前很多大佬都写过关于SpringBoot的教程了,如有雷同,请多多包涵,本教程基于最新的spring-boot-starter-parent:2.0.2.RELEASE编写,包括新版本的特性都会一起介绍…

说点什么

springboot技术交流群:681513531

个人博客:https://blog.winterchen.com

全文代码:https://github.com/WinterChenS/springboot-learning-experience

猜你喜欢

转载自blog.csdn.net/Winter_chen001/article/details/81018644