vertx在集群中启用了指标

时间:2016-11-21 17:09:51

标签: java vert.x

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

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

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

使用以下命令启动应用

发件人

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

接收机

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);
        }
    });
}
}

不提供任何指标。

源代码

Sender.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);
        });
    }
}

Receiver.java

<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>

的pom.xml

{{1}}

如何启用指标?

1 个答案:

答案 0 :(得分:1)

通过在另一场战争中改变开始的应用程序,特别是Sender和Receiver 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,尤其是它们的主要类别的阴影插件。我通过在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>