简体   繁体   English

Quarkus 无法使用外部配置

[英]Unable to use external configuration with Quarkus

I am having trouble to use external configuration properties for my Quarkus application.我无法为我的 Quarkus 应用程序使用外部配置属性。

I want to override the database properties.我想覆盖数据库属性。 The application.properties inside the project has properties for dev and test, not for prod.项目中的 application.properties 具有用于开发和测试的属性,而不是用于生产的属性。 The values for prod are inside the external application.properties. prod 的值在外部 application.properties 中。

I read the official guide ( https://quarkus.io/guides/config#overriding-properties-at-runtime ) and tried to place an application.properties file in a folder next to the runner-jar called config.我阅读了官方指南( https://quarkus.io/guides/config#overriding-properties-at-runtime )并尝试将 application.properties 文件放在 runner-jar 旁边名为 config 的文件夹中。 That didn't work.那没有用。 But just passing the properties on the command line didn't work either.但是仅仅在命令行上传递属性也不起作用。

C:.
│   0.1.0-SNAPSHOT-runner.jar
│   0.1.0-SNAPSHOT.jar.original
│
├───config
│       application.properties

Internal:内部的:

%dev.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/postgres
%dev.quarkus.datasource.username=postgres
%dev.quarkus.datasource.password=postgres
%dev.quarkus.datasource.db-kind=postgresql
%dev.quarkus.hibernate-orm.database.generation=none

%test.quarkus.datasource.jdbc.url=jdbc:h2:mem:test
%test.quarkus.datasource.username=sa
%test.quarkus.datasource.password=
%test.quarkus.datasource.db-kind=h2
%test.quarkus.hibernate-orm.database.generation=none

External:外部的:

quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/postgres
quarkus.datasource.username=postgres
quarkus.datasource.password=postgres
quarkus.datasource.db-kind=postgresql
quarkus.hibernate-orm.database.generation=none

With java -jar.\0.1.0-SNAPSHOT-runner.jar I get the following error:使用java -jar.\0.1.0-SNAPSHOT-runner.jar我收到以下错误:

2020-07-24 16:58:16,666 ERROR [io.qua.application] (main) Failed to start application: java.lang.IllegalArgumentException: Parameter 'dataSource' may not be null
        at org.wildfly.common.Assert.checkNotNullParamChecked(Assert.java:71)
        at org.wildfly.common.Assert.checkNotNullParam(Assert.java:49)
        at org.wildfly.security.auth.realm.jdbc.QueryConfiguration.<init>(QueryConfiguration.java:40)
        at org.wildfly.security.auth.realm.jdbc.QueryBuilder.buildQuery(QueryBuilder.java:76)
        at org.wildfly.security.auth.realm.jdbc.JdbcSecurityRealmBuilder.build(JdbcSecurityRealmBuilder.java:51)
        at io.quarkus.elytron.security.jdbc.JdbcRecorder.createRealm(JdbcRecorder.java:42)
        at io.quarkus.deployment.steps.ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.deploy_0(ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.zig:76)
        at io.quarkus.deployment.steps.ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.deploy(ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.zig:36)
        at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:524)
        at io.quarkus.runtime.Application.start(Application.java:90)
        at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:91)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:61)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:38)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:106)
        at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)

2020-07-24 16:58:16,730 ERROR [io.qua.run.Application] (main) Error running Quarkus application: java.lang.RuntimeException: Failed to start quarkus
        at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:649)
        at io.quarkus.runtime.Application.start(Application.java:90)
        at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:91)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:61)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:38)
        at io.quarkus.runtime.Quarkus.run(Quarkus.java:106)
        at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
Caused by: java.lang.IllegalArgumentException: Parameter 'dataSource' may not be null
        at org.wildfly.common.Assert.checkNotNullParamChecked(Assert.java:71)
        at org.wildfly.common.Assert.checkNotNullParam(Assert.java:49)
        at org.wildfly.security.auth.realm.jdbc.QueryConfiguration.<init>(QueryConfiguration.java:40)
        at org.wildfly.security.auth.realm.jdbc.QueryBuilder.buildQuery(QueryBuilder.java:76)
        at org.wildfly.security.auth.realm.jdbc.JdbcSecurityRealmBuilder.build(JdbcSecurityRealmBuilder.java:51)
        at io.quarkus.elytron.security.jdbc.JdbcRecorder.createRealm(JdbcRecorder.java:42)
        at io.quarkus.deployment.steps.ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.deploy_0(ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.zig:76)
        at io.quarkus.deployment.steps.ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.deploy(ElytronSecurityJdbcProcessor$configureJdbcRealmAuthConfig-173765586.zig:36)
        at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:524)
        ... 6 more

With java -Dquarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/postgres -Dquarkus.datasource.username=postgres -Dquarkus.datasource.password=postgres -Dquarkus.datasource.db-kind=postgresql -jar.\0.1.0-SNAPSHOT-runner.jar I get this: With java -Dquarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/postgres -Dquarkus.datasource.username=postgres -Dquarkus.datasource.password=postgres -Dquarkus.datasource.db-kind=postgresql -jar.\0.1.0-SNAPSHOT-runner.jar我明白了:

Error: Could not find or load main class .datasource.jdbc.url=jdbc:postgresql:..localhost:5432.postgres
Caused by: java.lang.ClassNotFoundException: /datasource/jdbc/url=jdbc:postgresql://localhost:5432/postgres

I could only find two similar threads ( How can I override properties in Quarkus? and https://github.com/quarkusio/quarkus/issues/1218 ).我只能找到两个类似的线程( How can I override properties in Quarkus?https://github.com/quarkusio/quarkus/issues/1218 )。 Both mention the method with the application.properties in the config folder.两者都在配置文件夹中提到了带有 application.properties 的方法。 Since this does not work for me and I cannot find any new information, I am somewhat lost...由于这对我不起作用并且我找不到任何新信息,所以我有些迷茫......

I would be grateful for any help!如果有任何帮助,我将不胜感激!

Ok, so it turns out there are several properties which you can not override at runtime.好的,事实证明有几个属性在运行时是不能覆盖的。 Those are marked on the offical overview of all properties ( https://quarkus.io/guides/all-config ).这些都标记在所有属性的官方概述上( https://quarkus.io/guides/all-config )。 There is a little hint about this at the top of the page.页面顶部对此有一点提示。

quarkus.datasource.db-kind is one of those properties. quarkus.datasource.db-kind是这些属性之一。 So all I had to do is include this property in my main application.properties.所以我所要做的就是在我的主 application.properties 中包含这个属性。 After that the external file was found and I could use those values for url, password, and username.之后找到外部文件,我可以将这些值用于 url、密码和用户名。

Max is right when he says that quarkus.datasource.db-kind is a runtime property. Max 说quarkus.datasource.db-kind是运行时属性时是对的。

The only way I got it to work so that I have我让它工作的唯一方法是

%dev 
quarkus.datasource.db-kind=postgres
%qa
quarkus.datasource.db-kind=h2
%prod
quarkus.datasource.db-kind=<otherDBKind>

is to, either use profiling:是,要么使用分析:

gradle/maven quarkusDev -Dquarkus-profile=[dev,qa,prod]

or, if you want to deploy the application and run it through java -jar , you have to compile it with the profile context或者,如果要部署应用程序并通过java -jar运行它,则必须使用配置文件上下文对其进行编译

gradle/maven quarkusBuild -Dquarkus-profile=[dev,qa,prod]

a build for each profile with a different db-kind使用不同的 db-kind 为每个配置文件构建

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

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