使用 spring-config-server 的 Vertx 配置:未知配置存储实现:spring-config-server

Vertx config with spring-config-server: unknown configuration store implementation: spring-config-server

我想从 spring 云配置服务器获取我的配置,如 https://vertx.io/docs/vertx-config/java/#_spring_config_server_store

中所述

二手进口:

import io.vertx.config.ConfigRetrieverOptions;
import io.vertx.config.ConfigStoreOptions;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.VertxOptions;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.dns.AddressResolverOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.reactivex.config.ConfigRetriever;
import io.vertx.reactivex.core.Vertx;

// 相关部分:

    final ConfigStoreOptions storeOptions = new ConfigStoreOptions()
            .setType("spring-config-server")
            .setConfig(new JsonObject().put("url", "url-to-server"));

    final ConfigRetrieverOptions options = new ConfigRetrieverOptions()
            .addStore(storeOptions);

我使用 maven 构建 jar,我可以 运行 IntelliJ 中的应用程序。 Jar 包含所有需要的依赖项。但是,如果我通过 CLI "java -jar articfact.jar" 启动 jar,我会收到以下错误:

2020-01-17 10:54:04.121 INFO [main] c.e.Runner - Bootstrapping application... Exception in thread "main" java.lang.IllegalArgumentException: unknown configuration store implementation: spring-config-server (known implementations are: [event-bus, file, json, http, env, sys, directory]) at io.vertx.config.impl.ConfigRetrieverImpl.(ConfigRetrieverImpl.java:111) at io.vertx.config.ConfigRetriever.create(ConfigRetriever.java:53) at com.example.Runner.main(Runner.java:41)

我使用的是 Vertx 版本 3.8.4

可用的配置存储是使用 Java Service Loader 实用程序找到的。这意味着服务加载器将查找类路径中的所有文件,如:

META-INF/services/io.vertx.config.spi.ConfigStoreFactory

这些文件包含可用配置存储工厂的名称。

由于您构建了一个 FAT jar,您的构建过程很可能只保留核心服务文件并丢弃 spring 配置服务器模块附带的服务文件。

您必须配置构建以合并所有这些文件的内容。

Vert.xMaven 插件does it by default,但您也可以使用 Maven shade 插件:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
      <execution>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/services/io.vertx.config.spi.ConfigStoreFactory</resource>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>