Skip to content

Commit

Permalink
Used Zookeeper for cluster discovery rather than Hazelcast
Browse files Browse the repository at this point in the history
  • Loading branch information
marutdelhivery committed Jun 12, 2017
1 parent f58fab0 commit c61cbf8
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 109 deletions.
219 changes: 123 additions & 96 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,108 +1,135 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<groupId>io.vertx</groupId>
<artifactId>vertx-cluster</artifactId>
<version>3.2.1</version>
<name>spring-example</name>
<groupId>io.vertx</groupId>
<artifactId>vertx-cluster-example</artifactId>
<version>3.2.1</version>
<name>spring-example</name>

<properties>
<!-- the main verticle class name -->
<main.class>io.vertx.examples.spring.SpringExampleRunner</main.class>
<spring.data.jpa.version>1.8.0.RELEASE</spring.data.jpa.version>
<h2.version>1.4.187</h2.version>
<slf4j.version>1.7.12</slf4j.version>
<hibernate.version>4.3.10.Final</hibernate.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>3.2.1</version>
</dependency>
<properties>
<!-- the main verticle class name -->
<kotlin.version>1.1.2-4</kotlin.version>
<boot.version>1.3.2.RELEASE</boot.version>
<main.class>io.vertx.examples.spring.SpringExampleRunner</main.class>
<spring.data.jpa.version>1.8.0.RELEASE</spring.data.jpa.version>
<h2.version>1.4.187</h2.version>
<slf4j.version>1.7.12</slf4j.version>
<hibernate.version>4.3.10.Final</hibernate.version>
<vertx.version>3.4.1</vertx.version>
</properties>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>-->
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.vertx/vertx-zookeeper -->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-zookeeper</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-kotlin</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

</dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>

<build>
<pluginManagement>
<plugins>
<!-- We specify the Maven compiler plugin as we need to set it to Java 1.8 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</dependencies>

<!--
You only need the part below if you want to build your application into a fat executable jar.
This is a jar that contains all the dependencies required to run it, so you can just run it with
java -jar
-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${main.class}</Main-Class>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
</transformer>
</transformers>
<artifactSet>
</artifactSet>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<build>
<plugins>
<!-- We specify the Maven compiler plugin as we need to set it to Java 1.8 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/resources</source>
<source>src/main/java</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs></sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

</build>
</project>
33 changes: 25 additions & 8 deletions src/main/java/io/vertx/examples/spring/SpringExampleRunner.java
Original file line number Diff line number Diff line change
@@ -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 {
}
});
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/io/vertx/examples/spring/verticle/KotlinVerticle.kt
Original file line number Diff line number Diff line change
@@ -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<Message<String>> {
System.out.println("Hello World!")
return { msg ->
System.out.println("1 received message.body() = "
+ message.body());
}
// throw new Exception("");
*//*return msg -> vertx.<String>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<String> ->
System.out.println("1 received message.body() = "
+ message.body());
})

//Start consuming events
/*vertx.eventBus().consumer<String>(ALL_PRODUCTS_ADDRESS, {
allProductsHandler(productService)
})*/

}

companion object {

public val ALL_PRODUCTS_ADDRESS: String = "example.all.products"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void start() throws Exception {
}
});
} else {
System.out.println("Printing");
req.response().setStatusCode(200).write("Hello from vert.x").end();
}

Expand Down
Loading

0 comments on commit c61cbf8

Please sign in to comment.