无法在Quarkus中使用外部配置。

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

Unable to use external configuration with Quarkus

问题

我在使用Quarkus应用程序时遇到了问题,无法使用外部配置属性。

我想要覆盖数据库属性。项目中的application.properties文件具有开发和测试环境的属性,但没有生产环境的属性。生产环境的值在外部的application.properties文件中。

我阅读了官方指南(https://quarkus.io/guides/config#overriding-properties-at-runtime),并尝试将一个名为config的文件夹与runner-jar文件平行放置一个application.properties文件。但这并没有起作用。而且仅仅通过命令行传递属性也不起作用。

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

内部配置:

%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

外部配置:

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

使用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

使用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

我只找到两个类似的讨论帖子(https://stackoverflow.com/questions/55043399/how-can-i-override-properties-in-quarkus 和 https://github.com/quarkusio/quarkus/issues/1218)。两者都提到了使用config文件夹中的application.properties文件的方法。因为这对我不起作用,我找不到任何新信息,所以我有点迷茫...

我会非常感谢任何帮助!

英文:

I am having trouble to use external configuration properties for my Quarkus application.

I want to override the database properties. The application.properties inside the project has properties for dev and test, not for prod. The values for prod are inside the external 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. 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:

2020-07-24 16:58:16,666 ERROR [io.qua.application] (main) Failed to start application: java.lang.IllegalArgumentException: Parameter &#39;dataSource&#39; 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.&lt;init&gt;(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 &#39;dataSource&#39; 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.&lt;init&gt;(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:

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 (https://stackoverflow.com/questions/55043399/how-can-i-override-properties-in-quarkus and https://github.com/quarkusio/quarkus/issues/1218). Both mention the method with the application.properties in the config folder. 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!

答案1

得分: 1

好的,原文已翻译如下:

嗯,事实证明在运行时无法覆盖几个属性。这些属性在官方的所有属性概览中有标注(https://quarkus.io/guides/all-config)。页面顶部有关于这方面的小提示。

quarkus.datasource.db-kind 就是其中之一。所以我所需要做的就是在我的主 application.properties 文件中包含这个属性。在那之后,外部文件就能被找到,我可以使用这些值作为 URL、密码和用户名。

英文:

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). There is a little hint about this at the top of the page.

quarkus.datasource.db-kind is one of those properties. So all I had to do is include this property in my main application.properties. After that the external file was found and I could use those values for url, password, and username.

答案2

得分: 0

Max在说quarkus.datasource.db-kind是一个运行时属性时是正确的。

唯一让我成功工作的方式是这样的,这样我就有了:

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

要么使用分析:

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

要么,如果你想部署应用并通过java -jar运行它,你必须使用配置文件上下文编译它:

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

为每个配置文件构建一个具有不同db-kind的构建。

英文:

Max is right when he says that quarkus.datasource.db-kind is a runtime property.

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=&lt;otherDBKind&gt;

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

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

a build for each profile with a different db-kind

huangapple
  • 本文由 发表于 2020年7月24日 23:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63076582.html
匿名

发表评论

匿名网友

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

确定