1 Star 2 Fork 0

sarba_home/vertx-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DashboardVerticle.java 1.99 KB
一键复制 编辑 原始数据 按行查看 历史
zhanghongtao 提交于 2023-12-20 13:23 . vertx 4.x 官网实例提交
package io.vertx.example.kafka.dashboard;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.bridge.PermittedOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.sockjs.SockJSBridgeOptions;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
import io.vertx.kafka.client.consumer.KafkaReadStream;
import java.util.Collections;
public class DashboardVerticle extends AbstractVerticle {
@Override
public void start() {
Router router = Router.router(vertx);
// The event bus bridge handler
SockJSBridgeOptions options = new SockJSBridgeOptions();
options.addOutboundPermitted(new PermittedOptions().setAddress("dashboard"));
router.route("/eventbus*").subRouter(SockJSHandler.create(vertx).bridge(options));
// The web server handler
router.route().handler(StaticHandler.create().setCachingEnabled(false));
// Start http server
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(router).listen(8080, ar -> {
if (ar.succeeded()) {
System.out.println("Http server started");
} else {
ar.cause().printStackTrace();
}
});
// Our dashboard that aggregates metrics from various kafka topics
JsonObject dashboard = new JsonObject();
// Publish the dashboard to the browser over the bus
vertx.setPeriodic(1000, timerID -> {
vertx.eventBus().publish("dashboard", dashboard);
});
// Get the Kafka consumer config
JsonObject config = config();
// Create the consumer
KafkaReadStream<String, JsonObject> consumer = KafkaReadStream.create(vertx, config.getMap(), String.class, JsonObject.class);
// Aggregates metrics in the dashboard
consumer.handler(record -> {
JsonObject obj = record.value();
dashboard.mergeIn(obj);
});
// Subscribe to Kafka
consumer.subscribe(Collections.singleton("the_topic"));
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/sarba/vertx-examples.git
git@gitee.com:sarba/vertx-examples.git
sarba
vertx-examples
vertx-examples
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385