配置管理

Spring Boot支持.properties.yml两种格式的配置文件,通常情况下,建议选择对象化的YAML配置文件。

配置文件分为两种,bootstrap.xmlapplication-{profile}.xml。前者在应用引导阶段加载,通常用于配置应用的基础信息,如:配置中心地址、应用名、激活的配置等;后者主要配置应用的运行参数,如:MQ地址、数据源、日志等。应用启动时首先加载bootstrap.xml,然后加载spring.profiles.active对应的application.xml

示例的配置文件:

bootstrap.yml
spring:
  application:
    name: demo-application
  profiles:
    active: dev
  boot:
    admin:
      client:
        url: http://localhost:8080
  cloud:
    config:
      discovery:
        enabled: true
        service-id: configserver
      uri: http://localhost:8888
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
application-dev.yml
server:
  port: 8080
logging:
  level:
    org.springframework: error
    com.netflix: error
spring:
  datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    url: jdbc:oracle:thin:@localhost:1521:demo
    username: demo
    password: demo
    hikari:
      connection-test-query: select * from dual
  jpa:
    generate-ddl: true
    show-sql: true
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000

Last updated