简体   繁体   English

版本控制中的 Spring 属性

[英]Spring Properties in Version Control

I've created the following properties files that are all checked into git:我创建了以下所有已签入 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.开发人员 B 开始处理这个项目并克隆它。 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.开发人员 B 的过滤器存在问题,并希望在 DEBUG 上设置 spring 安全跟踪。 Again, how can he modify it without polluting the git repository?再次,他如何修改它而不污染git存储库?

What options I see:我看到的选项:

  1. Environment variables环境变量
  2. A git ignores -local.properties file that every developer can setup locally. git 会忽略每个开发人员可以在本地设置的 -local.properties 文件。 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.您可以定义用于构建 spring 的配置文件,这些配置文件将传递到项目构建时间。 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您可以通过在项目配置中传递 VM-Options 来加载您的属性

-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:并将application.properties的默认配置文件设置为:

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 $user 是存储在操作系统本身中的环境变量

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. JVM 选项,如项目运行配置中的-DuserDatasourceUrl=jdbc:mysql:http://localhost:3306/db_example 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.这仍然需要开发人员提供他们的数据库配置,但是在 README.md 中有一个很好的“如何在本地运行”部分,它可能是您的另一个选择。

Spring Boot's Externalized Configuration documentation was really helpful for us in understanding our options and the PropertySource order. Spring Boot 的外部化配置文档对我们理解我们的选项和 PropertySource 顺序非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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