使用SpringBoot Admin監控SpringCloud微服務
阿新 • • 發佈:2018-07-27
urb val gap 中心 1.5 支持 環境 cover ref
本文基於SpringCloud的環境和配置上增加配置信息,而不是從0開始的配置。
一、搭建admin服務端
1、引入pom
<properties> <spring-boot-admin-starter-client.version>1.5.6</spring-boot-admin-starter-client.version> <spring-boot-admin-serve.version>1.5.7</spring-boot-admin-serve.version> <spring-boot-admin-serve-ui.version>1.5.7</spring-boot-admin-serve-ui.version> </properties> <dependencies> <!-- 增加admin server 和 ui支持 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server</artifactId> <version>${spring-boot-admin-serve.version}</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui</artifactId> <version>${spring-boot-admin-serve-ui.version}</version> </dependency> <!-- 增加對hystrix的UI支持,需要服務依賴了hystrix --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-hystrix</artifactId> <version>1.5.7</version> </dependency> <!-- 增加對turbine的集成支持,需要指定相關turbine參數信息 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-turbine</artifactId> <version>1.5.4</version> </dependency> </dependencies>
2、application.properties
# 應用名稱 # 這個變量必須寫在這裏,因為項目啟動的時候需要記錄日誌文件,如果寫在git上講導致生成bootstrap.log和springAppName_IS_UNDEFINED.log spring.application.name=crm-admin-server # 服務端口 server.port=21500 spring.profiles.active=local # 非本地的啟動,註冊中心采用啟動參數傳入,本地測試也在啟動參數中註入 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:20000/eureka/ eureka.instance.lease-renewal-interval-in-seconds=2 eureka.instance.lease-expiration-duration-in-seconds=6 eureka.instance.preferIpAddress=true eureka.client.registryFetchIntervalSeconds=2 #禁用管理的鑒權 management.security.enabled=false #開啟shutdown endpoints.shutdown.enabled=true #禁用shutdown的鑒權 endpoints.shutdown.sensitive=false #開啟重啟支持 endpoints.restart.enabled=true #admin管理的端點(actuator) spring.boot.admin.routes.endpoints=env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream # turbine服務id spring.boot.admin.turbine.location=crm-turbine
3、Main方法
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import de.codecentric.boot.admin.config.EnableAdminServer; @SpringBootApplication @EnableDiscoveryClient @EnableAdminServer public class CrmAdminServerApplication { public static void main(String[] args) { SpringApplication.run(CrmAdminServerApplication.class, args); } }
二、admin客戶端
1、引用pom
對於SpringCloud項目來說會默認依賴actuator,只要再加入如下依賴即可
<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
當然,簡單省事的方案就是直接引用spring-boot-admin-starter-client
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency>
2、application.properties
# 禁用actuator管理端鑒權 management.security.enabled=false # 啟用shutdown host:port/shutdown endpoints.shutdown.enabled=true # 禁用密碼驗證 endpoints.shutdown.sensitive=false # 開啟重啟支持 endpoints.restart.enabled=true # admin的log選項卡用 logging.file=/data/logs/crm/${spring.application.name}.log
3、logback-spring.xml中增加對JMX的支持
<configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <jmxConfigurator/>
這樣做的好處是可以在admin的ui界面動態改變某些的日誌級別。
三、訪問Spring Boot Admin UI界面
使用SpringBoot Admin監控SpringCloud微服務