Sring Boot Admin 2.1.6一看就会的配置 查看日志

背景

因为公司的项目查看日志,需要连服务器跳机,再跳到各自部署的服务器上才能查看日志,很不方便,所以就想搞一个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,其他模块随意。
Sring Boot Admin 2.1.6一看就会的配置 查看日志_第1张图片
Sring Boot Admin 2.1.6一看就会的配置 查看日志_第2张图片

Sring Boot Admin 2.1.6一看就会的配置 查看日志_第3张图片

如果还搭不上,请留言,我看到第一时间回复。

你可能感兴趣的:(Spring)