这篇文章将讲解 Vert.x 应用相关的 Operation。
Modules
集群模式
Vert.x 支持多种集群模式,详情参考官网,这里使用 Zookeeper 作为集群注册中心,所以你需要准备一个 ZK 的服务。
添加依赖
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-zookeeper</artifactId></dependency>
复制代码
添加 zookeeper.json 到 classpath,内容为 zk 的配置,使用相同的 rootPath 将会共享 EventBus,SharedData 等。
{ "zookeeperHosts": "localhost", "sessionTimeout": 20000, "connectTimeout": 3000, "rootPath": "train.demo", "retry": { "policy": "exponential_backoff", "initialSleepTime": 100, "intervalTimes": 10000, "maxTimes": 5 }}
复制代码
修改代码,将部署变成集群模式,设置 cluster manager 为ZookeeperClusterManager。
ClusterManager mgr = new ZookeeperClusterManager();VertxOptions options = new VertxOptions().setClusterManager(mgr);Vertx.clusteredVertx(options).onComplete(ar -> { if (ar.succeeded()) { ar.result().deployVerticle(new ConfigVerticle()); }});
复制代码
配置中心
默认配置为 classpath:/conf/config.json,默认将其直接转成 JsonObject,核心类为ConfigRetriever
添加依赖,如果需要支持其他格式,例如 Yaml,需要额外添加 yaml 的依赖。
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-config</artifactId></dependency><dependency> <groupId>io.vertx</groupId> <artifactId>vertx-config-yaml</artifactId></dependency>
复制代码
获取配置
ConfigRetriever retriever = ConfigRetriever.create(vertx);retriever.getConfig(ar -> { if (ar.succeeded()) { JsonObject config = ar.result().getJsonObject(); // pass config to another modules. }});
复制代码
监听配置变化,让其作为配置中心,创建一个 RetrieverStore,指定配置文件地址,配置configRetriever.listen(),通过 EventBus 发布修改过来的配置。
ConfigRetrieverOptions configRetrieverOptions = new ConfigRetrieverOptions();configRetrieverOptions.addStore(new ConfigStoreOptions() .setType("file") .setFormat(type) .setOptional(false) .setConfig(new JsonObject().put("path", f)) // f为配置文件路径);ConfigRetriever configRetriever = ConfigRetriever.create(vertx, configRetrieverOptions);configRetriever.listen(change -> { if (!change.getPreviousConfiguration().isEmpty()) { LOGGER.info("before: {}, after:{}", change.getPreviousConfiguration(), change.getNewConfiguration()); JsonObject changedConfig = change.getNewConfiguration(); eventBus.publish(name, changedConfig); }});
复制代码
链路跟踪
Vert.x 支持多种链路跟踪,这里选择使用 Zipkin 来做链路追踪,所以你需要先要搭建一个 Zipkin 的服务。
添加依赖
<dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-instrumentation-okhttp3</artifactId></dependency><dependency> <groupId>io.vertx</groupId> <artifactId>vertx-zipkin</artifactId></dependency>
复制代码
添加配置,这里使用全局配置,你也可以对单个 route 进行配置。
VertxOptions options = new VertxOptions() .setClusterManager(mgr) .setTracingOptions( new ZipkinTracingOptions() .setSenderOptions( new HttpSenderOptions().setSenderEndpoint("http://zipkin.kevin.com/api/v2/spans")) // 指定zipkin的地址 .setServiceName("demo")); // 指定zipkin中服务名称
复制代码
监控
Vert.x 支持多种监控工具,这里选择使用 Prometheus 来做监控,生成一个/metrics接口,然后集成到 Prometheus 配置文件中。
添加依赖
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-micrometer-metrics</artifactId></dependency><dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>${micrometer.version}</version></dependency>
复制代码
生成/metrics接口,同理,这里使用全局配置,你也可以对单个 route 进行配置,选择是否上报数据。
VertxOptions options = new VertxOptions() .setClusterManager(mgr) .setMetricsOptions( new MicrometerMetricsOptions() .setPrometheusOptions( new VertxPrometheusOptions() .setEnabled(true) ) .setEnabled(true) );
复制代码
router.route("/metrics").handler(PrometheusScrapingHandler.create());
复制代码
- job_name: "demo" static_configs: - targets: ["localhost:8888"]
复制代码
最后可以集成 Zipkin 和 Prometheus 到 Grafana 中统一呈现。
评论