使用SpringBoot Admin監控SpringCloud微服務


原文:https://www.cnblogs.com/yangzhilong/p/9378876.html

spring-boot admin的github地址:https://github.com/codecentric/spring-boot-admin

 

本文基於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界面

 

Admin Server的配置文件:

復制代碼
spring.boot.admin.routes.endpoints=env,metrics,dump,jolokia,info,configprops,trace,logfile,restart,shutdown,refresh,heapdump,loggers,auditevents,hystrix.stream
# turbine服務id
spring.boot.admin.turbine.location=crm-admin-server

spring.boot.admin.turbine.clusters=default
turbine.clusterNameExpression=new String("default")
#設置需要監控的serviceId
turbine.app-config=crm-security-rest,crm-security-service,crm-basic-rest,crm-basic-service,crm-customer-open-service,crm-customer-rest,crm-customer-service,crm-report-rest,crm-report-service,crm-gateway,crm-inside-gateway
#同一主機上的服務通過host和port的組合來進行區分
turbine.combine-host-port=true

spring.mail.host=smtp.exmail.qq.com
spring.mail.username=xx@qq.com
spring.mail.password=xxxxxx

spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.socket.factory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socket.factory.fallback=false
spring.mail.properties.mail.smtp.port=465
spring.mail.properties.mail.transport.protocol=smtp

#需要忽略的狀態改變通知,逗號分隔,例如不通知離線到上線的狀態,則填寫為OFFLINE:UP
#spring.boot.admin.notify.mail.ignore-changes=
#接收通知的郵箱地址,逗號分隔
spring.boot.admin.notify.mail.to=yangzhilong@qq.com
#需要抄送的郵箱地址,逗號分隔
#spring.boot.admin.notify.mail.cc=test1@qq.com
#郵件發送者,大部分情況與登錄名相同
spring.boot.admin.notify.mail.from=${spring.mail.username}
#郵件主題,默認是:#{application.name} (#{application.id}) is #{to.status}
spring.boot.admin.notify.mail.subject=${spring.profiles.active} profile's #{application.name} (#{application.id}) is #{to.status}
#郵件內容,默認是:#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}
spring.boot.admin.notify.mail.text=${spring.profiles.active} profile's #{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}
#Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed.默認值:"UNKNOWN:UP"
#spring.boot.admin.notify.mail.ignore-changes=
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM