From c61cbf8242820522d4bbb2c0f13453787d9c2fff Mon Sep 17 00:00:00 2001 From: marutsingh Date: Mon, 12 Jun 2017 17:28:23 +0530 Subject: [PATCH] Used Zookeeper for cluster discovery rather than Hazelcast --- pom.xml | 219 ++++++++++-------- .../examples/spring/SpringExampleRunner.java | 33 ++- .../spring/verticle/KotlinVerticle.kt | 75 ++++++ .../spring/verticle/ServerVerticle.java | 1 + .../spring/verticle/SpringDemoVerticle.java | 21 +- src/main/resources/application.properties | 1 + 6 files changed, 241 insertions(+), 109 deletions(-) create mode 100644 src/main/java/io/vertx/examples/spring/verticle/KotlinVerticle.kt diff --git a/pom.xml b/pom.xml index 4e439c6..bc00bc2 100755 --- a/pom.xml +++ b/pom.xml @@ -1,108 +1,135 @@ - 4.0.0 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + 4.0.0 - io.vertx - vertx-cluster - 3.2.1 - spring-example + io.vertx + vertx-cluster-example + 3.2.1 + spring-example - - - io.vertx.examples.spring.SpringExampleRunner - 1.8.0.RELEASE - 1.4.187 - 1.7.12 - 4.3.10.Final - + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + - - - io.vertx - vertx-core - ${project.version} - - - io.vertx - vertx-core - ${project.version} - - - io.vertx - vertx-hazelcast - 3.2.1 - + + + 1.1.2-4 + 1.3.2.RELEASE + io.vertx.examples.spring.SpringExampleRunner + 1.8.0.RELEASE + 1.4.187 + 1.7.12 + 4.3.10.Final + 3.4.1 + - - com.h2database - h2 - ${h2.version} - - - org.slf4j - slf4j-api - ${slf4j.version} - + + + + io.vertx + vertx-core + ${vertx.version} + + + + io.vertx + vertx-zookeeper + ${vertx.version} + + + org.springframework.boot + spring-boot-starter + + + io.vertx + vertx-lang-kotlin + ${vertx.version} + + + com.h2database + h2 + ${h2.version} + - - org.hibernate - hibernate-entitymanager - ${hibernate.version} - + + org.slf4j + slf4j-api + ${slf4j.version} + - + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + - - - - - - maven-compiler-plugin - 3.1 - - 1.8 - 1.8 - - - - + - - - - org.apache.maven.plugins - maven-shade-plugin - 2.3 - - - package - - shade - - - - - - ${main.class} - - - - META-INF/services/io.vertx.core.spi.VerticleFactory - - - - - ${project.build.directory}/${project.artifactId}-${project.version}-fat.jar - - - - - - + + + + + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + process-sources + + compile + + + + src/main/resources + src/main/java + + + + + test-compile + process-test-sources + + test-compile + + + + + + + + + + diff --git a/src/main/java/io/vertx/examples/spring/SpringExampleRunner.java b/src/main/java/io/vertx/examples/spring/SpringExampleRunner.java index a02d67a..13444b2 100755 --- a/src/main/java/io/vertx/examples/spring/SpringExampleRunner.java +++ b/src/main/java/io/vertx/examples/spring/SpringExampleRunner.java @@ -1,32 +1,49 @@ package io.vertx.examples.spring; -import com.hazelcast.config.Config; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; +import io.vertx.core.json.JsonObject; import io.vertx.core.spi.cluster.ClusterManager; import io.vertx.examples.spring.verticle.ServerVerticle; import io.vertx.examples.spring.verticle.SpringDemoVerticle; -import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager; +import io.vertx.examples.spring.verticle.SpringDemoVerticle1; +import io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; /** * Runner for the vertx-spring sample * */ +@SpringBootApplication public class SpringExampleRunner { + @Value("${server.port}") + String someValue; + public static void main( String[] args ) { - System.out.println("adfadf"); - Config hazelcastConfig = new Config(); - hazelcastConfig.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1").setEnabled(true); - hazelcastConfig.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); + //Config hazelcastConfig = new Config(); + //hazelcastConfig.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1").setEnabled(true); + //hazelcastConfig.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); + + ConfigurableApplicationContext ctx = SpringApplication.run(SpringExampleRunner.class, args); + + JsonObject zkConfig = new JsonObject(); + zkConfig.put("zookeeperHosts", "127.0.0.1"); + zkConfig.put("rootPath", "io.vertx"); + zkConfig.put("retry", new JsonObject() + .put("initialSleepTime", 3000) + .put("maxTimes", 3)); - ClusterManager mgr = new HazelcastClusterManager(hazelcastConfig); + ZookeeperClusterManager mgr = new ZookeeperClusterManager(); VertxOptions options = new VertxOptions().setClusterManager(mgr); Vertx.clusteredVertx(options, res -> { if (res.succeeded()) { Vertx vertx = res.result(); vertx.deployVerticle(new SpringDemoVerticle()); - vertx.deployVerticle(new ServerVerticle(Integer.parseInt(args[0]))); + vertx.deployVerticle(new ServerVerticle(Integer.parseInt(ctx.getEnvironment().getProperty("server.port")))); } else { } }); diff --git a/src/main/java/io/vertx/examples/spring/verticle/KotlinVerticle.kt b/src/main/java/io/vertx/examples/spring/verticle/KotlinVerticle.kt new file mode 100644 index 0000000..ecc48c2 --- /dev/null +++ b/src/main/java/io/vertx/examples/spring/verticle/KotlinVerticle.kt @@ -0,0 +1,75 @@ +package io.vertx.examples.spring.verticle + + +/** + * Created by marutsingh on 5/27/17. + */ + +import com.fasterxml.jackson.core.JsonProcessingException +import com.fasterxml.jackson.databind.ObjectMapper +import io.vertx.core.AbstractVerticle +import io.vertx.core.Handler +import io.vertx.core.eventbus.Message +import io.vertx.examples.spring.service.ProductService + + +/** + * Simple verticle to wrap a Spring service bean - note we wrap the service call + * in executeBlocking, because we know it's going to be a JDBC call which blocks. + * As a general principle with Spring beans, the default assumption should be that it will block unless you + * know for sure otherwise (in other words use executeBlocking unless you know for sure your service call will be + * extremely quick to respond) + */ +class SpringDemoVerticle1 : AbstractVerticle() { + + private val mapper = ObjectMapper() + + /*fun allProductsHandler(service: ProductService): Handler> { + + + System.out.println("Hello World!") + return { msg -> + System.out.println("1 received message.body() = " + + message.body()); + } + // throw new Exception(""); + + *//*return msg -> vertx.executeBlocking(future -> { + throw new Exception(""); + try { + future.complete(mapper.writeValueAsString(service.getAllProducts())); + } catch (JsonProcessingException e) { + System.out.println("Failed to serialize result"); + future.fail(e); + } + }, + result -> { + if (result.succeeded()) { + msg.reply(result.result()); + } else { + msg.reply(result.cause().toString()); + } + });*//* + } +*/ + override fun start() { + super.start() + val productService = ProductService() + + vertx.eventBus().consumer(ALL_PRODUCTS_ADDRESS, { message: Message -> + System.out.println("1 received message.body() = " + + message.body()); + }) + + //Start consuming events + /*vertx.eventBus().consumer(ALL_PRODUCTS_ADDRESS, { + allProductsHandler(productService) + })*/ + + } + + companion object { + + public val ALL_PRODUCTS_ADDRESS: String = "example.all.products" + } +} diff --git a/src/main/java/io/vertx/examples/spring/verticle/ServerVerticle.java b/src/main/java/io/vertx/examples/spring/verticle/ServerVerticle.java index 60b249b..b001bf2 100755 --- a/src/main/java/io/vertx/examples/spring/verticle/ServerVerticle.java +++ b/src/main/java/io/vertx/examples/spring/verticle/ServerVerticle.java @@ -32,6 +32,7 @@ public void start() throws Exception { } }); } else { + System.out.println("Printing"); req.response().setStatusCode(200).write("Hello from vert.x").end(); } diff --git a/src/main/java/io/vertx/examples/spring/verticle/SpringDemoVerticle.java b/src/main/java/io/vertx/examples/spring/verticle/SpringDemoVerticle.java index dbed73e..260329d 100755 --- a/src/main/java/io/vertx/examples/spring/verticle/SpringDemoVerticle.java +++ b/src/main/java/io/vertx/examples/spring/verticle/SpringDemoVerticle.java @@ -25,8 +25,12 @@ public SpringDemoVerticle() { } - private Handler> allProductsHandler(ProductService service) { - return msg -> vertx.executeBlocking(future -> { + /*private Handler> allProductsHandler(ProductService service) { + + // throw new Exception(""); + + *//*return msg -> vertx.executeBlocking(future -> { + throw new Exception(""); try { future.complete(mapper.writeValueAsString(service.getAllProducts())); } catch (JsonProcessingException e) { @@ -40,14 +44,21 @@ private Handler> allProductsHandler(ProductService service) { } else { msg.reply(result.cause().toString()); } - }); + });*//* } - +*/ @Override public void start() throws Exception { super.start(); ProductService productService = new ProductService(); + + /* vertx.eventBus().consumer("anAddress", message -> { + System.out.println("1 received message.body() = " + + message.body()); + });*/ //Start consuming events - vertx.eventBus().consumer(ALL_PRODUCTS_ADDRESS).handler(allProductsHandler(productService)); + vertx.eventBus().consumer(ALL_PRODUCTS_ADDRESS).handler(msg -> { + System.out.println(msg); + }); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 75f2300..837cfce 100755 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,3 +9,4 @@ jdbc.password= hibernate.dialect=org.hibernate.dialect.H2Dialect hibernate.show_sql=true hibernate.hbm2ddl.auto=validate +server.port=9000