gpt4 book ai didi

java - vertx 在集群中启用指标的情况下运行

转载 作者:行者123 更新时间:2023-12-01 09:13:52 32 4
gpt4 key购买 nike

  • 操作系统:Linux
  • Vert.x 版本:3.3.3
  • 机器:两台不同的机器同一子网内的不同IP

我想为一个简单的乒乓应用程序启用指标监控,我可以在其中观察两个节点之间发送了多少数据包。

测量的指标应通过事件总线推送并由网站使用,提供仪表板。复制自 vertx examples on Github

使用以下命令启动应用程序

发件人:

vertx run de.avm.boundary.Sender -cluster -cp target/vertx-ping-pong-3.3.3-fat.jar -Dvertx.metrics.options.enabled=true

接收者

vertx run de.avm.boundary.Receiver -cluster -cp target/vertx-ping-pong-3.3.3-fat.jar

不提供任何指标。

<小时/>

源代码

Sender.java

package de.avm.boundary;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.dropwizard.MetricsService;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.sockjs.BridgeOptions;
import io.vertx.ext.web.handler.sockjs.PermittedOptions;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;

public class Sender extends AbstractVerticle {

Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

@Override
public void start() {
Vertx.currentContext().config();
System.out.println("START SENDER VERTICLE DEPLOYMENT ID: " + deploymentID());

BridgeOptions bridgeOptions = new BridgeOptions().
addOutboundPermitted(
new PermittedOptions().
setAddress("metrics-sender")
).addOutboundPermitted(new PermittedOptions().
setAddressRegex("metrics-receiver")
);

Router router = Router.router(vertx);
router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(bridgeOptions));
router.route().handler(StaticHandler.create());

HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(router::accept).listen(8080);
//why is the service object null ??
MetricsService service = MetricsService.create(vertx.eventBus());
System.out.println("Metrics-Service: " + service);
vertx.setPeriodic(100, new Handler<Long>() {
@Override
public void handle(Long timerID) {
vertx.eventBus().publish("ping-address", "more news! at: " + System.currentTimeMillis());
}
});

vertx.setPeriodic(1000, new Handler<Long>() {
@Override
public void handle(Long timerID) {
JsonObject metrics = service.getMetricsSnapshot(vertx);
vertx.eventBus().publish("metrics-sender", metrics);
System.out.println("Metrics: " + metrics);
}
});
}
}

Receiver.java

package de.avm.boundary;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.dropwizard.MetricsService;

public class Receiver extends AbstractVerticle {

MetricsService service = MetricsService.create(vertx);

@Override
public void start() {
System.out.println("START RECEIVER VERTICLE DEPLOYMENT ID: " + deploymentID());

vertx.eventBus().consumer("ping-address", new Handler<Message<String>>() {
@Override
public void handle(Message<String> message) {
// Reply to it
System.out.println("Received message: " + message.body());
message.reply("pong!");
}

}).completionHandler(new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> event) {
if (event.succeeded()) {
System.out.println("Eventbus registration complete!");
}
}
});

// Send a metrics events every second
vertx.setPeriodic(1000, t -> {
JsonObject metrics = service.getMetricsSnapshot(vertx.eventBus());
vertx.eventBus().publish("metrics-receiver", metrics);
});
}
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.avm</groupId>
<artifactId>vertx-ping-pong</artifactId>
<version>3.3.3</version>


<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dropwizard-metrics</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>${project.version}</version>
<!--<scope>provided</scope>-->
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-js</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Launcher</Main-Class>
<Main-Verticle>de.avm.boundary.Sender</Main-Verticle>
</manifestEntries>
</transformer>
</transformers>
<artifactSet/>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

如何启用指标?

最佳答案

通过在另一场 war 中更改启动应用程序(尤其是发送者和接收者 Verticles)解决了该问题。

对我来说最好的解决方案是启动应用程序

发件人:

java -jar target/vertx-ping-pong-3.3.3-Sender-fat.jar -cluster -Dvertx.metrics.options.enabled=true

接收者

java -jar target/vertx-ping-pong-3.3.3-Receiver-fat.jar -cluster -Dvertx.metrics.options.enabled=true

陷阱是,你必须告诉maven,特别是shade插件,它们的主类是什么。我通过在 pom.xml 的属性部分中定义占位符并在 Maven 构建执行期间传递主 Vericle 的名称来实现此目的。

mvn package -DmainClass=Sender 

这还会导致在目标文件夹中放置一个 fat-jar,其中传递的参数的名称包含在文件名中。

这里是修改后的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.avm</groupId>
<artifactId>vertx-ping-pong</artifactId>
<version>3.3.3</version>
<properties>
<runWithClass>${mainClass}</runWithClass>
</properties>


<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dropwizard-metrics</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-js</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Launcher</Main-Class>
<Main-Verticle>de.avm.boundary.${runWithClass}</Main-Verticle>
</manifestEntries>
</transformer>
</transformers>
<artifactSet/>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-${runWithClass}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

关于java - vertx 在集群中启用指标的情况下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40725664/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com