Vertx集群配置

vertx集群配置

1. pom文件中引入vertx-hazelcast jar

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

目前是3.3.3版本

2. resources内加入hazelcast的配置文件cluster.xml

默认vertx会去找vertx-hazelcast.jar内的default-cluster.xml
default-cluster.xml
如果需要自定义配置文件,需要将其放在classpath下,并且命名为cluster.xml,即打成fat包后在主目录下面。
fat包的cluster.xml
eclipse的java项目只需将default-cluster.xml文件copy一份到resources目录内,并修改名字为cluster.xml,打包的时候就会打到classpath的主目录下。
cluster.xml

3. cluster.xml配置

hazelcast的集群有3种方式

  1. 广播multicast模式
<multicast enabled="true">
  <multicast-group>224.2.2.3</multicast-group>
  <multicast-port>54327</multicast-port>
</multicast>

如果为true,同一个网段内的vertx实例会自动发现,并集群
2. ip地址tcp-ip模式

<tcp-ip enabled="true">
  <interface>192.168.1.28</interface>
  <interface>192.168.1.27</interface>
  <interface>192.168.1.26</interface>
</tcp-ip>

需要把要集群的ip地址列在这里
3. aws亚马逊云集群模式

<aws enabled="false">
  <access-key>my-access-key</access-key>
  <secret-key>my-secret-key</secret-key>
  <!--optional, default is us-east-1 -->
  <region>us-west-1</region>
  <!--optional, default is ec2.amazonaws.com. If set, region shouldn't be set as it will override this property -->
  <host-header>ec2.amazonaws.com</host-header>
  <!-- optional, only instances belonging to this group will be discovered, default will try all running instances -->
  <security-group-name>hazelcast-sg</security-group-name>
  <tag-key>type</tag-key>
  <tag-value>hz-nodes</tag-value>
</aws>

一般采用第二种ip地址tcp-ip模式,将其它模式置为false。整体的cluster.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.2.xsd"
           xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <properties>
    <property name="hazelcast.mancenter.enabled">false</property>
    <property name="hazelcast.memcache.enabled">false</property>
    <property name="hazelcast.rest.enabled">false</property>
    <property name="hazelcast.wait.seconds.before.join">0</property>
  </properties>

  <group>
    <name>dev</name>
    <password>dev-pass</password>
  </group>
  <management-center enabled="false">http://localhost:8080/mancenter</management-center>
  <network>
    <port auto-increment="true" port-count="10000">5701</port>
    <outbound-ports>
      <!--
      Allowed port range when connecting to other nodes.
      0 or * means use system provided port.
      -->
      <ports>0</ports>
    </outbound-ports>
    <join>
      <multicast enabled="false">
        <multicast-group>224.2.2.3</multicast-group>
        <multicast-port>54327</multicast-port>
      </multicast>
      <tcp-ip enabled="true">
        <interface>192.168.1.28</interface>
        <interface>192.168.1.27</interface>
        <interface>192.168.1.26</interface>
      </tcp-ip>
      <aws enabled="false">
        <access-key>my-access-key</access-key>
        <secret-key>my-secret-key</secret-key>
        <!--optional, default is us-east-1 -->
        <region>us-west-1</region>
        <!--optional, default is ec2.amazonaws.com. If set, region shouldn't be set as it will override this property -->
        <host-header>ec2.amazonaws.com</host-header>
        <!-- optional, only instances belonging to this group will be discovered, default will try all running instances -->
        <security-group-name>hazelcast-sg</security-group-name>
        <tag-key>type</tag-key>
        <tag-value>hz-nodes</tag-value>
      </aws>
    </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>

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

  <map name="__vertx.haInfo">

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

4. 集群方式运行fat.jar包

java -jar F:\work\i5xwxpro\target\i5xwxpro.jar -cluster

分别在对应的服务器上执行命令即可。

-cluster即是代表集群方式启动

5. nginx负载均衡

    upstream web{
        server 192.168.1.28:8080;
        server 192.168.1.27:8080;
        server 192.168.1.26:8080;
    }

nginx将请求负载到不同的vertx服务器即可

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vertx是一种基于事件驱动的应用程序框架,而MyBatis是一种流行的Java持久层框架。Vertx是轻量级的,可扩展的,适用于构建性能的网络应用程序,而MyBatis则提供了一种优雅的方法来管理应用程序与数据库之间的交互。 Vertx与MyBatis的结合可以在构建Web应用程序时提供更好的开发体验和性能。Vertx通过异步IO和事件驱动的方式,可以快速地处理来自Web客户端的请求。而MyBatis可以通过提供有针对性的SQL语句,方便地对数据库进行操作。 Vertx提供了一个轻量级的Web服务器,可以处理HTTP请求和Websocket连接。而MyBatis可以方便地将数据库查询结果映射为Java对象。通过使用Vertx和MyBatis,我们可以将数据库查询和处理请求的逻辑解耦,使代码更易于理解和维护。 使用Vertx和MyBatis的组合,我们可以轻松地构建可扩展的,性能的Web应用程序。我们可以使用Vertx的事件总线机制来处理不同组件之间的消息传递,同时利用MyBatis的缓存机制提数据库查询的性能。 总的来说,Vertx和MyBatis是一对强大的组合,可以帮助我们构建性能、可扩展的Web应用程序。通过将异步事件驱动的编程模型与优雅的数据库交互方式结合起来,我们可以更轻松地开发复杂的应用程序。 ### 回答2: Vert.x是一个基于JVM的性能、非阻塞、事件驱动的应用框架,它可以用于构建轻量级、可伸缩、可扩展的分布式系统。而MyBatis是一个流行的Java持久化框架,它能够帮助我们方便地进行数据库访问。 Vert.x和MyBatis可以结合使用,以实现效的数据库访问和处理。通常情况下,我们可以使用Vert.x的事件驱动机制来处理Web请求,而MyBatis则用于执行数据库操作。 首先,我们可以使用Vert.x的路由器来处理HTTP请求,并在请求处理程序中调用MyBatis执行数据库查询或更新操作。通过这种方式,我们可以确保数据库的访问是非阻塞的,避免了阻塞其他请求的情况发生。 其次,Vert.x的事件总线机制可以用于将不同模块之间的消息进行传递和处理。我们可以在MyBatis的映射器接口中定义观察者,当数据库发生变化时,MyBatis会向事件总线发布消息,其他模块可以根据需求来订阅这些消息进行相应的处理。 另外,Vert.x的异步特性与MyBatis的异步API结合使用,可以进一步提升应用程序的性能。我们可以使用Vert.x的异步回调机制来执行MyBatis的异步数据库操作,从而避免线程阻塞和资源浪费。 总而言之,Vert.x和MyBatis的结合使用可以为我们提供一个性能、可伸缩的应用程序架构。通过将事件驱动的非阻塞处理与灵活的数据库访问相结合,我们可以构建出更好的分布式系统。 ### 回答3: Vertx是一个开源的、响应式的应用程序平台,可用于构建性能、可伸缩的微服务和分布式系统。MyBatis是一个Java持久层框架,用于管理数据库访问,并且可以将数据库操作与应用程序逻辑分离。那么,Vertx和MyBatis可以结合使用吗? 答案是可以的。Vertx提供了异步的、非阻塞的I/O操作,而MyBatis则可以将I/O操作,例如数据库查询和更新,封装成简洁易用的API。Vertx和MyBatis的结合可以提供效的数据库访问和管理,并且保持系统的响应性和可伸缩性。 在Vertx使用MyBatis非常简单。首先,需要在Vertx应用程序的依赖中添加MyBatis的库。然后,可以通过MyBatis的配置文件来配置数据库连接和SQL映射。接下来,可以在Vertx的事件处理器中使用MyBatis的API来执行数据库操作。 在Vertx使用MyBatis可以享受到Vertx的优势,例如事件驱动的架构和性能的I/O操作。此外,Vertx还提供了丰富的异步编程模型和工具,可以方便地与MyBatis结合使用,例如使用Vertx的Future和Promise来处理异步的数据库操作结果。 总而言之,Vertx和MyBatis的结合可以提供效的数据库访问和管理,并且保持系统的响应性和可伸缩性。这是一种强大的组合,可以用于构建性能、可伸缩的微服务和分布式系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值