vert.x实践一——集群及监控

本文介绍了如何部署vert.x的监控中心,并展示了vert.x集群的代码实现,详细解析了集群启动过程及原理,包括各个服务间的TCP和UDP端口作用,以及如何通过心跳保持连接以确保集群稳定性。
摘要由CSDN通过智能技术生成

1.部署监控中心程序

监控中心下载地址
在这里插入图片描述
解压后目录是这样
在这里插入图片描述
我这边是在命令行运行

start.bat 7070 mancenter

表示端口7070 应用根路径 mancenter

启动!
在这里插入图片描述

2.vertx集群代码展示

启动类

package org.example;

import com.hazelcast.config.Config;
import com.hazelcast.config.FileSystemXmlConfig;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.EventBusOptions;
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author: Administrator
 * @Description:
 * @Date: 2020/7/16 10:42
 * @Version: 1.0
 */
public class AppCluster {
   

    public static void main(String[] args) throws  UnknownHostException {
   
        final VertxOptions vertxOptions = new VertxOptions();
        EventBusOptions eventBusOptions = new EventBusOptions();
        // 本机局域网Ip
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        //集群方式启动时监听端口,用于接收数据
        vertxOptions.setEventBusOptions(eventBusOptions).getEventBusOptions().setHost(hostAddress);

      //集群方式启动
        HazelcastClusterManager clusterManager = new HazelcastClusterManager();

        vertxOptions.setClusterManager(clusterManager);
        Vertx.clusteredVertx(vertxOptions, res -> {
   
            Vertx result = res.result();
            result.deployVerticle(new MainClusterVerticle(), r -> {
   
                if (r.succeeded()) {
   
                    System.out.println(MainClusterVerticle.class.getName() + " --> 部署成功");
                } else {
   
                    r.cause().printStackTrace();
                    System.err.println(MainClusterVerticle.class.getName() + " --> 部署失败, " + r.cause().getMessage());
                }
            });
        });
    }
}

Verticle类

package org.example;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonObject;

public class MainClusterVerticle extends AbstractVerticle {
   

  public void start()  {
   
//to do something

  }
}

cluster.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<!--
  The default Hazelcast configuration.

  This XML file is used when no cluster.xml is present.

  To learn how to configure Hazelcast, please see the schema at
  https://hazelcast.com/schema/config/hazelcast-config-3.12.xsd
  or the Reference Manual at https://hazelcast.org/documentation/
-->

<!--suppress XmlDefaultAttributeValue -->
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hazelcast.com/schema/config
           http://www.hazelcast.com/schema/config/hazelcast-config-3.12.xsd">

    <group>
        <name>dev</name>
    </group>
    //监控中心地址
    <management-center enabled="true">http://192.168.2.112:7070/mancenter</management-center>
    <network>
    //TCP监听端口5700,如果占用会使用下一个端口,一直到5800
        <port auto-increment="true" port-count="100">5700</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="true">
                <multicast-group>224.2.2.3</multicast-group>
                <multicast-port>54327</multicast-port<
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值