Vertx:eventBus模块间通信-cluster

来源:互联网 发布:流媒体服务器 linux 编辑:程序博客网 时间:2024/06/06 05:51
在知道了vertx上EventBus通信的基础上,做了一个项目,然后让两个module通信

module1-verticle: eventBus.send(XXX)

module2-verticle: eventBus.consume(XXX)


恩,应该可以了,于是:

java -jar module1-fat.jar

java -jar module2-fat.jar

发现找不到服务,也不能通信。

查找了两天资料,发现:eventBus模块间通信是需要用到 集群 部署的。

同上的两个启动方法,如果加入:

<dependency>    <groupId>io.vertx</groupId>    <artifactId>vertx-hazelcast</artifactId></dependency>
再同样的使用:

java -jar module1-fat.jar -cluster

java -jar module2-fat.jar -cluster

就可以通信了。

以集群方式启动Verticle,相应的EventBus也是集群的,才可以在进程间通信。

其实 hazelcast 是一个跟redis类似的内存缓存工具,在这样一个工具上,模块使用tcp/ip协议跟hazelcast通信,然后间接实现两个模块的通信。


这里留一个hazelcast 的 cluster配置文件和Launcher工具类,以便后来参考:

<?xml version="1.0" encoding="UTF-8"?><hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.6.xsd"           xmlns="http://www.hazelcast.com/schema/config"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  <properties>    <property name="hazelcast.memcache.enabled">false</property>    <property name="hazelcast.rest.enabled">false</property>    <property name="hazelcast.wait.seconds.before.join">0</property>    <property name="hazelcast.logging.type">jdk</property>    <property name="hazelcast.health.monitoring.delay.seconds">2</property>    <property name="hazelcast.max.no.heartbeat.seconds">5</property>    <property name="hazelcast.max.no.master.confirmation.seconds">10</property>    <property name="hazelcast.master.confirmation.interval.seconds">10</property>    <property name="hazelcast.member.list.publish.interval.seconds">10</property>    <property name="hazelcast.connection.monitor.interval">10</property>    <property name="hazelcast.connection.monitor.max.faults">2</property>    <property name="hazelcast.partition.migration.timeout">10</property>    <property name="hazelcast.migration.min.delay.on.member.removed.seconds">3</property>  </properties>  <network>    <port auto-increment="true" port-count="10000">5701</port>    <outbound-ports>      <ports>0</ports>    </outbound-ports>    <join>      <multicast enabled="false"/>      <tcp-ip enabled="true">        <interface>127.0.0.1</interface>      </tcp-ip>    </join>    <interfaces enabled="false">      <interface>10.10.1.*</interface>    </interfaces>    <ssl enabled="false"/>    <socket-interceptor enabled="false"/>    <symmetric-encryption enabled="false">      <!--         encryption algorithm such as         DES/ECB/PKCS5Padding,         PBEWithMD5AndDES,         AES/CBC/PKCS5Padding,         Blowfish,         DESede      -->      <algorithm>PBEWithMD5AndDES</algorithm>      <!-- salt value to use when generating the secret key -->      <salt>thesalt</salt>      <!-- pass phrase to use when generating the secret key -->      <password>thepass</password>      <!-- iteration count to use when generating the secret key -->      <iteration-count>19</iteration-count>    </symmetric-encryption>  </network>  <partition-group enabled="false"/>  <executor-service name="default">    <pool-size>16</pool-size>    <!--Queue capacity. 0 means Integer.MAX_VALUE.-->    <queue-capacity>0</queue-capacity>  </executor-service>  <map name="__vertx.subs">    <!--        Number of backups. If 1 is set as the backup-count for example,        then all entries of the map will be copied to another JVM for        fail-safety. 0 means no backup.    -->    <backup-count>1</backup-count>    <!--  Maximum number of seconds for each entry to stay in the map. Entries that are  older than <time-to-live-seconds> and not updated for <time-to-live-seconds>  will get automatically evicted from the map.  Any integer between 0 and Integer.MAX_VALUE. 0 means infinite. Default is 0.-->    <time-to-live-seconds>0</time-to-live-seconds>    <!--  Maximum number of seconds for each entry to stay idle in the map. Entries that are  idle(not touched) for more than <max-idle-seconds> will get  automatically evicted from the map. Entry is touched if get, put or containsKey is called.  Any integer between 0 and Integer.MAX_VALUE. 0 means infinite. Default is 0.-->    <max-idle-seconds>0</max-idle-seconds>    <!--        Valid values are:        NONE (no eviction),        LRU (Least Recently Used),        LFU (Least Frequently Used).        NONE is the default.    -->    <eviction-policy>NONE</eviction-policy>    <!--        Maximum size of the map. When max size is reached,        map is evicted based on the policy defined.        Any integer between 0 and Integer.MAX_VALUE. 0 means        Integer.MAX_VALUE. Default is 0.    -->    <max-size policy="PER_NODE">0</max-size>    <!--        When max. size is reached, specified percentage of        the map will be evicted. Any integer between 0 and 100.        If 25 is set for example, 25% of the entries will        get evicted.    -->    <eviction-percentage>25</eviction-percentage>    <!--        While recovering from split-brain (network partitioning),        map entries in the small cluster will merge into the bigger cluster        based on the policy set here. When an entry merge into the        cluster, there might an existing entry with the same key already.        Values of these entries might be different for that same key.        Which value should be set for the key? Conflict is resolved by        the policy set here. Default policy is PutIfAbsentMapMergePolicy        There are built-in merge policies such as        com.hazelcast.map.merge.PassThroughMergePolicy; entry will be added if there is no existing entry for the key.        com.hazelcast.map.merge.PutIfAbsentMapMergePolicy ; entry will be added if the merging entry doesn't exist in the cluster.        com.hazelcast.map.merge.HigherHitsMapMergePolicy ; entry with the higher hits wins.        com.hazelcast.map.merge.LatestUpdateMapMergePolicy ; entry with the latest update wins.    -->    <merge-policy>com.hazelcast.map.merge.LatestUpdateMapMergePolicy</merge-policy>  </map>  <!-- Used internally in Vert.x to implement async locks -->  <semaphore name="__vertx.*">    <initial-permits>1</initial-permits>  </semaphore></hazelcast>

然后是一个自定义Launcher类的例子:

package io.vertx.workshop.common;import io.vertx.core.DeploymentOptions;import io.vertx.core.VertxOptions;import io.vertx.core.json.DecodeException;import io.vertx.core.json.JsonObject;import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner;/** * @author <a href="http://escoffier.me">Clement Escoffier</a> */public class Launcher extends io.vertx.core.Launcher {  public static void main(String[] args) {    new Launcher().dispatch(args);  }  @Override  public void beforeStartingVertx(VertxOptions options) {    options.setClustered(true)        .setClusterHost("127.0.0.1");  }  @Override  public void beforeDeployingVerticle(DeploymentOptions deploymentOptions) {    super.beforeDeployingVerticle(deploymentOptions);    if (deploymentOptions.getConfig() == null) {      deploymentOptions.setConfig(new JsonObject());    }    File conf = new File("src/conf/config.json");    deploymentOptions.getConfig().mergeIn(getConfiguration(conf));  }  private JsonObject getConfiguration(File config) {    JsonObject conf = new JsonObject();    if (config.isFile()) {      System.out.println("Reading config file: " + config.getAbsolutePath());      try (Scanner scanner = new Scanner(config).useDelimiter("\\A")) {        String sconf = scanner.next();        try {          conf = new JsonObject(sconf);        } catch (DecodeException e) {          System.err.println("Configuration file " + sconf + " does not contain a valid JSON object");        }      } catch (FileNotFoundException e) {        // Ignore it.      }    } else {      System.out.println("Config file not found " + config.getAbsolutePath());    }    return conf;  }}



原创粉丝点击