1.代码展示
第一个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());
}
});
});
}
}
复制代码
第一个vertx服务verticle
package org.example;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
public class MainClusterVerticle extends AbstractVerticle {
public void start() {
System.out.println("start thread" + Thread.currentThread().getName());
//发布eventbus服务
vertx.eventBus().consumer("com.xiaoniu.bus", msg -> {
System.out.println("read thread" + Thread.currentThread().getName());
System.out.println("收到消息");
System.out.println(msg != null ? ((JsonObject) msg.body()).encodePrettily() : "没有消息");
JsonObject j = new JsonObject();
j.put("info", "我是Main");
msg.reply(j);
});
//发布web服务
// 创建HttpServer
HttpServer server = vertx.createHttpServer();
// 创建路由对象
Router router = Router.router(vertx);
// 监听/index地址
router.route("/index").handler(request -> {
request.response().end("INDEX SUCCESS");
});
// 监听/index地址
router.route("/index1").handler(request -> {
request.response().end("INDEX1 SUCCESS");
});
// 把请求交给路由处理--------------------(1)
server.requestHandler(router);
server.listen(8888);
}
}
复制代码
第二个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.io.FileNotFoundException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @Author: Administrator
* @Description:
* @Date: 2020/7/16 10:42
* @Version: 1.0
*/
public class AppCluster2 {
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 MainClusterVerticle2(), r -> {
if (r.succeeded()) {
System.out.println(MainClusterVerticle2.class.getName() + " --> 部署成功");
} else {
r.cause().printStackTrace();
System.err.println(MainClusterVerticle2.class.getName() + " --> 部署失败, " + r.cause().getMessage());
}
});
});
}
}
复制代码
第二个vertx服务verticle
package org.example;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
public class MainClusterVerticle2 extends AbstractVerticle {
public void start() {
System.out.println("start thread" + Thread.currentThread().getName());
//发布web服务
// 创建HttpServer
HttpServer server = vertx.createHttpServer();
// 创建路由对象
Router router = Router.router(vertx);
// 监听/index地址
router.route("/index").handler(request -> {
System.out.println("haha" + Thread.currentThread().getName());
System.out.println("时间到了,发送消息");
JsonObject json = new JsonObject().put("info", "我是另主");
System.out.println("send thread" + Thread.currentThread().getName());
//通过eventbus发送请求
vertx.eventBus().request("com.xiaoniu.bus", json, msg -> {
System.out.println("read thread" + Thread.currentThread().getName());
if (msg.succeeded()) {
System.out.println(msg.result() != null
? ((JsonObject)msg.result().body()).encodePrettily()
: "没有信息");
} else {
System.err.println(msg.cause().getMessage());
msg.cause().printStackTrace();
}
});
request.response().end("INDEX SUCCESS");
});
// 把请求交给路由处理--------------------(1)
server.requestHandler(router);
server.listen(7777);
}
}
复制代码
2.对外采用http交互
对内采用EventBus请求应答模式
请求
3.目录结构
4.效果
http请求
EventBus请求应答