springboot基本配置文件

本文介绍了如何通过env.properties文件集中管理不同环境的配置,简化开发、测试和生产环境的配置切换。同时,详细展示了logback-spring.xml配置,包括日志输出位置、日志文件命名和滚动策略,确保日志按天和大小归档,并保留30天。此外,还提及了maven配置中针对不同环境的资源配置以及log4j.properties的日志级别和输出设置。
摘要由CSDN通过智能技术生成

Background

项目多时,开发、测试、生产环境,环境多的时候修改配置文件很麻烦,如果又使用了很多中间件,那修改不同环境的配置就更麻烦了,这里基于一定的规则把变量提取了出来,把需要修改的配置提取了出来,每来个项目只用修改env.properties就行了。这里仅作记录,方便以后不断完善:

在这里插入图片描述

1、banner.txt

banner 在线生成工具:https://www.bootschool.net/ascii

2、logback-spring.xml

日志生成策略

  • 日志文件保留三十天
  • 日志名叫server.log
  • 日志按天、文件大小归档
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds" debug="false">
<!--    <include resource="org/springframework/boot/logging/logback/base.xml"/>-->
<!--    <include resource="org/springframework/boot/logging/logback/defaults.xml" />-->

    <!-- 日志输出位置 -->
    <property name="LOG_DIR" value="logs"/>
    <!-- 日志文件名字 -->
    <property name="LOG_NAME" value="server"/>

    <!--输出到控制台-->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <encoder>
            <pattern>
<!--                [%d{yyyy-MM-dd HH:mm:ss.SSS}] [ %-5level] - [ %thread ] %logger{35}: %msg%n-->
                [%d{yyyy-MM-dd HH:mm:ss.SSS}] [ %-5level] - %logger{35}: %msg%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- 输出到文档 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_DIR}/${LOG_NAME}.log</file>
        <encoder>
            <!-- 日志文档输出格式 -->
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{90}: %msg%n
            </pattern>
            <charset>UTF-8</charset>
        </encoder>
        <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 日志归档 -->
            <fileNamePattern>${LOG_DIR}/${LOG_NAME}-%d{yyyy-MM-dd}-%i.log.gz</fileNamePattern>
            <!-- 日志文档保留天数 -->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
    </appender>


    <root level="DEBUG">
        <appender-ref ref="FILE"/>
    </root>

    <!-- 开发、测试环境 -->
<!--    <springProfile name="dev,test">-->
<!--        <logger name="com.zaxxer.hikari.pool" level="INFO"/>-->
<!--        <logger name="org.apache.ibatis" level="INFO"/>-->
<!--        <logger name="org.mybatis" level="ERROR"/>-->
<!--        <logger name="org.springframework" level="ERROR"/>-->
<!--        <logger name="springfox.documentation" level="ERROR"/>-->
<!--        <logger name="io.lettuce" level="INFO"/>-->
<!--        <logger name="com.zaxxer.hikari" level="INFO"/>-->
<!--        <logger name="com.baomidou" level="INFO"/>-->
<!--        <logger name="io.netty" level="INFO"/>-->
<!--        <logger name="jdk.event.security" level="INFO"/>-->
<!--        <logger name="org.xnio.nio" level="INFO"/>-->
<!--        <logger name="cn.hutool" level="INFO"/>-->
<!--        <logger name="org.hibernate" level="INFO"/>-->
<!--        <logger name="reactor" level="INFO"/>-->
<!--        <logger name="com.cloudansys" level="DEBUG"/>-->
<!--    </springProfile>-->

    <!-- 生产环境 -->
    <!--<springProfile name="prod">
        <logger name="com.zaxxer.hikari.pool" level="ERROR"/>
        <logger name="org.quartz" level="ERROR"/>
        <logger name="org.apache.ibatis" level="ERROR"/>
        <logger name="org.mybatis.spring" level="ERROR"/>
        <logger name="com.ksyun.kbdp.dts" level="INFO"/>
        <logger name="com.cloudansys" level="DEBUG"/>
    </springProfile>-->

    <springProfile name="dev,test">
        <root level="INFO">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="FILE" />
        </root>
    </springProfile>

    <springProfile name="prod">
        <root level="INFO">
            <appender-ref ref="CONSOLE" />
            <appender-ref ref="FILE" />
        </root>
    </springProfile>

</configuration>

3、env.properties

## 项目名转换,用底杠代替横杠
pName=hhtl_gzm

## 项目端口
pPort=8182

## redis 数据库
pRdb=9

4、application.yml

server:
  port: @pPort@
spring:
  profiles:
    active: @environment@
  application:
    name: @project.artifactId@
  mvc:
    date-format: yyyy-MM-dd HH:mm:ss
  jackson:
    locale: zh_CN
    time-zone: GMT+08:00
    date-format: yyyy-MM-dd HH:mm:ss
    deserialization:
      fail-on-unknown-properties: false
#    default-property-inclusion: non_null
mybatis:
  mapper-locations: classpath*:/mybatis/*.xml
  configuration:
    map-underscore-to-camel-case: true

# 查看执行的 sql 语句
#mybatis-plus:
#  configuration:
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

# Swagger配置
swagger:
  host: localhost
  #  title: ${project.name}接口文档
  #  description: ${project.name}接口文档
  enabled: true
  # 禁用swagger时的重定向地址

5、application-dev.yml

##### dev 配置 #######
spring:
  profiles:
    active: dev

  # mysql 配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://host-dev:3306/db_@pName@_dev?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
    username: @pName@_dev
    password: 123
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimum-idle: 5
      maximum-pool-size: 10
      connection-timeout: 30000
      connection-test-query: SELECT 1

  # influx 配置
  influx:
    url: http://host-dev:8086
    user: admin
    password: 123
    database: db_@pName@

  # redis 配置
  redis:
    host: host-dev
    port: 6377
    jedis:
      pool:
        max-active: 100
        max-wait: -1
        max-idle: 10
        min-idle: 0
    password: 123
    timeout: 5000
    database: @pRdb@

6、application-test.yml

##### test 配置 #######
spring:
  profiles:
    active: test

  # mysql 配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://host-test:3306/db_@pName@_test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
    username: @pName@_test
    password: 123
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimum-idle: 5
      maximum-pool-size: 10
      connection-timeout: 30000
      connection-test-query: SELECT 1

  # influx 配置
  influx:
    url: http://host-test:8086
    user: admin
    password: 123
    database: db_@pName@_test

  # redis 配置
  redis:
    host: host-test
    port: 6377
    jedis:
      pool:
        max-active: 100
        max-wait: -1
        max-idle: 10
        min-idle: 0
    password: 123
    timeout: 5000
    database: @pRdb@

7、application-prod.yml

##### prod 配置 #######
spring:
  profiles:
    active: prod

  # mysql 配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://host-prod:3306/db_@pName@?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
    username: @pName@
    password: 123
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimum-idle: 5
      maximum-pool-size: 10
      connection-timeout: 30000
      connection-test-query: SELECT 1

  # influx 配置
  influx:
    url: http://host-prod:8086
    user: admin
    password: 123
    database: db_@pName@

  # redis 配置
  redis:
    host: host-prod
    port: 6377
    jedis:
      pool:
        max-active: 100
        max-wait: -1
        max-idle: 10
        min-idle: 0
    password: 123
    timeout: 5000
    database: @pRdb@

8、maven配置

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <environment>dev</environment>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <environment>test</environment>
        </properties>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <environment>prod</environment>
        </properties>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
    </profile>
</profiles>
<resources>
    <!--排除环境配置文件-->
    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>application-*.yml</exclude>
            <exclude>application.yml</exclude>
        </excludes>
    </resource>

    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <!-- 打包时包含的文件 -->
        <includes>
            <include>application-${environment}.yml</include>
            <include>application.yml</include>
        </includes>
    </resource>
</resources>
<filters>
    <!--指定公共配置文件位置,可以实现 properties 文件变量内部引用-->
    <filter>src/main/resources/env.properties</filter>
</filters>

9、log4j.properties

# level
log4j.rootLogger=INFO, console, file


# console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-50c %x - %m%n

# file 按天生成
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.file=logs/processor.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-50c %x - %m%n


# app
log4j.logger.akka=ERROR
log4j.logger.org.apache.flink=ERROR
log4j.logger.com.zaxxer.hikari.HikariDataSource=ERROR
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WaiSaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值