SpringProfile easily switch between multi-environment configuration file

In the process of project development, we will inevitably encounter switch development, testing, production environment, and configure each environment is certainly different. The traditional approach is to modify the configuration file in the project when packaged. But people do things that will inevitably produce unexpected. Spring provides a multi-environment configuration files exist for us, and the way the selected profile runs only when you run / packaging. Specific description is as follows:

Spring provides two properties:

spring.profiles.active

spring:
  profiles:
#    active: yaya
    active: keats

Specify which file is active, the value is application- {profileName} profileName .yml in. Used to distinguish between different operating environments .

PS: If application.yml yml and activated simultaneously configured with a same key, the value of the activated yml overrides a value of application.yml.

spring.profiles.include

spring:
  profiles:
    include: yayaDB,yayaRedis

Start with the content specified include (merge) multiple of yml. This helps classified management yml file under the same environment

PS: Like the above case where a plurality of values include, in addition to the use of separated input methods may also be used wrap - profileName way, which is a syntax of yml. If the syntax is not clear yml, refer SpringBoot entry and YML file Detailed

spring:
  profiles:
    include:
      - yayaDB # 注意换行和空格
      - yayaRedis

The next item is one example of the DEMO, a configuration of the following items:

1582377859097

Each document reads as follows:

application.yml

spring:
  profiles:
    active: yaya
#    active: keats
server:
  port: 9001 # 如果被激活的 yml 中有相同的配置,会覆盖此值

application-keats.yml

server:
  port: 9000
eureka:
  server:
    enable-self-preservation: false # 关闭自我保护
    eviction-interval-timer-in-ms: 4000 # 剔除服务间隔,单位/ms
  instance:
    hostname: localhost
  client:
    register-with-eureka: false # 是否将自己注册到注册中心
    fetch-registry: false # 是否从Eureka中获取注册信息
    service-url: # Eureka Client 的请求地址
      defaultZone: http://#{eureka.instance.hostname}:#{server.port}/eureka/

application-yaya.yml

spring:
  profiles:
    include: yayaDB,yayaRedis

application-yayaDB.yml

# 专门配置DB 的yml,实际项目中可以根据需求,配置文件多了需要分类就分类,少了用不着分类就一种环境一个YML# 这里因为Eureka服务用不到连接数据库,用端口号配置信息代替
server: 
  port: 9100

application-yayaRedis.yml

# 可以专门用来配置Redis的信息。这里因为用不到Redis。我就用除端口以外的其他配置信息填充了
eureka:
  server:
    enable-self-preservation: false # 关闭自我保护
    eviction-interval-timer-in-ms: 4000 # 剔除服务间隔,单位/ms
  instance:
    hostname: localhost
  client:
    register-with-eureka: false # 是否将自己注册到注册中心
    fetch-registry: false # 是否从Eureka中获取注册信息
    service-url: # Eureka Client 的请求地址
      defaultZone: http://#{eureka.instance.hostname}:#{server.port}/eureka/

After the above configuration according yml, can be released by a different Active Notes and, to achieve a switching environment.

Another way to teach you skills:

Idea will launch the same project multiple times

Select the corresponding class project started, click Edit Configurations

1582378343139

Uncheck: Single instance only

1582378399992

Guess you like

Origin www.cnblogs.com/keatsCoder/p/12347382.html