Spring Cloud 入门 ---- Admin 监控中心
简介
是什么

Spring Boot Admin 是由 codecentric 组织开发的开源项目,使可以对 Spring Boot 应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用。它分为客户端和服务端两部分,客户端添加到你的 Spring Boot 应用增加暴漏相关信息的 HTTP 接口,然后注册到 Spring Boot Admin 服务端,这一步可以直接向服务端注册,也可以通过 Eureka 或者 Consul 进行注册。而 Spring Boot Admin Server 通过 Vue.js 程序监控信息进行可视化呈现。并且支持多种事件通知操作。

官网:https://codecentric.github.io/spring-boot-admin/current/

github: https://github.com/codecentric/spring-boot-admin

能干什么

Spring Boot Admin 可以提供以下的功能:

  • 监控应用运行过程中的概览信息;
  • 度量指标信息,比如JVM、Tomcat及进程信息;
  • 环境变量信息,比如系统属性、系统环境变量以及应用配置信息;
  • 查看所有创建的Bean信息;
  • 查看应用中的所有配置信息;
  • 查看应用运行日志信息;
  • 查看JVM信息;
  • 查看可以访问的Web端点;
  • 查看HTTP跟踪信息。
准备工作

在正式开始之前我们需要确定 spring boot 与 admin 的版本,如果版本不兼容会出现如下错误:

Error creating bean with name 'adminHandlerMapping' defined in class path resource [de/codecentric/boot/admin/server/config/AdminServerWebConfiguration$ServletRestApiConfirguation.class]: Invocation of init method failed; nested exception is java.lang.StackOverflowError

关于版本的选择请参考官网,我所选用的是:spring boot 2.3.3.RELEASEadmin 2.3.0 admin 的服务端与客户端依赖都是 2.3.0;并在父 pom 中指定版本依赖。

20201123102208930

Admin 监控中心模块

导入 pom 依赖

<!--admin service-->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>

添加 yml 配置

server:
  port: 9301

spring:
  application:
    name: admin-management-service

eureka-connection:
  name: akieay
  password: 1qaz2wsx
eureka:
  client:
    #表示是否将自己注册进 Eureka Server服务 默认为true
    register-with-eureka: true
    #f是否从Eureka Server抓取已有的注册信息,默认是true。单点无所谓,集群必需设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url: # 设置与 Eureka Server 交互的地址 查询服务与注册服务都需要这个地址
      #      defaultZone: http://localhost:7001/eureka
      defaultZone: http://${eureka-connection.name}:${eureka-connection.password}@eureka7001.com:7001/eureka,http://${eureka-connection.name}:${eureka-connection.password}@eureka7002.com:7002/eureka
  instance:
    instance-id: admin-management-9301
    ## 当调用getHostname获取实例的hostname时,返回ip而不是host名
    prefer-ip-address: true
    # Eureka客户端向服务端发送心跳的时间间隔,单位秒(默认30秒)
    lease-renewal-interval-in-seconds: 10
    # Eureka服务端在收到最后一次心跳后的等待时间上限,单位秒(默认90秒)
    lease-expiration-duration-in-seconds: 30

主启动 添加 @EnableAdminServer

@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer
public class AdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}
Admin 客户端模块

客户端我们沿用之前的 hystrix-provider-servicehystrix-consumer-service 在原有的基础上做修改添加 admin client 相关配置

修改客户端模块

客户端模块包括:hystrix-provider-servicehystrix-consumer-service,注意 hystrix-provider-service 的两个集群配置文件都要修改

导入 pom 依赖

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

添加 yml 配置

spring:
  boot:
    admin:
      client:
        # admin 监控中心地址
        url: http://localhost:9301
        
# 暴露端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

# 日志配置
logging:
  level:
    root: info
  com.akieay.cloud.hystrix.provider: debug
  file:
    path: logs/
    name: 服务名-端口号.log
测试监控中心

按顺序启动 Eureka 注册中心,Admin 监控中心,hystrix-provider-service 服务提供者的两个节点,hystrix-consumer-service 服务消费者;进入注册中心查看服务是否注册成功。

20201121174327368

访问:http://localhost:9301/ 进入监控中心

20201121174425546

多次调用 http://localhost/consumer/hystrix/ok/15 ,然后点击 HYSTRIX-CONSUMER-SERVICE 查看监控信息。

20201121174557684

点击 细节 并把界面往下拉,即可查看 进程线程堆内存非堆内存 的信息。

20201121162322760

点击 性能 即可查看 应用程序的各项指标信息,如 Hystrix 、JVM、Tomcat 的 各项指标信息。

20201121175626686

点击 环境 即可查看应用程序中各系统属性、环境变量、应用配置;如:systemPropertiessystemEnvironmentspringCloudClientHostInfoapplicationConfig 等。

20201121180049836

点击 可以查看应用程序中所创建的 bean 的信息。

20201121180355251

点击 配置属性 即可查看应用程序中的所有配置信息。

20201121180602438

点击 计划任务 可以查看应用程序中的计划任务的各项信息。

20201121180727344

点击 日志 模块可以查看当前应用程序的 实时日志 以及 日志配置,但是需要注意的是必须添加 日志相关的配置才能查看到日志,如上面的 logging 相关的配置。

20201123094842661

JVM 模块能够查看 线程 与 内存 相关信息。

20201123095916491

映射 模块能够查看应用程序中的 url 映射相关信息。

20201123095836068

Admin 监控中心添加认证

我们可以通过 Spring Security 为 Admin 监控中心添加登录认证功能,避免谁都能访问。

修改监控中心模块

导入 pom 依赖

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

添加 yml 配置

spring:
  security:
    # 配置spring security登录用户名和密码
    user:
      name: akieay-admin
      password: 123456

添加配置文件

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public WebSecurityConfig(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.csrf().disable()
                .authorizeRequests()
                //1.配置所有静态资源和登录页可以公开访问
                .antMatchers(adminContextPath + "/actuator/**").permitAll()
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated().and()
                //2.配置登录和登出路径
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                //3.开启http basic支持,admin-client注册时需要使用
                .httpBasic();
    }
}

重启服务,并访问:http://localhost:9301/ 这时进入admin 监控中心就需要我们自定义的用户名密码登录。

20201123101055629

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐