Skip to content

Commit 2441c4d

Browse files
authored
Merge pull request vert-x3#168 from tsegismont/springboot-clustering
Adds a new Spring Boot clustering examples folder.
2 parents 0267f43 + 83a38ec commit 2441c4d

File tree

10 files changed

+565
-1
lines changed

10 files changed

+565
-1
lines changed

spring-examples/pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<modules>
1414
<module>springboot-example</module>
1515
<module>spring-example</module>
16+
<module>springboot-clustering</module>
1617
</modules>
1718

18-
</project>
19+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
= Spring Boot Clustering Examples
2+
3+
These projects show how you can setup a clustered Vert.x embedded in Spring Boot, having the Spring container managing the clustering engine:
4+
5+
* link:springboot-clustering-hazelcast/README.adoc[Spring Boot Hazelcast Clustering]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2017 Red Hat, Inc.
4+
~
5+
~ Red Hat licenses this file to you under the Apache License, version 2.0
6+
~ (the "License"); you may not use this file except in compliance with the
7+
~ License. You may obtain a copy of the License at:
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
~ License for the specific language governing permissions and limitations
15+
~ under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>io.vertx</groupId>
25+
<artifactId>spring-examples</artifactId>
26+
<version>3.3.3</version>
27+
</parent>
28+
29+
<artifactId>springboot-clustering</artifactId>
30+
<packaging>pom</packaging>
31+
32+
<modules>
33+
<module>springboot-clustering-hazelcast</module>
34+
</modules>
35+
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
= Spring Boot Hazelcast Clustering
2+
3+
This project shows how you can setup a clustered Vert.x embedded in Spring Boot, having the Spring container managing Hazelcast.
4+
5+
It consists of two verticles.
6+
The first one starts an HTTP server, expecting a `name` query parameter, and sending it on the event bus.
7+
The second one listens to names sent on the event bus, and replies with a formatted message.
8+
Eventually the message goes to the client in the HTTP response body.
9+
10+
== The code
11+
12+
In the `io.vertx.examples.spring.clustering.hazelcast.VertxProducer` class, a clustered Vert.x instance is created.
13+
It uses a Spring-managed `com.hazelcast.core.HazelcastInstance`.
14+
15+
There are multiple ways to configure it, but a simple one is to put a `hazelcast.xml` file at the root of the classpath.
16+
17+
The Hazelcast instance will be created *before* and destroyed *after* Vert.x.
18+
19+
IMPORTANT: the Hazelcast shutdown hook must be disabled.
20+
21+
To disable it, simply add this to the `hazelcast.xml` file:
22+
23+
[source,xml]
24+
----
25+
<properties>
26+
<!-- ... other properties ... -->
27+
<property name="hazelcast.shutdownhook.enabled">false</property>
28+
</properties>
29+
----
30+
31+
== Trying
32+
33+
To see clustering in action, you should start multiple instances of this application.
34+
The HTTP server will be setup only if you provide an `httpPort` system property.
35+
36+
So you can start the first instance with an HTTP server on port 8081:
37+
38+
[source,shell]
39+
----
40+
mvn spring-boot:run -DhttpPort=8081
41+
----
42+
43+
And then start the other instances without the HTTP server:
44+
45+
[source,shell]
46+
----
47+
mvn spring-boot:run
48+
----
49+
50+
It's time for testing now:
51+
52+
[source,shell]
53+
----
54+
seq 10 | while read i; do curl http://localhost:8081/?name=Thomas${i}; echo; done
55+
----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2017 Red Hat, Inc.
4+
~
5+
~ Red Hat licenses this file to you under the Apache License, version 2.0
6+
~ (the "License"); you may not use this file except in compliance with the
7+
~ License. You may obtain a copy of the License at:
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
~ License for the specific language governing permissions and limitations
15+
~ under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>io.vertx</groupId>
25+
<artifactId>springboot-clustering</artifactId>
26+
<version>3.3.3</version>
27+
</parent>
28+
29+
<artifactId>springboot-clustering-hazelcast</artifactId>
30+
31+
<properties>
32+
<boot.version>1.4.3.RELEASE</boot.version>
33+
<vertx.version>${project.version}</vertx.version>
34+
</properties>
35+
36+
<dependencyManagement>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-dependencies</artifactId>
41+
<version>${boot.version}</version>
42+
<type>pom</type>
43+
<scope>import</scope>
44+
</dependency>
45+
</dependencies>
46+
</dependencyManagement>
47+
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.vertx</groupId>
55+
<artifactId>vertx-hazelcast</artifactId>
56+
<version>${vertx.version}</version>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<artifactId>maven-compiler-plugin</artifactId>
64+
<configuration>
65+
<source>1.8</source>
66+
<target>1.8</target>
67+
</configuration>
68+
</plugin>
69+
<plugin>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-maven-plugin</artifactId>
72+
<version>${boot.version}</version>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
77+
<profiles>
78+
<profile>
79+
<id>staging</id>
80+
<repositories>
81+
<repository>
82+
<id>staging</id>
83+
<url>https://oss.sonatype.org/content/repositories/iovertx-3295</url>
84+
</repository>
85+
</repositories>
86+
</profile>
87+
</profiles>
88+
89+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2017 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.examples.spring.clustering.hazelcast;
18+
19+
import io.vertx.core.DeploymentOptions;
20+
import io.vertx.core.Vertx;
21+
import io.vertx.core.json.JsonObject;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.beans.factory.annotation.Value;
24+
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.boot.context.event.ApplicationReadyEvent;
27+
import org.springframework.context.event.EventListener;
28+
29+
/**
30+
* @author Thomas Segismont
31+
*/
32+
@SpringBootApplication
33+
public class Application {
34+
35+
@Autowired
36+
Vertx vertx;
37+
38+
@Value("${systemProperties.httpPort:#{null}}")
39+
Integer port;
40+
41+
public static void main(String[] args) {
42+
SpringApplication.run(Application.class, args);
43+
}
44+
45+
/**
46+
* Deploy verticles when the Spring application is ready.
47+
*/
48+
@EventListener
49+
void deployVerticles(ApplicationReadyEvent event) {
50+
vertx.deployVerticle(new HelloVerticle());
51+
if (port != null) {
52+
JsonObject config = new JsonObject().put("port", port);
53+
vertx.deployVerticle(new HttpVerticle(), new DeploymentOptions().setConfig(config));
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.examples.spring.clustering.hazelcast;
18+
19+
import io.vertx.core.AbstractVerticle;
20+
import io.vertx.core.Future;
21+
22+
import java.util.UUID;
23+
24+
/**
25+
* @author Thomas Segismont
26+
*/
27+
public class HelloVerticle extends AbstractVerticle {
28+
private static final String ID = UUID.randomUUID().toString();
29+
30+
@Override
31+
public void start(Future<Void> startFuture) throws Exception {
32+
vertx.eventBus().<String>consumer("hello", message -> {
33+
message.reply("Hello " + message.body() + " from " + ID);
34+
}).completionHandler(startFuture.completer());
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2017 Red Hat, Inc.
3+
*
4+
* Red Hat licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package io.vertx.examples.spring.clustering.hazelcast;
18+
19+
import io.vertx.core.AbstractVerticle;
20+
import io.vertx.core.Future;
21+
import io.vertx.core.http.HttpServerOptions;
22+
23+
/**
24+
* @author Thomas Segismont
25+
*/
26+
public class HttpVerticle extends AbstractVerticle {
27+
28+
@Override
29+
public void start(Future<Void> startFuture) throws Exception {
30+
HttpServerOptions options = new HttpServerOptions().setPort(config().getInteger("port"));
31+
vertx.createHttpServer(options).requestHandler(request -> {
32+
String name = request.getParam("name");
33+
if (name == null) {
34+
request.response().setStatusCode(400).end("Missing name");
35+
} else {
36+
vertx.eventBus().<String>send("hello", name, ar -> {
37+
if (ar.succeeded()) {
38+
request.response().end(ar.result().body());
39+
} else {
40+
request.response().setStatusCode(500).end(ar.cause().getMessage());
41+
}
42+
});
43+
}
44+
}).listen(ar -> {
45+
if (ar.succeeded()) {
46+
startFuture.complete();
47+
} else {
48+
startFuture.fail(ar.cause());
49+
}
50+
});
51+
}
52+
}

0 commit comments

Comments
 (0)