背景
因爲公司的項目查看日誌,需要連服務器跳機,再跳到各自部署的服務器上才能查看日誌,很不方便,所以就想搞一個Spring Boot Admin(SBA) 來查看日誌。
官網文檔地址
最開始的時候,我全部都用的最新版,springboot 2.3.0.RELEASE ,SBA 2.2.3, spring cloud Hoxton.SR4,然而查看日誌功能始終404,搞了我兩天 404問題描述;百度,google都找了一下,關於2.2.3的SBA是少之又少,能查看日誌的幾乎沒有。
解決
最後我妥協了,換了個2.1.6版本(springboot 2.1.0.RELEASE ,SBA 2.1.6, spring cloud Finchley.SR2 ,可通過Spring Initializr來驗證版本兼容問題),再將client的配置稍改一下就解決了,而且這一改版本,還解決了2.2.3的一個問題,client重啓時,server端不刷新client的狀態。
因爲是結合註冊中心來做的,所以先上eureka的配置
eureka-server
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.xml
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
register-with-eureka: false
fetch-registry: false
啓動類(加上@EnableEurekaServer)
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
admin-server
pom.xml
<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.6</spring-boot-admin.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
spring:
application:
name: admin-server
server:
port: 8080
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
admin-server啓動類(加上
@EnableEurekaClient
@EnableAdminServer
這兩個註解
)
@SpringBootApplication
@EnableEurekaClient
@EnableAdminServer
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
admin-client
最關鍵的配置莫過於客戶端
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
spring:
application:
name: admin-customer1
server:
port: 8081
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
management:
endpoints:
web:
exposure:
include: "*" #暴露所有端點,默認只暴露health和info
# CORS跨域支持
cors:
allowed-origins: "*"
allowed-methods: GET,POST
endpoint:
health:
show-details: ALWAYS
logging:
file: logs/output.log # 這個地方輸入日誌配置裏的輸出路徑,如果不填,頁面上則不顯示logging-logfile模塊,如果填錯找不到,則報404
# 2.2.x之後是logging.file.path
pattern:
file: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"
#輸出日誌格式
啓動類
@SpringBootApplication
@EnableEurekaClient
public class AdminCustomer1Application {
public static void main(String[] args) {
SpringApplication.run(AdminCustomer1Application.class, args);
}
}
在application.yml平級創建logback-spring.xml,因爲SpringBoot官方是推薦這個命名方式。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="APP_Name" value="adminTest" />
<contextName>${APP_Name}</contextName>
<!-- 彩色日誌依賴的渲染類 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日誌格式 -->
<property name="CONSOLE_LOG_PATTERN" value="adminTest >> ${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(LN:%L){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />
<!-- 控制檯輸出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<!-- 按照每天生成日誌文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 這裏我寫的是相對路徑,不建議用相對路徑,我是圖方便 -->
<file>logs/output.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日誌文件每天輸出的文件名-->
<FileNamePattern>logs/output-%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日誌文件保留天數-->
<MaxHistory>15</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日誌消息,%n是換行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n</pattern>
</encoder>
</appender>
<!-- 日誌輸出級別 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
這樣就全部搭建好了,啓動看效果,先啓eureka,其他模塊隨意。
如果還搭不上,請留言,我看到第一時間回覆。