目錄
一、配置監控位址方式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依賴即可:
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> <spring-boot-admin.version>2.1.5</spring-boot-admin.version> </properties>
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency>
一、配置監控位址方式Spring Boot Admin搭建
1、服務端
1)、@SpringBootApplication啟動類
啟動類中除了添加@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);
}
}
2)、配置資訊
# 服務端口 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
2、用戶端
1)、maven依賴
用戶端除了需要引入spring-boot-admin-starter-client,如果需要在management.endpoints.web.exposure.include中添加jolokia依賴或者指定為 * ,則需要添加maven依賴:
<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
2)、@SpringBootApplication啟動類
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);
}
}
3)、配置資訊
配置資訊中主要是配置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
3、啟動服務,檢視效果
二、注冊中心方式的Spring Boot Admin搭建
1、服務端
1)、maven依賴
使用Eureka Server監控中心進行服務注冊和發現的Spring Boot Admin服務端,還需要添加額外的配置,說明如下:
1、spring-boot-starter-web下可以排掉spring-boot-starter-tomcat包,内嵌伺服器使用spring-boot-starter-jetty
2、jolokia-core是官方推薦的一個監控子產品
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </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>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.3.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
2)、@SpringBootApplication啟動類
啟動類中需要在@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
}
}
}
3)、配置資訊
安裝官方推薦,我們在項目上使用的時候也配置了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
2、用戶端
1)、maven依賴
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
2)、@SpringBootBootstrap啟動類
啟動類中添加@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);
}
}
2)、配置檔案
配置檔案中隻是添加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