Vert.x中实现类似Spring Boot的EKS配置外置与默认值加载
在Vert.x中实现类似Spring Boot的外置配置方案
1. 引入依赖
在构建文件中添加Vert.x配置与Web注解相关依赖:
- Maven:
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-config</artifactId> <version>4.5.1</version> <!-- 使用最新稳定版 --> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-config-yaml</artifactId> <version>4.5.1</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web-api-controller</artifactId> <version>4.5.1</version> </dependency>
- Gradle:
implementation 'io.vertx:vertx-config:4.5.1' implementation 'io.vertx:vertx-config-yaml:4.5.1' implementation 'io.vertx:vertx-web-api-controller:4.5.1'
2. 创建YAML配置文件
在resources目录下创建config.yaml,用${环境变量名:默认值}占位符实现「环境变量优先、默认值兜底」逻辑:
server: port: ${APP_PORT:8080} api-prefix: ${API_PREFIX:/api/v1} database: url: ${DB_URL:jdbc:h2:mem:defaultdb}
EKS ConfigMap映射的环境变量会被自动读取,不存在则使用冒号后的默认值。
3. 加载并全局初始化配置
在Vert.x启动类中,通过ConfigRetriever加载配置,解析占位符后存入全局上下文:
public class MainVerticle extends AbstractVerticle { @Override public void start(Promise<Void> startPromise) throws Exception { ConfigRetrieverOptions options = new ConfigRetrieverOptions() .addStore(new ConfigStoreOptions() .setType("yaml") .setConfig(new JsonObject().put("path", "config.yaml"))); ConfigRetriever retriever = ConfigRetriever.create(vertx, options); retriever.getConfig(config -> { if (config.succeeded()) { JsonObject appConfig = config.result(); // 将配置存入本地共享Map,全局可访问 vertx.sharedData().getLocalMap("app-config").put("config", appConfig); // 启动HTTP服务 startHttpServer(appConfig, startPromise); } else { startPromise.fail(config.cause()); } }); } private void startHttpServer(JsonObject appConfig, Promise<Void> startPromise) { Router router = Router.router(vertx); // 注册注解控制器 WebApiControllerFactory.create(vertx, new ApiController(), router); int port = appConfig.getInteger("server.port"); vertx.createHttpServer() .requestHandler(router) .listen(port, http -> { if (http.succeeded()) { startPromise.complete(); System.out.println("Server started on port " + port); } else { startPromise.fail(http.cause()); } }); } }
4. 在注解路由中直接引用配置项
使用@ConfigValue注解(来自vertx-web-api-controller),直接在控制器方法或字段中注入配置值:
@Controller public class ApiController { // 字段注入配置项 @ConfigValue("server.api-prefix") private String apiPrefix; @Route(path = "/hello", methods = HttpMethod.GET) public void hello(RoutingContext ctx) { ctx.response() .putHeader("Content-Type", "text/plain") .end("Hello from " + apiPrefix); } // 方法参数注入配置项 @Route(path = "/db-info", methods = HttpMethod.GET) public void getDbInfo(@ConfigValue("database.url") String dbUrl, RoutingContext ctx) { ctx.response() .putHeader("Content-Type", "text/plain") .end("Database URL: " + dbUrl); } }
5. 适配EKS ConfigMap
在EKS中创建ConfigMap并映射为环境变量,Vert.x会自动读取对应值:
- 创建ConfigMap:
apiVersion: v1 kind: ConfigMap metadata: name: app-config data: APP_PORT: "8081" API_PREFIX: "/api/v2"
- 在Deployment中引用:
spec: containers: - name: app image: your-app-image envFrom: - configMapRef: name: app-config
此时应用会优先使用ConfigMap中的配置值,而非本地默认值。
内容的提问来源于stack exchange,提问作者Sudhanshu Gupta
相关产品推荐
相关产品推荐

