Skip to content

配置文件

置文件主要包含以下几种:

  1. config-dev.properties
  2. bootstrap.yml
  3. application.yml
  4. common.yml
  5. acuity-xxx-server.yml
  6. logback-spring.xml
  7. spy.properties

config-dev.properties

acuity-boot项目没有这个配置文件,请忽略。 在acuity-cloud项目中,src/mian/filters目录存放了几个 config-${profile.active}.properties文件。 该配置文件中的内容,将会在执行maven编译命令时,替换resource中文件的占位符。常用于解决不同环境下配置的差异性。

properties
nacos.ip=127.0.0.1
nacos.port=8848
nacos.namespace=0361a564-eb23-4d6c-adfc-b0aceb6ecadd
nacos.username=nacos
nacos.password=nacos

seata.namespace=3cca7d98-3b1c-44d3-90e5-86abaaf0048a
seata.ip=192.168.1.181
seata.port=8091

logging.file.path=/Users/acuity/data/projects/logs

sentinel.dashboard=127.0.0.1:8088
nacos.ip=127.0.0.1
nacos.port=8848
nacos.namespace=0361a564-eb23-4d6c-adfc-b0aceb6ecadd
nacos.username=nacos
nacos.password=nacos

seata.namespace=3cca7d98-3b1c-44d3-90e5-86abaaf0048a
seata.ip=192.168.1.181
seata.port=8091

logging.file.path=/Users/acuity/data/projects/logs

sentinel.dashboard=127.0.0.1:8088

在本项目中,主要是解决 =不同的服务= 、=不同的环境=都要用到的参数配置在这个文件中。如:

  • acuity-base-server/src/main/resource/bootstrap.yml
[bootstrap.yml]
yaml
acuity:
  swagger:
    version: '@project.version@'
  nacos:
    ip: ${NACOS_IP:@nacos.ip@}
    port: ${NACOS_PORT:@nacos.port@}
    namespace: ${NACOS_NAMESPACE:@nacos.namespace@}
    username: ${NACOS_USERNAME:@nacos.username@}
    password: ${NACOS_PASSWORD:@nacos.password@}
  seata:
    ip: ${SEATA_IP:@seata.ip@}
    port: ${SEATA_PORT:@seata.port@}
    namespace: ${SEATA_NAMESPACE:@seata.namespace@}
  sentinel:
    dashboard: ${SENTINEL_DASHBOARD:@sentinel.dashboard@}

spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: @project.artifactId@
    # acuity自定义配置,用于acuity-scan-starter扫描接口时,拼接uri前缀。 需要和网关配置的前缀一致
    path: /base
  profiles:
    active: @profile.active@
  cloud:
    sentinel:
      enabled: true
      filter:
        enabled: true
      eager: true  # 取消Sentinel控制台懒加载
      transport:
        dashboard: ${acuity.sentinel.dashboard}
    nacos:
      config:
        server-addr: ${acuity.nacos.ip}:${acuity.nacos.port}
        file-extension: yml
        namespace: ${acuity.nacos.namespace}
        shared-configs:
          - dataId: common.yml
            refresh: true
          - dataId: redis.yml
            refresh: false
          - dataId: database.yml
            refresh: true
          - dataId: rabbitmq.yml
            refresh: false
        enabled: true
        username: ${acuity.nacos.username}
        password: ${acuity.nacos.password}
      discovery:
        username: ${acuity.nacos.username}
        password: ${acuity.nacos.password}
        server-addr: ${acuity.nacos.ip}:${acuity.nacos.port}
        namespace: ${acuity.nacos.namespace}
        metadata: # 元数据,用于权限服务实时获取各个服务的所有接口
          management.context-path: ${server.servlet.context-path:}${spring.mvc.servlet.path:}${management.endpoints.web.base-path:}
          gray_version: zuihou

logging:
  file:
    path: '@logging.file.path@'
    name: ${logging.file.path}/${spring.application.name}/root.log
  config: classpath:logback-spring.xml

# 用于/actuator/info
info:
  name: '@project.name@'
  description: '@project.description@'
  version: '@project.version@'
acuity:
  swagger:
    version: '@project.version@'
  nacos:
    ip: ${NACOS_IP:@nacos.ip@}
    port: ${NACOS_PORT:@nacos.port@}
    namespace: ${NACOS_NAMESPACE:@nacos.namespace@}
    username: ${NACOS_USERNAME:@nacos.username@}
    password: ${NACOS_PASSWORD:@nacos.password@}
  seata:
    ip: ${SEATA_IP:@seata.ip@}
    port: ${SEATA_PORT:@seata.port@}
    namespace: ${SEATA_NAMESPACE:@seata.namespace@}
  sentinel:
    dashboard: ${SENTINEL_DASHBOARD:@sentinel.dashboard@}

spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: @project.artifactId@
    # acuity自定义配置,用于acuity-scan-starter扫描接口时,拼接uri前缀。 需要和网关配置的前缀一致
    path: /base
  profiles:
    active: @profile.active@
  cloud:
    sentinel:
      enabled: true
      filter:
        enabled: true
      eager: true  # 取消Sentinel控制台懒加载
      transport:
        dashboard: ${acuity.sentinel.dashboard}
    nacos:
      config:
        server-addr: ${acuity.nacos.ip}:${acuity.nacos.port}
        file-extension: yml
        namespace: ${acuity.nacos.namespace}
        shared-configs:
          - dataId: common.yml
            refresh: true
          - dataId: redis.yml
            refresh: false
          - dataId: database.yml
            refresh: true
          - dataId: rabbitmq.yml
            refresh: false
        enabled: true
        username: ${acuity.nacos.username}
        password: ${acuity.nacos.password}
      discovery:
        username: ${acuity.nacos.username}
        password: ${acuity.nacos.password}
        server-addr: ${acuity.nacos.ip}:${acuity.nacos.port}
        namespace: ${acuity.nacos.namespace}
        metadata: # 元数据,用于权限服务实时获取各个服务的所有接口
          management.context-path: ${server.servlet.context-path:}${spring.mvc.servlet.path:}${management.endpoints.web.base-path:}
          gray_version: zuihou

logging:
  file:
    path: '@logging.file.path@'
    name: ${logging.file.path}/${spring.application.name}/root.log
  config: classpath:logback-spring.xml

# 用于/actuator/info
info:
  name: '@project.name@'
  description: '@project.description@'
  version: '@project.version@'
  • acuity-system-server/src/main/resource/bootstrap.yml
  • acuity-oauth-server/src/main/resource/bootstrap.yml
  • acuity-generator-server/src/main/resource/bootstrap.yml

在多个服务的bootstrap.yml中,都需要配置nacos、seata、sentinel等组件的参数。对于这些参数,同一个环境不同的服务配置相同。

例如:开发环境、测试环境、生产环境,对于acuity-base-server、acuity-system-server等服务需要链接的nacos是不同的。

  • src/mian/filters下创建3个配置文件

    bash
    config-dev.properties
    config-test.properties
    config-prod.properties
    config-dev.properties
    config-test.properties
    config-prod.properties
  • 修改 acuity-dependencies-parent/pom.xml

    xml
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile.active>dev</profile.active>
            </properties>
            <activation>
              	<!-- 默认激活的属性 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile.active>test</profile.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profile.active>prod</profile.active>
            </properties>
        </profile>
    </profiles>
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile.active>dev</profile.active>
            </properties>
            <activation>
              	<!-- 默认激活的属性 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile.active>test</profile.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profile.active>prod</profile.active>
            </properties>
        </profile>
    </profiles>
  • 修改各个服务下的pom.xml,如:acuity-base-server/pom.xmlacuity-system-server/pom.xml

    xml
    <build>
        <filters>
          	<!-- 这个地址一定要配置正确
    						${profile.active} 这个值会根据执行maven命令时,指定 -P 参数来激活 对应的profile
    					-->
            <filter>../../src/main/filters/config-${profile.active}.properties</filter>
        </filters>
    </build>
    <build>
        <filters>
          	<!-- 这个地址一定要配置正确
    						${profile.active} 这个值会根据执行maven命令时,指定 -P 参数来激活 对应的profile
    					-->
            <filter>../../src/main/filters/config-${profile.active}.properties</filter>
        </filters>
    </build>
  • 编译时,指定参数 若没有指定 -P 参数,将激活activeByDefault=true的配置

    bash
    mvn package –P dev
    mvn package –P test
    mvn package –P prod
    mvn package –P dev
    mvn package –P test
    mvn package –P prod
  • 替换参数 在执行了 mvn package –P xxx命令后,编译器会将 <filtering>true</filtering><directory/>目录中 <includes/>的配置文件中 @value_of_pom@的属性全部替换。

    xml
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>

常见问题

  • 为什么占位符是 @xxx@

    acuity-parent继承了spring-boot-starter-parent,spring-boot-starter-parent属性栏定义了:

    xml
    <properties>
    	<resource.delimiter>@</resource.delimiter>
    </properties>
    <build>
      <pluginManagement>
        <plugins> 
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <configuration>
            <propertiesEncoding>${project.build.sourceEncoding}</propertiesEncoding>
            <delimiters>
              <delimiter>${resource.delimiter}</delimiter>
            </delimiters>
            <useDefaultDelimiters>false</useDefaultDelimiters>
          </configuration>
        </plugin>
      	</plugins>
      </pluginManagement>
    </build>
    <properties>
    	<resource.delimiter>@</resource.delimiter>
    </properties>
    <build>
      <pluginManagement>
        <plugins> 
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <configuration>
            <propertiesEncoding>${project.build.sourceEncoding}</propertiesEncoding>
            <delimiters>
              <delimiter>${resource.delimiter}</delimiter>
            </delimiters>
            <useDefaultDelimiters>false</useDefaultDelimiters>
          </configuration>
        </plugin>
      	</plugins>
      </pluginManagement>
    </build>

    所以编译时,文件中 @xxx@的参数能被替换。

    提示

    如果不指定 <delimiter>@</delimiter>,会使用默认的 ${xxx}作为占位符,这会和springboot的占位符产生冲突。

  • config-dev.properties中没有'@project.version@',为什么也能替换?

    这里的参数主要有两个来源,一个是来自于pom文件中的properties属性,一个是来源于指定的.properties文件 而project.version、project.name等属性是maven的隐藏属性。

本地配置文件

bootstrap.yml和application.yml 存放于项目本地目录:src/main/resource。若application.yml 和bootstrap.yml 在同一目录下:bootstrap.yml 先加载 application.yml后加载。

bootstrap.yml

bootstrap.yml(bootstrap.properties) 用于应用程序上下文的引导阶段,bootstrap.yml 由父Spring ApplicationContext加载。可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。必须引入spring-cloud-starter-bootstrap组件bootstrap.yml才能生效。

xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

在使用springcloud时,配置信息一般从nacos中加载,为了取得nacos中的配置信息,就需要在bootstrap.yml中配置nacos链接参数,让程序在启动时,先读取到nacos链接参数,然后在去nacos中读取配置信息。

application.yml

application.yml(application.properties) 用来定义应用级别的, 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。

提示

文件名.properties /文件名.yml文件名相同且同时存在时,先加载 .properties 文件,再加载 .yml 文件。 但为了你不被其他开发人员问候,一个项目里面最好只用一种后缀的文件!

[优先级

application-${spring.profiles.active}.yml > application.yml > bootstrap-${spring.profiles.active}.yml > bootstrap.yml

nacos配置文件

common.yml、database.yml、redis.yml、rabbitmq.yml、${spring.application.name}.yml${spring.application.name}-${spring.profiles.active}.yml等配置文件存放于nacos中。

  • common.yml、database.yml、redis.yml、rabbitmq.yml 属于共享配置文件,所有服务都会读取。

    yaml
    spring:
      cloud:
        nacos:
          config:
            server-addr: ${acuity.nacos.ip}:${acuity.nacos.port}
            file-extension: yml
            namespace: ${acuity.nacos.namespace}
            shared-configs:
              - dataId: common.yml
                refresh: true
              - dataId: redis.yml
                refresh: false
              - dataId: database.yml
                refresh: true
              - dataId: rabbitmq.yml
                refresh: false
    spring:
      cloud:
        nacos:
          config:
            server-addr: ${acuity.nacos.ip}:${acuity.nacos.port}
            file-extension: yml
            namespace: ${acuity.nacos.namespace}
            shared-configs:
              - dataId: common.yml
                refresh: true
              - dataId: redis.yml
                refresh: false
              - dataId: database.yml
                refresh: true
              - dataId: rabbitmq.yml
                refresh: false
  • ${spring.application.name}.yml${spring.application.name}-${spring.profiles.active}.yml等属于个服务的配置文件。 spring.profiles.active属性用于隔离同一服务的不同环境,spring.application.name用于区分不同的服务。

优先级

${spring.application.name}-${spring.profiles.active}.yml > ${spring.application.name}.yml > rabbitmq.yml > database.yml > redis.yml > common.yml

总结

  1. 编译期间通过 -P ${profile.active} 参数将 config-${profile.active}.properties文件中的参数替换到resource下的各文件中。
  2. 运行期间配置文件的优先级为 ${spring.application.name}-${spring.profiles.active}.yml > ${spring.application.name}.yml > rabbitmq.yml > database.yml > redis.yml > common.yml > application-${spring.profiles.active}.yml > application.yml > bootstrap-${spring.profiles.active}.yml > bootstrap.yml spring.application.name是应用名,spring.profiles.active是当前激活的环境。如:acuity-base-server-dev.yml、acuity-base-server.yml、application-prod.yml。 优先级:排名靠前的文件中的参数可以覆盖排名靠后的文件中的参数。

logback-spring.xml

这个logback的配置文件将整个项目中最常用的日志配置抽取到这里.

  1. 通过springProperty标签设置的参数, 可以在项目的application.yml配置文件中, 设置source属性对其defaultValue进行覆盖
xml
<!-- log.path 属性来源于 yml配置中的 logging.file.path 属性, 若yml中没有配置 logging.file.path, 则取默认值/data/projects/logs ,在logback配置文件的任意地方, 都能使用 ${log.path}读取这个参数. -->

<springProperty scope="context" name="log.path" source="logging.file.path" defaultValue="/data/projects/logs"/>
<!-- log.path 属性来源于 yml配置中的 logging.file.path 属性, 若yml中没有配置 logging.file.path, 则取默认值/data/projects/logs ,在logback配置文件的任意地方, 都能使用 ${log.path}读取这个参数. -->

<springProperty scope="context" name="log.path" source="logging.file.path" defaultValue="/data/projects/logs"/>
yml
logging:
  file: 
    path: /root/logs  # 在yml中设置这个参数后, 日志的生成路径就会到 /root/logs
logging:
  file: 
    path: /root/logs  # 在yml中设置这个参数后, 日志的生成路径就会到 /root/logs
  1. 文件中有类似 ASYNC_CONTROLLER_APPENDER<span> </span>CONTROLLER_APPENDER 之类的Appender, 区别在于

    • ASYNC_<span> </span>开头的Appender采用了 异步输出日志, 而没有 ASYNC_<span> </span>开头的Appender采用了 实时输出日志.
    • 异步输出 性能明显好于 实时输出,
    • 异步输出有一些参数无法记录,所以在生产环境打印的日志文件中, 有很多 ?.
    bash
    # 2个问号 表示那个方法, 那行 , 但由于使用了异步输出, 所以输出为 ? 
    
    [acuity-authority-server:8760:0000:2] 2020-12-15 17:53:12.191[ INFO] 22531 50b3bd53c2f344e790834b587a7f1891] [task-13961:934466928] [top.acuity.basic.database.mybatis.WriteInterceptor.?:?] mapper id=top.acuity.box.authority.dao.common.OptLogExtMapper.insert, userId=0
    # 2个问号 表示那个方法, 那行 , 但由于使用了异步输出, 所以输出为 ? 
    
    [acuity-authority-server:8760:0000:2] 2020-12-15 17:53:12.191[ INFO] 22531 50b3bd53c2f344e790834b587a7f1891] [task-13961:934466928] [top.acuity.basic.database.mybatis.WriteInterceptor.?:?] mapper id=top.acuity.box.authority.dao.common.OptLogExtMapper.insert, userId=0
  2. defaults.xml 中配置了如下信息

    • springProperty: spring 环境配置
    • appender: 定义了Controller、Service、Dao、第三方jar、全局异常类、root等appender
  3. defaults-dev.xml: 实时配置文件,用于配置项目在 开发环境的全局日志输出规则。性能低,实时性高。

  4. defaults-prod.xml : 异步配置文件,用于配置项目在 生产环境的全局日志输出规则。 性能高,实时性低。

  5. 项目运行中的业务日志,请在各自项目的resources目录下配置 logback-spring.xml(生产) 或 logback-spring-dev.xml (开发)

spy.properties

该配置位于acuity-common,用于配置p6spy相关参数。

properties
   modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
   # 自定义日志打印
   logMessageFormat=top.acuity.basic.database.p6spy.TenantP6SpyLogger
   #日志输出到控制台
   appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
   # 使用日志系统记录 sql
   #appender=com.p6spy.engine.spy.appender.Slf4JLogger
   # 设置 p6spy driver 代理
   deregisterdrivers=true
   # 取消JDBC URL前缀
   useprefix=true
   # 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
   excludecategories=info,debug,result,commit,resultset
   # 日期格式
   dateformat=yyyy-MM-dd HH:mm:ss
   # 实际驱动可多个
   #driverlist=com.mysql.cj.jdbc.Driver
   #driverlist=com.microsoft.sqlserver.jdbc.SQLServerDriver
   #driverlist=oracle.jdbc.driver.OracleDriver
   # 是否开启慢SQL记录
   outagedetection=true
   # 慢SQL记录标准 2 秒
   outagedetectioninterval=2
   modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
   # 自定义日志打印
   logMessageFormat=top.acuity.basic.database.p6spy.TenantP6SpyLogger
   #日志输出到控制台
   appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
   # 使用日志系统记录 sql
   #appender=com.p6spy.engine.spy.appender.Slf4JLogger
   # 设置 p6spy driver 代理
   deregisterdrivers=true
   # 取消JDBC URL前缀
   useprefix=true
   # 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
   excludecategories=info,debug,result,commit,resultset
   # 日期格式
   dateformat=yyyy-MM-dd HH:mm:ss
   # 实际驱动可多个
   #driverlist=com.mysql.cj.jdbc.Driver
   #driverlist=com.microsoft.sqlserver.jdbc.SQLServerDriver
   #driverlist=oracle.jdbc.driver.OracleDriver
   # 是否开启慢SQL记录
   outagedetection=true
   # 慢SQL记录标准 2 秒
   outagedetectioninterval=2

欢迎使用天源云Saas快速开发系统