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<