春季版本控制中的属性

huangapple go评论136阅读模式
英文:

Spring Properties in Version Control

问题

我创建了以下属性文件,它们都已经提交到了 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

在生产环境中,所有配置都从配置服务器中获取。

关于此设置的问题:

  1. 开发人员 B 开始处理这个项目并克隆了它。不幸的是,他本地的数据库位于另一个端口,并使用另一个用户。他该如何进行更改?使用指定的环境变量吗?
  2. 开发人员 B 在一个过滤器中遇到问题,并希望将 Spring Security 的跟踪级别设置为 DEBUG。同样,在不污染 Git 仓库的情况下,他如何进行修改?

我看到的选项有:

  1. 环境变量
  2. 忽略 Git 的 -local.properties 文件,每个开发人员可以在本地进行设置。在那里,我可以设置跟踪级别等。

还有其他我可能忽略的内容吗?我希望有一个立即可用的开发配置文件,但当然需要针对每个开发人员的机器进行调整。

英文:

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.

答案1

得分: 1

你可以为构建 Spring 的配置定义配置文件,这些配置文件会在项目构建时传递。因此,您可以根据需要定义多个配置文件,比如:

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

你可以通过在项目配置中传递 VM 选项来加载您的属性,如下所示:

-Dspring.profiles.active=user2

请注意,您可以定义一些通用属性,这些属性将被所有配置文件使用,例如:

application.properties

并且您可以在 application.properties 中设置默认配置文件,如下所示:

spring.profiles.active=prod
英文:

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

答案2

得分: 0

每个用户可能有自己的数据库方案基于以下内容:

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

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

$user 是存储在操作系统中的环境变量。

英文:

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

答案3

得分: 0

我在我们公司看到了几种不同的方法:

  1. 像您在示例中所示的环境变量
  2. 像在项目的运行配置中使用的JVM选项,例如 -DuserDatasourceUrl=jdbc:mysql:http://localhost:3306/db_example。这仍然需要开发人员进行一些工作,以提供他们的数据库配置,但是通过README.md中良好的「如何在本地运行」部分,这可能是您的另一个选择。

Spring Boot的外部化配置文档对我们理解选项和PropertySource顺序非常有帮助。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月28日 19:41:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63633101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定