简体   繁体   中英

Spring Properties in Version Control

I've created the following properties files that are all checked into git:

   **application.properties**  
    spring.application.name=my-service  
    spring.cloud.config.fail-fast=true    
    spring.cloud.config.uri=${CONFIG_URI:http://localhost:8888}
    spring.cloud.loadbalancer.ribbon.enabled=false

   **application-dev.properties**  
    spring.cloud.config.enabled=false  
    eureka.client.enabled=false  
    spring.application.name=my-service  
    spring.datasource.url=jdbc:mysql://${DB_URL:http://localhost:3306/db_example}
    spring.datasource.username=dev
    spring.datasource.password=local

In production all configurations are retrieved from the config server.

Questions regarding this setup:

  1. Developer B starts working on this project and clones it. Unfortunately his local database is on another port with another user. How would he change it? Use the specified environment variables?
  2. Developer B has an issue in a filter and wants to set spring security trace on DEBUG. Again, how can he modify it without polluting the git repository?

What options I see:

  1. Environment variables
  2. A git ignores -local.properties file that every developer can setup locally. There I can set trace levels etc.

Something I'm missing? I want a ready-to-go dev profile, but of course adjustments are necessary for every individual developers machine.

You can define profiles for building spring which would be passed to project build time. So define as much as profile properties file you like as :

application-prod.properties
application-user1.properties
application-user2.properties
application-user3.properties

you can load your Properties by passing VM-Options in your project configuration as

-Dspring.profiles.active=user2

Please note that you can define common properties which will be used by all profiles as:

application.properties

and set your default profile in application.properties as:

spring.profiles.active=prod

Each user might have its proper Database scheme based on. :

spring.datasource.url=jdbc:myql://host:port/database_${user.lowername}


spring.datasource.username=database_${user.lowername}
spring.datasource.password=database_${user.lowername}

$user is an environment variable stored in the os itself

I've seen this a couple different ways at my company:

  1. Environment variables like you have in your example
  2. JVM options like -DuserDatasourceUrl=jdbc:mysql:http://localhost:3306/db_example in the project's run configuration. This still requires work by developers to provide their database configuration, but with a good "how to run locally" section in the README.md it might be another option for you.

Spring Boot's Externalized Configuration documentation was really helpful for us in understanding our options and the PropertySource order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM