Spring Boot Admin监控系统搭建

Spring Boot Admin监控系统搭建

介绍

Spring Boot Admin是对Spring Boot的管理和监控的一个开源框架,支持Eureka服务注册列表状态监控,JMX监控,日志监控,JVM信息,垃圾信息,内存情况的监控,还可以设置日志的level级别。Spring Boot Admin UI 采用AngularJs将数据展示在前端。Spring Boot Admin分为服务端和客户端。

Spring Boot Admin Server端搭建

  1. 添加依赖项,依赖项内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    <dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server</artifactId>
    </dependency>
    <dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    </dependency>
  2. 在Spring Boot主类上添加对Spring Boot Admin Server启动的注解@EnableAdminServer

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    @SpringBootApplication
    @EnableDiscoveryClient # 开启服务注册。
    @EnableTurbineStream # 开启Turbine服务监控。
    @EnableAdminServer # 开启Spring Boot Admin Server功能。
    public class SpringCloudMonitorApplication {
    public static void main(String[] args) {
    SpringApplication.run(SpringCloudMonitorApplication.class, args);
    }

    @Configuration
    public static class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    // 具有登录表单的页面作为/login.html提供,并在/ login上执行POST。
    http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
    // 设置登出页面地址。
http.logout().logoutUrl("/logout");
    // 设置目前不支持csrf。
http.csrf().disable();
    // 允许静态页面,静态数据的访问。
    http.authorizeRequests().antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**").permitAll();
    // 除了授权的页面所有内容都要进行授权访问。
    http.authorizeRequests().antMatchers("/**").authenticated();
    // 启用安全认证,以便客户端可以通过HTTP basic进行身份验证以进行注册。
    http.httpBasic();
    }
    }
    }

我们看到上面其实还添加对Eureka的注解以及对Turbine Stream的注解功能,需要添加Turbine Stream注解相关与Spring boot Admin Server ui相关依赖项,为了安全起见导入了安全相关依赖项。依赖项内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!—spring boot admin server ui支持登录页面依赖-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
</dependency>
<!--Spring Boot 后台管理系统集成Hystrix监控-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-hystrix</artifactId>
</dependency>
<!--Spring Boot 后台管理系统集成turbine-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-turbine</artifactId>
</dependency>
<!--对Turbine Stream支持,使用RabbitMQ的方式对Hystrix数据进行收集分析。-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine-stream</artifactId>
<exclusions>
<exclusion>
<artifactId>netty-transport-native-epoll</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>netty-codec-http</artifactId>
<groupId>io.netty</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<!-- Eureka支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!—安全支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

其实Spring Boot Admin Server依赖中包含对Zuul和Hystrix以及Eureka核心包的依赖整合,所以我们看到上面只是整合了UI相关的依赖项。而针对Eureka和Turbine Stream以及安全方面的依赖需要额外导入进去,依赖项也导入进去后需要对端点进行权限安全控制,我们可以看到上面内部类中对安全进行控制,通过继承WebSecurityConfigurerAdapter对安全进行配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 具有登录表单的页面作为/login.html提供,并在/ login上执行POST。
http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
// 设置登出页面地址。
http.logout().logoutUrl("/logout");
// 设置目前不支持csrf。
http.csrf().disable();
// 允许静态页面,静态数据的访问。
http.authorizeRequests()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
.permitAll();
// 除了授权的页面所有内容都要进行授权访问。
http.authorizeRequests().antMatchers("/**").authenticated();
// 启用安全认证,以便客户端可以通过HTTP basic进行身份验证以进行注册。
http.httpBasic();

也就是说对那些内容页面进行授权访问,那些页面是可以直接访问,对安全的开启等等一系列操作。
3. 修改配置文件信息,配置文件信息如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
logging:
level:
org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter: error
server:
port: 8040
turbine:
stream:
port: 8041 # 收集地址。
eureka: # 注册为eureka服务。
instance:
prefer-ip-address: true
metadata-map:
user.name: ${security.user.name}
user.password: ${security.user.password}
client:
service-url:
defaultZone: http://admin:${REGISTRY_SERVER_PASSWORD:password}@localhost:8761/eureka/
spring:
rabbitmq:
host: localhost
username: guest
password: guest
port: 5672
boot:
admin:
routes:
endpoints: env,metrics,trace,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream
turbine:
clusters: default
location: http://localhost:${turbine.stream.port}
security:
user:
name: admin
password: ${MONITOR_SERVER_PASSWORD:admin}

主要配置信息讲解:

  • turbine.stream.port: 对Turbine支持,turbine服务端口号。
  • boot.admin.routes.endpoints: Spring Boot Admin暴露的EndPoint端点
  • spring.boot.admin.routes.turbine.clusters: 集群的名称
  • spring.boot.admin.routes.turbine.location: 集成Turbine服务数据收集的地址。

这里的地址就是为了收集是需要访问地址,我们之前在Turbine章节中说过这里不在多阐述。
Eureka中的metadataMap是专门用来存放一些自定义的数据,当注册中心或者其他服务需要此服务的某些配置时可以在metadataMap里取。实际上,每个instance都有各自的metadataMap,map中存放着需要用到的属性。例如,上面配置中的eureka.instance.metadata-map.user.name,当这个服务成功注册到Eureka上,SpringBootAdmin就会取拿到这个instance,进而拿到metadataMap里的属性,然后放入请求头,向此服务发送请求,访问此服务的actuator开放的端点。

客户端

  1. 需要添加对安全保护的依赖项,以及对Turbine Stream注解。
    1
    2
    3
    4
    5
    6
    7
    8
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-netflix-hystrix-amqp</artifactId>
    </dependency>

或者添加如下依赖:

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

以上对Turbine Stream的支持请参考Spring Cloud Turbine相关文章内容。
2. 修改配置文件信息,添加对Turbine Stream 消息队列的支持。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
server:
port: 8889
eureka:
instance:
hostname: localhost
prefer-ip-address: true
metadata-map:
user.name: ${security.user.name}
user.password: ${security.user.password}
client:
service-url:
defaultZone: http://admin:${REGISTRY_SERVER_PASSWORD:password}@${eureka.instance.hostname}:8761/eureka/
healthcheck:
enabled: true
# Hystrix超时时间。
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
spring:
abbitmq:
port: 5672
username: guest
password: guest
host: localhost

# Spring Boot Admin配置相关
# SpringBoot 1.5以后的版本都默认开启端点保护
management:
security:
enabled: false

最主要的是management.security.enabled:关闭管理安全。
下面是Spring Boot Admin相关监控信息。

图片1
图片2
图片3
图片4

文章目录
  1. 1. Spring Boot Admin监控系统搭建
    1. 1.1. 介绍
    2. 1.2. Spring Boot Admin Server端搭建
    3. 1.3. 客户端
|
载入天数...载入时分秒...