Vertx集群部署,官方介绍了几种方式,其中有一种方式是使用HazelCast来实现的,下面介绍如何通过HazelCast
来实现Vertx集群部署。
HazelCast默认采用UDP广播的方式实现节点之间的互通,从而实现内部信息共享达到集群目的,前提是节点都在
同一个网段,而且节点如果有多个网卡,节点启动时,还得指定相应的网卡(cluster-host)作为集群之间通信的IP,
网络要求较高。也可以采用TCPIP的方式,通过配置cluster.xml,将节点IP加入tcp-ip节点的interface节点,节点启动
之后会通过指定IP使节点之间通过socket连接构建集群,通过这种方式构建集群,就需要改动hazelcast集群
配置文件cluster.xml。
项目介绍:
该工程实现这样的功能:通过vertx构建一个HttpServer,并且监听8082端口,接收前端的业务需求,最后通过判断,
如果请求(http://hostname:8082/pushservice/2.0/poll)中带有参数type,就利用vertx的EventBus事件机制向注册
pushservice-poll的Verticle发送一个消息,而注册pushservice-poll的Verticle接收到消息之后,会向mongo数据库
中插入一条记录,这样整个的请求就算完成。这里面利用了vertx-web的路由功能,接收指定path(/pushservice
/2.0/poll)请求,还利用了vertx-core的EventBus,实现Verticle之间互相通信。
工程目录结构:
第一步、构建maven项目,配置vertx-web和hazelcast相关依赖;
<properties> <mainClass>com.lenovo.push.start.ClusterStarter</mainClass> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-unit</artifactId> <version>3.3.3</version> <scope>test</scope> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-mongo-client</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-mongo-embedded-db</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-hazelcast</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.7.Final</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.6</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.5</version> <configuration> <excludes> <exclude>/*.properties</exclude> <exclude>/*.xml</exclude> </excludes> <archive> <manifest> <addClasspath>false</addClasspath> <mainClass>${mainClass}</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.3</version> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>replace</goal> </goals> </execution> </executions> <configuration> <file>assemblise/bin/app.sh</file> <replacements> <replacement> <token>#MAIN_CLASS#</token> <value>${mainClass}</value> </replacement> </replacements> <outputFile>target/bin/app.sh</outputFile> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptors> <descriptor>assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
这里面还配置了一个利用Assembly插件打包的工具,日志采用log4j2,引入了vertx-mongo,后面会用来向mongo
数据库中插入数据。
第二步、书写业务代码,实现项目的功能;
配置文件vertx.properties(数据库连接属性和集群配置本例子没有使用集群配置,只是简单的设置了cluster为true)
#VertxOptions vertx.active=VXWEB vertx.VXWEB.pool.size.event.loop=64 vertx.VXWEB.pool.size.worker=256 vertx.VXWEB.pool.size.internal.blocking=256 vertx.VXWEB.cluster.enabled=true vertx.VXWEB.cluster.host=localhost vertx.VXWEB.cluster.port=3000 vertx.VXWEB.cluster.ping.interval=1000 vertx.VXWEB.cluster.ping.interval.reply=1000 vertx.VXWEB.blocked.thread.check.interval=1000 vertx.VXWEB.execute.time.max.event.loop=60000000000 vertx.VXWEB.execute.time.max.worker=60000000000 vertx.VXWEB.ha.enabled=true vertx.VXWEB.ha.group=__DEFAULT__ vertx.VXWEB.quorum.size=1 vertx.VXWEB.warning.exception.time=5000000000 #DeploymentOptions http.port=8082 db_name=pushservice connection_string=mongodb://192.168.56.201:27017
ResourceLoader.java(读取配置文件工具类)
package com.lenovo.push.loader; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; public class ResourceLoader { private static ResourceLoader loader = new ResourceLoader(); private static Map<String, Properties> loadermap = new HashMap<String,Properties>(); private static String DEFAULT_CONFIG_FILE="vertx.properties"; private ResourceLoader(){} public static ResourceLoader getInstance(){ return loader; } public Properties getPropFromProperties(String fileName) throws Exception{ Properties prop = loadermap.get(fileName); if(prop!=null)return prop; String path = getClass().getClassLoader().getResource(fileName).getPath(); prop = new Properties(); prop.load(new FileInputStream(new File(path))); loadermap.put(fileName, prop); return prop; } public String getString(String key) throws Exception{ Properties prop = ResourceLoader.getInstance().getPropFromProperties(DEFAULT_CONFIG_FILE); String value = prop.getProperty(key); return value; } public Integer getInt(String key) throws Exception{ return Integer.parseInt(getString(key)); } public Long getLong(String key) throws NumberFormatException, Exception{ return Long.parseLong(getString(key)); } public Boolean getBoolean(String key) throws Exception{ return Boolean.parseBoolean(getString(key)); } public static void main(String[] args) throws Exception{ System.out.println(loader.getString("vertx.VXWEB.cluster.enabled")); } }
OptionsReader.java(获取集群和部署配置类属性)
package com.lenovo.push.deploy; import io.vertx.core.DeploymentOptions; import io.vertx.core.VertxOptions; import io.vertx.core.json.JsonObject; import com.lenovo.push.loader.ResourceLoader; public class OptionsReader { private static final String VX_PREFIX = "vertx."; private static ResourceLoader LOADER = ResourceLoader.getInstance(); public static VertxOptions readOpts(final String name) throws Exception{ final VertxOptions options = new VertxOptions(); options.setEventLoopPoolSize(LOADER.getInt(VX_PREFIX+name+".pool.size.event.loop")); options.setWorkerPoolSize(LOADER.getInt(VX_PREFIX+name+".pool.size.worker")); options.setInternalBlockingPoolSize(LOADER.getInt(VX_PREFIX+name+".pool.size.internal.blocking")); options.setClustered(LOADER.getBoolean(VX_PREFIX+name+".cluster.enabled")); options.setClusterHost(LOADER.getString(VX_PREFIX+name+".cluster.host")); options.setClusterPort(LOADER.getInt(VX_PREFIX+name+".cluster.port")); options.setClusterPingInterval(LOADER.getLong(VX_PREFIX+name+".cluster.ping.interval")); options.setClusterPingReplyInterval(LOADER.getLong(VX_PREFIX+name+".cluster.ping.interval.reply")); options.setBlockedThreadCheckInterval(LOADER.getLong(VX_PREFIX+name+".blocked.thread.check.interval")); options.setMaxEventLoopExecuteTime(LOADER.getLong(VX_PREFIX+name+".execute.time.max.event.loop")); options.setMaxWorkerExecuteTime(LOADER.getLong(VX_PREFIX+name+".execute.time.max.worker")); options.setHAEnabled(LOADER.getBoolean(VX_PREFIX+name+".ha.enabled")); options.setHAGroup(LOADER.getString(VX_PREFIX+name+".ha.group")); options.setQuorumSize(LOADER.getInt(VX_PREFIX+name+".quorum.size")); options.setWarningExceptionTime(LOADER.getLong(VX_PREFIX+name+".warning.exception.time")); return options; } public static DeploymentOptions readOpts() throws Exception{ final DeploymentOptions options = new DeploymentOptions(); options.setConfig(new JsonObject() .put("http.port", LOADER.getInt("http.port")) .put("db_name", LOADER.getString("db_name")) .put("connection_string", LOADER.getString("connection_string"))); return options; } }
RootVerticle.java(设置监听,请求路由,接收请求然后发送消息到PollVerticle)
package com.lenovo.push.verticle; import io.vertx.core.AbstractVerticle; import io.vertx.core.Future; import io.vertx.core.eventbus.EventBus; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerResponse; import io.vertx.core.json.JsonObject; import io.vertx.ext.web.Router; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class RootVerticle extends AbstractVerticle { public static final Logger log = LogManager.getLogger(RootVerticle.class); public void start(Future<Void> future){ final HttpServer server = vertx.createHttpServer(); Router router = Router.router(vertx); router.route("/").handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.putHeader("content-type", "text/html"); response.end("<h2>Hello from vertx cluster.</h2>"); }); //接收上报消息 router.routeWithRegex("/pushservice/*.*/poll*").handler(routingContext -> { HttpServerResponse response = routingContext.response(); boolean hastype = routingContext.request().params().contains("type"); if(hastype){ //使用eventbus发送消息 EventBus eb = vertx.eventBus(); eb.publish("pushservice-poll", "datareport"); }else{ // } response.putHeader("content-type", "application/json"); response.end(new JsonObject().put("code", 200).put("status", "ok").toString()); }); router.routeWithRegex("/pushservice/*.*/appfeedback*").handler(routingContext -> { HttpServerResponse response = routingContext.response(); String lpsst = routingContext.request().getParam("lpsst"); System.out.println("lpsst get: " + lpsst); String body = routingContext.getBodyAsString(); System.out.println(body); response.putHeader("content-type", "text/html"); response.end("<h2>feedback ok!</h2>"); }); router.routeWithRegex("/pushservice/*.*/appinfo*").handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.putHeader("content-type", "text/html"); response.end("<h2>appinfo ok!</h2>"); }); server.requestHandler(router::accept); server.listen(config().getInteger("http.port", 8080),result -> { if(result.succeeded()){ future.complete(); }else{ future.fail(result.cause()); } }); log.info("httpserver listenning on port : "+config().getInteger("http.port",8080)); } }
PollVerticle.java(接收消息然后向mongo中写入一条记录)
package com.lenovo.push.verticle; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import io.vertx.core.AbstractVerticle; import io.vertx.core.eventbus.EventBus; import io.vertx.core.json.JsonObject; import io.vertx.ext.mongo.MongoClient; public class PollVerticle extends AbstractVerticle { public static final Logger log = LogManager.getLogger(PollVerticle.class); public static int count = 1; @Override public void start() throws Exception { //final MongoClient mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "lenovodb").put("connection_string", "mongodb://192.168.56.201:27017")); final MongoClient mongo = MongoClient.createShared(vertx, config()); EventBus eb = vertx.eventBus(); eb.consumer("pushservice-poll",message ->{ String msg = (String)message.body(); log.info("received message: "+msg); JsonObject user = new JsonObject().put("id", count) .put("name", "vertx"+(count++)) .put("age", "333") .put("mobile", "no"); //插入用户信息 mongo.insert("users", user,lookup->{ if(lookup.failed()){ log.error("insert user error: "+lookup.cause()); return; } }); }); } }
ClusterStarter.java(部署Verticle,启动集群类
package com.lenovo.push.start; import io.vertx.core.DeploymentOptions; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import java.util.function.Consumer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.lenovo.push.deploy.OptionsReader; public class ClusterStarter { private static final Logger log = LogManager .getLogger(ClusterStarter.class); public static void main(String[] args) throws Exception { /* * final VertxOptions options = OptionsReader.readOpts("VXWEB"); final * VertxFactory factory = new VertxFactoryImpl(); final ClusterManager * manager = new HazelcastClusterManager(new Config()); * options.setClusterManager(manager); factory.clusteredVertx(options, * res -> { if(res.succeeded()){ final Vertx vertx = res.result(); final * DeploymentOptions opts = OptionsReader.readOpts(); * vertx.deployVerticle("com.lenovo.verticle.RouterVerticle", opts); * vertx.deployVerticle("com.lenovo.verticle.PollVerticle", opts); * log.info("Deploye RouterVerticle with cluster ok!"); } }); */ final VertxOptions options = new VertxOptions(); options.setClustered(true); final DeploymentOptions opts = OptionsReader.readOpts(); Consumer<Vertx> runner = vertx -> { vertx.deployVerticle("com.lenovo.push.verticle.RootVerticle",opts); vertx.deployVerticle("com.lenovo.push.verticle.PollVerticle",opts); }; if (options.isClustered()) { Vertx.clusteredVertx( options, result -> { if (result.succeeded()) { Vertx vertx = result.result(); runner.accept(vertx); log.info("pushservice is running with cluster by hazelcast."); } else { log.error("cluster running with error: " + result.cause().getMessage()); } }); } else { Vertx vertx = Vertx.vertx(options); runner.accept(vertx); } } }
第三步、打包部署并检验集群。
本实例通过Assembly插件打包。打包之后生成如下的文件。
我自己的两台虚拟机都只有一个网卡,而且都在同一网段,所以可以使用默认的广播方式构建集群,因此也不需要
配置文件cluster.xml。
启动之后查看日志,机器IP分别是:192.168.56.201和192.168.56.202
192.168.56.201上的启动日志,注意标识红色框的位置区别:
192.168.56.202上的启动日志:
集群确实构建成功,现在可以测试工程的功能,发送一个请求http://192.168.56.201:8082/pushservice/2.0/poll?
type=2,查看返回结果。