目录
一、配置监控地址方式Spring Boot Admin搭建
1、服务端
1)、@SpringBootApplication启动类
2)、配置信息
2、客户端
1)、maven依赖
2)、@SpringBootApplication启动类
3)、配置信息
3、启动服务,查看效果
二、注册中心方式的Spring Boot Admin搭建
1、服务端
1)、maven依赖
2)、@SpringBootApplication启动类
3)、配置信息
2、客户端
1)、maven依赖
2)、@SpringBootBootstrap启动类
2)、配置文件
3、启动服务,查看效果
项目GitHub地址为:https://github.com/kevin-lihongmin/spring-boot-project-kevin/tree/master/admin-monitor
Spring Boot Admin与其他的Eureka Server、Consul等一样都是分为服务端和客户端,服务端本身也可以将自身服务注册到Spring Boot Admin监控中心。提供了两种注册方式,一种是直接添加监控中心的地址,进行注册,一种是利用注册中心进行动态的注入发现。这点类型Spring Cloud的Ribbon,提供离线配置和动态发现配置方式(具体可以参考Spring Cloud Ribbon负载均衡实现)。若链接注册中心Eureka Server为例,可以参考:Spring Cloud Eureka Server集群和客户端调用
Spring Boot Admin利用Spring Boot的运维模块Endpoints,将每个节点的运维数据作为客户端,注册到独立监控中心,提供日志、jvm、env等Actuator的Endpoints的监控数据查看。自己觉得服务规模不大,并且对于小公司快速搭建自己的各维度监控平台是非常方便的。但是当规模比较大时,可能像大公司一样还是需要搭建自己内部的日志平台(类似Elk)、jvm平台等。个人理解,欢迎讨论。
应用根据是服务端还是客户端分别引入相关的maven依赖即可:
1.8 Greenwich.SR1 2.1.5
de.codecentric spring-boot-admin-starter-client de.codecentric spring-boot-admin-starter-server
启动类中除了添加@SpringBootApplication, 还需要添加@EnableAdminServer
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableAdminServer
@SpringBootApplication
public class AdminMonitorApplication {
public static void main(String[] args) {
SpringApplication.run(AdminMonitorApplication.class, args);
}
}
# 服务端口 server.port = 3000 # 服务名称 spring.application.name = spring-boot-admin # 运维端口 management.server.port = 3001 # 开发endpoint的服务 management.endpoints.web.exposure.include = * # 开启endpoint开关 management.endpoint.auditevents.enabled = true
客户端除了需要引入spring-boot-admin-starter-client,如果需要在management.endpoints.web.exposure.include中添加jolokia依赖或者指定为 * ,则需要添加maven依赖:
org.jolokia jolokia-core
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AdminMonitorClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminMonitorClientApplication.class, args);
}
}
配置信息中主要是配置Spring Boot Admin服务端的地址 spring.boot.admin.client.url = http://127.0.0.1:3000
当然还需要开放运营的endpoint模块,management.endpoints.web.exposure.include,当前可以直接使用 * 开放全部模块,也可以逗号间隔的形式开放具体的模块(info,beans,caches,env,health,heapdump,httptrace,jolokia,logfile,loggers,metrics,
mappings,threaddump)。具体:
# 服务端口 server.port = 3005 spring.application.name = spring-boot-admin-client # 运维端口 management.server.port = 3006 # 开放endpoint的服务 management.endpoints.web.exposure.include = * # 开启endpoint开关 management.endpoint.auditevents.enabled = true management.endpoint.health.show-details=always spring.application.admin.enabled = true # 服务监控中心的地址,一定是监控中心的server.port的地址 spring.boot.admin.client.url = http://127.0.0.1:3000
使用Eureka Server监控中心进行服务注册和发现的Spring Boot Admin服务端,还需要添加额外的配置,说明如下:
1、spring-boot-starter-web下可以排掉spring-boot-starter-tomcat包,内嵌服务器使用spring-boot-starter-jetty
2、jolokia-core是官方推荐的一个监控模块
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-security org.jolokia jolokia-core com.lmax disruptor 3.3.7 org.springframework.boot spring-boot-starter-log4j2 org.apache.logging.log4j log4j-web 2.12.0 org.springframework.boot spring-boot-starter-jetty
启动类中需要在@EnableAdminServer的基础上,添加@EnableAutoConfiguration;Eureka客户端的注解@EnableEurekaClient或@EnableDiscoveryClient
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
@EnableAutoConfiguration
@EnableAdminServer
@EnableEurekaClient
@SpringBootApplication
public class SpringBootAdminEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminEurekaApplication.class, args);
}
@Order(101)
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()//
.and().csrf().disable();
}
}
@Order(102)
@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 {
// @formatter:off
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();
// @formatter:on
}
}
}
安装官方推荐,我们在项目上使用的时候也配置了Spring Security相关的配置,如下:
application.properties
# 应用名称 spring.application.name = spring-boot-admin-eureka # 服务端口 server.port = 3006 # 根目录路径 server.servlet.context-path = / # 监控服务端口 management.server.port = 3007 # 开放endpoints的端点: info, health management.endpoints.web.exposure.include = * management.endpoint.health.show-details = always # spring boot admin 配置 spring.boot.admin.discovery.enabled = true # Spring Security相关配置 spring.security.user.name = admin spring.security.user.password = admin
bootstrap.properties
##### Eureka 相关配置信息 开始 ##### # 配置信息参考 EurekaClientConfigBean(eureka.client客户端配置)和 EurekaInstanceConfigBean(eureka.instance实例配置) # ribbon.eureka.enabled = true # Eureka Server 服务URL,defaultZone 客户端注册 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/,\ http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/ # 是否从eureka上获取注册信息 eureka.client.fetch-registry = true # 开启Eureka客户端的健康检查 eureka.client.healthcheck.enabled = true # 获取注册信息时间间隔 eureka.client.registryFetchIntervalSeconds = 5 # 调整客户端应用状态信息上报的周期 默认30 eureka.client.instanceInfoReplicationIntervalSeconds = 5 # 是否注册为服务 eureka.client.registerWithEureka = true # 是否获取注册信息 eureka.client.fetchRegistry = true eureka.instance.metadata-map.user.name = admin eureka.instance.metadata-map.user.password = admin
org.jolokia
jolokia-core
com.lmax
disruptor
3.3.7
de.codecentric
spring-boot-admin-starter-client
启动类中添加@EnableEurekaClient即可:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class SpringBootAdminEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminEurekaClientApplication.class, args);
}
}
配置文件中只是添加Eureka Server注册中心的地址即可:
application.properties
# 应用名称 spring.application.name = spring-boot-admin-eureka-client # 服务端口 server.port = 3008 # 根目录路径 server.servlet.context-path = / # 监控服务端口 management.server.port = 3009 # 开放endpoints的端点: info, health management.endpoints.web.exposure.include = info,beans,caches,env,health,heapdump,\ httptrace,jolokia,logfile,loggers,metrics,mappings,threaddump management.endpoint.health.show-details = always
bootstrap.properties
##### Eureka 相关配置信息 开始 ##### # 配置信息参考 EurekaClientConfigBean(eureka.client客户端配置)和 EurekaInstanceConfigBean(eureka.instance实例配置) # ribbon.eureka.enabled = true # Eureka Server 服务URL,defaultZone 客户端注册 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/,\ http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/ # 是否从eureka上获取注册信息 eureka.client.fetch-registry = true # 开启Eureka客户端的健康检查 eureka.client.healthcheck.enabled = true # 获取注册信息时间间隔 eureka.client.registryFetchIntervalSeconds = 5 # 调整客户端应用状态信息上报的周期 默认30 eureka.client.instanceInfoReplicationIntervalSeconds = 5 # 是否注册为服务 eureka.client.registerWithEureka = true # 是否获取注册信息 eureka.client.fetchRegistry = true eureka.instance.metadata-map.user.name = admin eureka.instance.metadata-map.user.password = admin