2016-11-21 75 views
0
  • 操作系統:Linux
  • Vert.x版本:3.3.3
  • 機:兩個與同一子網內 不同IP不同

我想啓用度量監控一個簡單的乒乓應用程序,在這裏我可以看到兩個節點之間發送了多少數據包。vertx運行在羣集啓用

度量的度量標準應推送到事件總線上並由網站使用,提供儀表板。從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> 

如何啓用指標?

+0

您發佈的指標,但你永遠不使用它們。或者你沒有在你的地方發佈代碼。 –

+0

在這個問題的第一部分中提到了官方Vertx示例的鏈接。使用儀表板網站。但主要的問題是,啓動應用程序時沒有啓用這些指標。 似乎沒有考慮給定選項「-Dvertx.metrics.options.enabled = true」。 – AdemC

回答

1

通過在另一場戰爭中更改開始應用程序,特別是發件人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 

的缺陷是,你必須告訴行家,尤其是陰影插件,它們是主要的類。我通過在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> 
+0

請分享你的代碼嗎?我正在努力定義自定義指標... – anhldbk