项目使用的Spring Cloud为Hoxton版本,Spring Boot为2.2.2.RELEASE版本
序号 | 内容 | 链接地址 |
---|---|---|
1 | Spring Cloud入门-十分钟了解Spring Cloud | https://blog.csdn.net/ThinkWon/article/details/103715146 |
2 | Spring Cloud入门-Eureka服务注册与发现(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103726655 |
3 | Spring Cloud入门-Ribbon服务消费者(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103729080 |
4 | Spring Cloud入门-Hystrix断路器(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103732497 |
5 | Spring Cloud入门-Hystrix Dashboard与Turbine断路器监控(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103734664 |
6 | Spring Cloud入门-OpenFeign服务消费者(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103735751 |
7 | Spring Cloud入门-Zuul服务网关(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103738851 |
8 | Spring Cloud入门-Config分布式配置中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103739628 |
9 | Spring Cloud入门-Bus消息总线(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103753372 |
10 | Spring Cloud入门-Sleuth服务链路跟踪(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103753896 |
11 | Spring Cloud入门-Consul服务注册发现与配置中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103756139 |
12 | Spring Cloud入门-Gateway服务网关(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103757927 |
13 | Spring Cloud入门-Admin服务监控中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103758697 |
14 | Spring Cloud入门-Oauth2授权的使用(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103761687 |
15 | Spring Cloud入门-Oauth2授权之JWT集成(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103763364 |
16 | Spring Cloud入门-Oauth2授权之基于JWT完成单点登录(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103766368 |
17 | Spring Cloud入门-Nacos实现注册和配置中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103769680 |
18 | Spring Cloud入门-Sentinel实现服务限流、熔断与降级(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103770879 |
19 | Spring Cloud入门-Seata处理分布式事务问题(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103786102 |
20 | Spring Cloud入门-汇总篇(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103786588 |
Spring Boot Admin 可以对SpringBoot应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用,本文将对其用法进行详细介绍。
SpringBoot应用可以通过Actuator来暴露应用运行过程中的各项指标,Spring Boot Admin通过这些指标来监控SpringBoot应用,然后通过图形化界面呈现出来。Spring Boot Admin不仅可以监控单体应用,还可以和Spring Cloud的注册中心相结合来监控微服务应用。
Spring Boot Admin 可以提供应用的以下监控信息:
这里我们创建一个admin-server模块来作为监控中心演示其功能。
在pom.xml中添加相关依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-starter-serverartifactId>
dependency>
在application.yml中进行配置:
server:
port: 9301
spring:
application:
name: admin-server
在启动类上添加@EnableAdminServer来启用admin-server功能:
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
这里我们创建一个admin-client模块作为客户端注册到admin-server。
在pom.xml中添加相关依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-starter-clientartifactId>
dependency>
在application.yml中进行配置:
server:
port: 9305
spring:
application:
name: admin-client
boot:
admin:
client:
# 配置admin-server地址
url: http://localhost:9301
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
# 添加开启admin的日志监控
logging:
file: admin-client.log
启动admin-server和admin-client服务。
访问如下地址打开Spring Boot Admin的主页:http://localhost:9301
点击wallboard按钮,选择admin-client查看监控信息;
监控信息概览;
度量指标信息,比如JVM、Tomcat及进程信息;
环境变量信息,比如系统属性、系统环境变量以及应用配置信息;
查看所有创建的Bean信息;
查看应用中的所有配置信息;
查看日志信息,admin-client模块的applicaiton.yml需要添加以下配置才能开启;
logging:
#添加开启admin的日志监控
file: admin-client.log
查看JVM信息;
查看可以访问的Web端点;
Spring Boot Admin结合Spring Cloud 注册中心使用,只需将admin-server和注册中心整合即可,admin-server 会自动从注册中心获取服务列表,然后挨个获取监控信息。这里以Eureka注册中心为例来介绍下该功能。
在pom.xml中添加相关依赖:
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
在application-eureka.yml中进行配置,只需添加注册中心配置即可:
server:
port: 9301
spring:
application:
name: admin-server
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
在启动类上添加@EnableDiscoveryClient来启用服务注册功能:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
在pom.xml中添加相关依赖:
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
在application-eureka.yml中进行配置,删除原来的admin-server地址配置,添加注册中心配置即可:
server:
port: 9305
spring:
application:
name: admin-client
boot:
admin:
client:
# 配置admin-server地址
url: http://localhost:9301
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
# 添加开启admin的日志监控
logging:
file: admin-client.log
在启动类上添加@EnableDiscoveryClient来启用服务注册功能:
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
启动eureka-server,使用application-eureka.yml配置启动admin-server,admin-client;
查看注册中心发现服务均已注册:http://localhost:8001/
查看Spring Boot Admin 主页发现可以看到服务信息:http://localhost:9301
我们可以通过给admin-server添加Spring Security支持来获得登录认证功能。
在pom.xml中添加相关依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-starter-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
在application.yml中进行配置,配置登录用户名和密码,忽略admin-security-server的监控信息:
server:
port: 9301
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
spring:
application:
name: admin-security-server
security:
user:
# 配置登录用户名和密码
name: root
password: 123456
boot:
admin:
discovery:
# 不显示admin-security-server的监控信息
ignored-services: ${spring.application.name}
对SpringSecurity进行配置,以便admin-client可以注册:
@Configuration
public 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");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
//1.配置所有静态资源和登录页可以公开访问
.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().and()
.csrf()
//4.开启基于cookie的csrf保护
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
//5.忽略这些路径的csrf保护以便admin-client注册
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
}
}
修改启动类,开启AdminServer及注册发现功能:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminSecurityServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminSecurityServerApplication.class, args);
}
}
启动eureka-server,admin-security-server,访问Spring Boot Admin 主页发现需要登录才能访问:http://localhost:9301
springcloud-learning
├── eureka-server -- eureka注册中心
├── admin-server -- admin监控中心服务
├── admin-client -- admin监控中心监控的应用服务
└── admin-security-server -- 带登录认证的admin监控中心服务
GitHub项目源码地址