Spring Cloud实战系列(八) - 微服务监控Spring Boot Admin

前言

Spring Boot Admin 是一个 管理监控 Spring Boot 应用程序 的一款开源软件。Spring Boot Admin 分为 Server 端和 Client 端,Spring Boot Admin UI 部分使用 AngularJS 将数据展示在前端。

正文

1. 项目结构

  • Eureka Server服务注册中心,端口号为 8761

  • Admin Server:用于对 微服务系统 进行统一的 监控管理

  • Admin Client客户端 集成 Admin

2. 构建Admin Server

新建一个 Spring Boot 的项目模块,取名为 admin-server,其完整依赖如下:

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>io.github.ostenant.springcloud</groupId>
<artifactId>admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>admin-server</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 管理界面与JMX-Beans交互 -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

配置 application.yml,设置 management.security.enabled=false,保证 安全验证 关闭,设置 Spring Boot Admin 默认开启的 服务端点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 5000
spring:
application:
name: admin-server
boot:
admin:
routes:
endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream
management:
security:
enabled: false
logging:
file: "logs/boot-admin-sample.log"

src/main/resources 目录下新建一个 logback-spring.xml 文件:

1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<jmxConfigurator/>
</configuration>

在应用的 启动类 上通过注解 @EnableAdminServer 开启 Admin Server 的功能。

1
2
3
4
5
6
7
8
9
@EnableEurekaClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

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

Admin Server 创建完成,运行 AdminServerApplication 启动应用程序!

2. 构建Admin Client

新建一个 Spring Boot 的项目模块,取名为 admin-client,其完整依赖如下:

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>io.github.ostenant.springcloud</groupId>
<artifactId>admin-client</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>admin-server</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

配置 application.yml 文件,设置 日志输出路径,并关闭 Actuator 模块的 安全验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: admin-client
management:
security:
enabled: false
logging:
file: "logs/boot-admin-client.log"

在应用的 启动类 上加上 @EnableEurekaClient 注解,开启 EurekaClient 的功能。

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableEurekaClient
public class AdminClientApplication {

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

3. 启动应用程序

依次启动 eureka-serveradmin-serveradmin-client 模块,访问 admin-server 的主页 http://localhost:5000,浏览器显示界面如图所示:

JOURNAL 选项为 服务注册下线剔除时间线

4. 添加安全登录界面

Spring Boot Admin 提供了登录界面的组件,并且和 Spring Boot Security 结合使用,需要 用户登录 才能访问。在 Admin Serverpom.xml 文件中引入以下依赖:

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

admin-server 模块的 application.yml 中完成如下配置,创建一个 securityuser 用户,它的用户名为 admin,密码为 123456。通过 eureka.instance.metadate-map 配置属性 带上该 securityuser 用户信息。

1
2
3
4
5
6
7
8
9
security:
user:
name: admin
password: 123456
eureka:
instance:
metadata-map:
user.name: admin
user.password: 123456

然后在 应用程序 中配置 Spring Boot Security,创建一个 SecurityConfig配置类,给 静态资源 加上 permitAll() 权限,其他的 资源访问 则需要 权限认证,另外这些资源不支持 CSFR跨站请求伪造),所以禁用掉 CSFR,最后需要开启 HTTP 的基本认证,即 httpBasic() 方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
http.logout().logoutUrl("/logout");
http.csrf().disable();

http.authorizeRequests()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
.permitAll();
http.authorizeRequests().antMatchers("/**").authenticated();

http.httpBasic();
}
}

重新启动 admin-server 项目模块,在浏览器中访问 admin-client 的地址 http://localhost:5000,在登录界面输入用户名 admin,密码为 123456,登录即可。

参考

  • 方志朋《深入理解Spring Cloud与微服务构建》

欢迎关注技术公众号: 零壹技术栈

零壹技术栈

本帐号将持续分享后端技术干货,包括虚拟机基础,多线程编程,高性能框架,异步、缓存和消息中间件,分布式和微服务,架构学习和进阶等学习资料和文章。

文章作者: Chen Vainlgory
文章链接: https://geek.vainlgory.top/2019/01/30/Spring Cloud实战系列(八) - 微服务监控Spring Boot Admin/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 零壹技術棧 | VainlgoryのBlog
微信打赏
支付宝打赏