无法在Helidon Mp中设置自定义配置源。

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

Not able to set up Custom Config Source in Helidon Mp

问题

I have made the changes as suggested in the doc: https://helidon.io/docs/v2/#/mp/config/02_MP_config_sources
But it is not able to identify the new config source and gives the following error:
Config source of type test-config-provider is not supported

Here are the changes that I have done to add the new Custom Config Source:

  1. Created a MyConfigProvider class which implements MpMetaConfigProvider
  2. Created class MyConfigSource which implements ConfigSource
  3. MyConfigProvider::create() and supportedTypes()
  1. public static final String TYPE = "test-config-provider";
  2. @Override
  3. public List<? extends ConfigSource> create(String s, Config config, String s1) {
  4. Map<String, String> result = new HashMap<String, String>();
  5. ConfigSource configSource = new MyConfigSource(result);
  6. return Collections.singletonList(configSource);
  7. }
  8. @Override
  9. public Set<String> supportedTypes() {
  10. return Set.of(TYPE);
  11. }
  1. Inside resources/META-INF, added services/io.helidon.config.mp.spi.MpMetaConfigProvider file. In the file added the file package path for MyConfigProvider i.e <package_name>.MyConfigProvider
  2. File: 'mp-meta-config.yaml' contents
    add-discovered-sources: true
    add-discovered-converters: false
    add-default-sources: true

sources:

  • type: "environment-variables"
  • type: "system-properties"
  • type: "test-config-provider"
  1. In my main() function, we are creating the Config instance and passing it to the Server builder()
  1. public class Main {
  2. private static final String DEFAULT_CONFIG_FILE = "mp-meta-config.yaml";
  3. public static void main(final String[] args) {
  4. Config config = buildHelidonConfig();
  5. Server server = startServer1(config);
  6. }
  7. private static Config buildHelidonConfig() {
  8. return MetaConfig.config(Config.create(getSources()));
  9. }
  10. private static Supplier<? extends ConfigSource>[] getSources() {
  11. return new Supplier[] {
  12. classpath(DEFAULT_CONFIG_FILE).build()
  13. };
  14. }
  15. public static Server startServer1(Config config) {
  16. return Server
  17. .builder()
  18. .config(config)
  19. .build()
  20. .start();
  21. }
  22. }

But it is not able to identify the new config source and gives the following error:
Config source of type test-config-provider is not supported

英文:

I have made the changes as suggested in the doc : https://helidon.io/docs/v2/#/mp/config/02_MP_config_sources
But it is not able to identify the new config source and gives the following error :
Config source of type test-config-provider is not supported`

Here are the changes that I have done to add the new Custom Config Source :

  1. Created a MyConfigProvider class which implements MpMetaConfigProvider
  2. Created class MyConfigSource which implements ConfigSource
  3. MyConfigProvider::create() and supportedTypes()
  1. public static final String TYPE = &quot;test-config-provider&quot;;
  2. @Override
  3. public List&lt;? extends ConfigSource&gt; create(String s, Config config, String s1) {
  4. Map&lt;String, String&gt; result = new HashMap&lt;String, String&gt;();
  5. ConfigSource configSource = new MyConfigSource(result);
  6. return Collections.singletonList(configSource);
  7. }
  8. @Override
  9. public Set&lt;String&gt; supportedTypes() {
  10. return Set.of(TYPE);
  11. }
  1. Inside resources/META-INF, added services/io.helidon.config.mp.spi.MpMetaConfigProvider file. In the file added the file package path for MyConfigProvider i.e &lt;package_name&gt;.MyConfigProvider
  2. File : 'mp-meta-config.yaml' contents
    add-discovered-sources: true
    add-discovered-converters: false
    add-default-sources: true

sources:

  • type: "environment-variables"
  • type: "system-properties"
  • type: "test-config-provider"
  1. In my main() function we are creating the Config instance and passing it to the Server builder()
  1. public class Main {
  2. private static final String DEFAULT_CONFIG_FILE = &quot;mp-meta-config.yaml&quot;;
  3. public static void main(final String\[\] args) {
  4. Config config = buildHelidonConfig();
  5. Server server = startServer1(config);
  6. //Server server = startServer2();
  7. }
  8. private static Config buildHelidonConfig() {
  9. return MetaConfig.config(Config.create(getSources()));
  10. }private static Supplier&lt;? extends ConfigSource&gt;[] getSources() {
  11. return new Supplier[] {
  12. classpath(DEFAULT_CONFIG_FILE).build()
  13. };
  14. }
  15. public static Server startServer1(Config config) {
  16. return Server
  17. .builder()
  18. .config(config)
  19. .build()
  20. .start();
  21. }
  22. }

But it is not able to identify the new config source and gives the following error :

Config source of type test-config-provider is not supported

答案1

得分: 1

io.helidon.config.Configorg.eclipse.microprofile.config.Config 是不同的API。

Helidon 提供了一个 MicroProfile Config 的实现,使用以下 Maven 依赖:io.helidon.config:helidon-config-mp

你可以像这样创建一个实例:

  1. org.eclipse.microprofile.config.ConfigProvider.getConfig()

Helidon 还提供了一些 SPIs,其中之一是 io.helidon.config.mp.spi.MpMetaConfigProvider,当使用上述方式创建 MicroProfile 实例时,会使用 MpMetaConfigProvider 的实现。


Helidon 还提供了一个桥接,用于创建由 io.helidon.config.Config 支持的 MicroProfile Config 实例。

  1. // 创建一个 "Helidon Config" 实例
  2. io.helidon.config.Config config0 = io.helidon.config.Config.create();
  3. // 创建一个由 "Helidon Config" 支持的 "MicroProfile Config" 实例
  4. org.eclipse.microprofile.config.Config config =
  5. ConfigProviderResolver.instance()
  6. .getBuilder()
  7. .withSources(MpConfigSources.create(config0))
  8. .build();

上面的代码将不使用 MpMetaConfigProvider 的实现。


为了让你的代码使用你的 MpMetaConfigProvider 实现,你可以选择:

  • 使用内置的主类
  • 使用 Server.Builder(org.eclipse.microprofile.config.Config) 而不是 Server.Builder(io.helidon.config.Config),使用通过 ConfigProvider.getConfig() 创建的实例。
英文:

io.helidon.config.Config and org.eclipse.microprofile.config.Config are different APIs.

Helidon provides an implementation of MicroProfile Config with the following Maven dependency io.helidon.config:helidon-config-mp.

You can create an instance like this:

  1. org.eclipse.microprofile.config.ConfigProvider.getConfig()

Helidon also provides a few SPIs along its implementation of MicroProfile Config, one of which is io.helidon.config.mp.spi.MpMetaConfigProvider.

When creating an instance of MicroProfile using the above, implementations of MpMetaConfigProvider are used.


Helidon also provides a bridge to create instances of MicroProfile Config backed by io.helidon.config.Config.

  1. // create an instance of &quot;Helidon Config&quot;
  2. io.helidon.config.Config config0 = io.helidon.config.Config.create();
  3. // create an instance of &quot;MicroProfile Config&quot; backed by &quot;Helidon Config&quot;
  4. org.eclipse.microprofile.config.Config config =
  5. ConfigProviderResolver.instance()
  6. .getBuilder()
  7. .withSources(MpConfigSources.create(config0))
  8. .build();

The code above will NOT use implementations of MpMetaConfigProvider.


In order for your code to pickup your implementation of MpMetaConfigProvider you can either:

  • Use the built-in main class
  • Use Server.Builder(org.eclipse.microprofile.config.Config) instead of Server.Builder(io.helidon.config.Config) with an instance created using ConfigProvider.getConfig()

huangapple
  • 本文由 发表于 2023年5月17日 16:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270210.html
匿名

发表评论

匿名网友

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

确定