简体   繁体   English

如何将配置中的集合转换为 Microprofile/Quarkus/Smallrye 中的环境变量

[英]How to convert a collection in config to environment variable in Microprofile/Quarkus/Smallrye

We are running our apps in a K8 Cluster and rely on the configuration by environment variables.我们在 K8 集群中运行我们的应用程序,并依赖于环境变量的配置。 For the conversion of application.properties/application.yaml parameters in Quarkus, the following conversion rules apply: https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc#default-configsources Quarkus中application.properties/application.yaml参数的转换,适用以下转换规则: https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc #default-configsources

In this rule it is not mentioned how to convert collections.这条规则中没有提到如何转换collections。

Let's say I have the following config:假设我有以下配置:

server.environments[0].name=dev
server.environments[0].apps[0].name=rest
server.environments[0].apps[0].services=bookstore,registration
server.environments[0].apps[0].databases=pg,h2
server.environments[0].apps[1].name=batch
server.environments[0].apps[1].services=stock,warehouse

How would I convert it to an environment variable?我如何将它转换为环境变量?

I've tried the following:我试过以下方法:

SERVER_ENVIRONMENT_0_APPS_0_DATABASES
SERVER_ENVIRONMENT[0]_APPS[0]_DATABASES

No chance to make it work.没有机会让它发挥作用。 Does anyone know how to do this?有谁知道如何做到这一点? Is this supported anyway?无论如何都支持吗?

You were pretty close, just follow the rules mentioned in the docu:您非常接近,只需遵循文档中提到的规则即可:

  1. Replace each character that is neither alphanumeric nor _ with _;将每个既不是字母数字也不是 _ 的字符替换为 _; then convert the name to upper case (ie COM_ACME_SIZE)然后将名称转换为大写(即 COM_ACME_SIZE)

So given we have a config property named server.environments[0].apps[0].name when you replace each non-alfanumeric character with _ and convert to upper case you end up with: SERVER_ENVIRONMENTS_0__APPS_0__NAME .因此,假设我们有一个名为server.environments[0].apps[0].name的配置属性,当您将每个非字母数字字符替换为_并转换为大写时,您最终会得到: SERVER_ENVIRONMENTS_0__APPS_0__NAME Note the double underscore between 0 and APPS as you substitute both .请注意0APPS之间的双下划线,因为您将两者都替换了. and [ for _ .[对于_ That will certainly not win any prize for the prettiest env var name but it does the job:).这当然不会因最漂亮的环境变量名称而赢得任何奖项,但它确实起到了作用:)。

You can check how exactly it is done in the Smallrye implementation of MP config - which is the implementation used by Quarkus.您可以在 MP config 的Smallrye 实现中检查它是如何完成的——这是 Quarkus 使用的实现。

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

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