最近在学习spring cloud相关的知识,对服务的监控这一块在网上找到了spring boot admin这个项目,网上的资料比较少,还没看到完整的2.0版本的示例。自己学习了下,写了个简单的demo。特此记录一下配置的过程。
spring boot admin 为Spring Boot应用提供了可视化的监控视图。它基于spring boot actuator模块,因为spring boot actuator 模块为监控Spring Boot 应用程序暴露的大量的管理端点(Endpoints)。可结合eureka、consul、zookeeper等服务注册及发现组件来监控并展示微服务集群中服务的状态及各项指标信息。
首先介绍一下用到的各项框架版本信息:
spring boot:2.0.2.RELEASE
spring cloud:Finchley.RC2
spring boot admin:2.0.1-SNAPSHOT
再介绍下相关的应用:
1、eureka server HA服务,启动两个。
2、spring cloud producer服务,即生产者服务。
3、spring boot admin server服务,即监控程序。
1、2的相关搭建过程这里就不赘述了,各位可以百度一下。
推荐几个博客地址。
纯洁的微笑:http://www.mooooc.com/spring-cloud.html;
程序猿DD:http://blog.didispace.com/。
本人在学习spring cloud过程中,主要是参看他们的文章,在此感谢他们提供的好文。
spring boot admin 的github地址:https://github.com/codecentric/spring-boot-admin
spring boot admin 2.0.0版本的帮助文档:http://codecentric.github.io/spring-boot-admin/2.0.0/
下面就着重说下 spring boot admin server的搭建过程。
pom.xml中添加以下依赖:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动类AdminServerApplication.java代码:
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
@Profile("insecure")
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
@Profile("secure")
@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();
}
}
}
配置文件(参照官方示例:详见点我):
server:
port: 8766
spring:
application:
name: spring-boot-admin-sample-eureka
profiles:
active:
- secure
info:
app:
name: "@project.name@"
description: "@project.description@"
version: "@project.version@"
eureka:
instance:
leaseRenewalIntervalInSeconds: 30
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 15
serviceUrl:
defaultZone: http://eureka1:8761/eureka,http://eureka2:8762/eureka
management:
endpoint:
health:
show-details: ALWAYS
endpoints:
web:
exposure:
include: "*"
security2:
user:
name: "sa"
password: "sa"
---
spring:
profiles: insecure
---
spring:
profiles: secure
security:
user:
name: ${security2.user.name}
password: ${security2.user.password}
eureka:
instance:
metadata-map:
user.name: ${security2.user.name}
user.password: ${security2.user.password}
下面是各种配置的说明:
#health endpoint是否必须显示全部细节。默认情况下, /actuator/health 是公开的,并且不显示细节。
management.endpoint.health.show-details=ALWAYS
#选择暴露的端点
#management.endpoints.web.exposure.include=info,health,env,beans
#暴露所有端点
management.endpoints.web.exposure.include=*
#不暴露的端点
management.endpoints.web.exposure.exclude=env
至此,服务端的程序配置完成。
那么通过eureka服务注册来实现监控producer应用,还要修改下producer程序的配置。
如果producer程序中没有spring boot actuator相关的依赖,则需要加入它。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
通过它来对外暴露一些端点。
spring boot 2.X中management.endpoints.web.exposure.include默认值是info, health;即默认只暴露这两个端点,如果不修改,后面的spring boot admin中的监控页面中只显示该服务的这两个信息,其它的像env、beans、httptrace等信息都不显示。具体的端点有哪些,请见端点列表。
修改下producer的配置文件,增加如下内容:
eureka.instance.leaseRenewalIntervalInSeconds=30
eureka.instance.health-check-url-path=/actuator/health
eureka.client.registryFetchIntervalSeconds=30
#health endpoint是否必须显示全部细节。默认情况下, /actuator/health 是公开的,并且不显示细节。
management.endpoint.health.show-details=ALWAYS
#选择暴露的端点
#management.endpoints.web.exposure.include=info,health,env,beans
#暴露所有端点
management.endpoints.web.exposure.include=*
#不暴露的端点
management.endpoints.web.exposure.exclude=env
至此,producer的修改完成。
依次启动eureka server ha,spring cloud producer,spring boot admin应用,
地址栏输入:http://localhost:8766,进入登录页面,输入配置文件中设置的用户名密码后,进入applications显示页面,如下图所示:

点击 spring-cloud-producer应用后,显示详情,如下图所示:

这里面各个tab页中就是对应的各项监控指标信息。
这样,一个简单的spring boot admin的应用就搭建完成了。
本文介绍如何使用 Spring Boot Admin 实现 Spring Boot 应用的监控,包括配置过程及与 Eureka 的集成。
1546

被折叠的 条评论
为什么被折叠?



