Skip to content

Commit 2c6be02

Browse files
committed
Add openshift examples
1 parent d656949 commit 2c6be02

File tree

12 files changed

+709
-0
lines changed

12 files changed

+709
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.vertx.book</groupId>
6+
<artifactId>hello-consumer</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<properties>
10+
<vertx.projectVersion>3.4.0-SNAPSHOT</vertx.projectVersion>
11+
<vertx.verticle>io.vertx.book.openshift.HelloConsumerWithCircuitBreakerHttpVerticle</vertx.verticle>
12+
</properties>
13+
<dependencyManagement>
14+
<dependencies>
15+
<dependency>
16+
<groupId>io.vertx</groupId>
17+
<artifactId>vertx-dependencies</artifactId>
18+
<version>${vertx.projectVersion}</version>
19+
<type>pom</type>
20+
<scope>import</scope>
21+
</dependency>
22+
</dependencies>
23+
</dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>io.vertx</groupId>
27+
<artifactId>vertx-core</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>io.vertx</groupId>
31+
<artifactId>vertx-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>io.vertx</groupId>
35+
<artifactId>vertx-rx-java</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>io.vertx</groupId>
39+
<artifactId>vertx-web-client</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.vertx</groupId>
43+
<artifactId>vertx-service-discovery</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.vertx</groupId>
47+
<artifactId>vertx-service-discovery-bridge-kubernetes</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>io.vertx</groupId>
51+
<artifactId>vertx-circuit-breaker</artifactId>
52+
<version>3.4.0-SNAPSHOT</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>io.vertx</groupId>
56+
<artifactId>vertx-health-checks</artifactId>
57+
<version>3.4.0-SNAPSHOT</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.slf4j</groupId>
61+
<artifactId>slf4j-api</artifactId>
62+
<version>1.7.22</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.slf4j</groupId>
66+
<artifactId>slf4j-jcl</artifactId>
67+
<version>1.7.22</version>
68+
</dependency>
69+
</dependencies>
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>io.fabric8</groupId>
74+
<artifactId>vertx-maven-plugin</artifactId>
75+
<version>1.0.4</version>
76+
<executions>
77+
<execution>
78+
<id>vmp-init-package</id>
79+
<goals>
80+
<goal>initialize</goal>
81+
<goal>package</goal>
82+
</goals>
83+
</execution>
84+
</executions>
85+
<configuration>
86+
<redeploy>true</redeploy>
87+
</configuration>
88+
</plugin>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-compiler-plugin</artifactId>
92+
<configuration>
93+
<source>1.8</source>
94+
<target>1.8</target>
95+
</configuration>
96+
</plugin>
97+
</plugins>
98+
</build>
99+
100+
<profiles>
101+
<profile>
102+
<id>openshift</id>
103+
<build>
104+
<plugins>
105+
<plugin>
106+
<groupId>io.fabric8</groupId>
107+
<artifactId>fabric8-maven-plugin</artifactId>
108+
<version>3.2.27</version>
109+
<executions>
110+
<execution>
111+
<id>fmp</id>
112+
<goals>
113+
<goal>resource</goal>
114+
<goal>helm</goal>
115+
<goal>build</goal>
116+
</goals>
117+
</execution>
118+
</executions>
119+
</plugin>
120+
</plugins>
121+
</build>
122+
</profile>
123+
</profiles>
124+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: Route
3+
metadata:
4+
name: ${project.artifactId}
5+
spec:
6+
to:
7+
kind: Service
8+
name: ${project.artifactId}
9+
weight: 100
10+
port:
11+
targetPort: http
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package io.vertx.book.openshift;
2+
3+
import io.vertx.circuitbreaker.CircuitBreakerOptions;
4+
import io.vertx.core.json.JsonObject;
5+
import io.vertx.rxjava.ext.healthchecks.HealthCheckHandler;
6+
import io.vertx.ext.healthchecks.Status;
7+
import io.vertx.rxjava.circuitbreaker.CircuitBreaker;
8+
import io.vertx.rxjava.core.AbstractVerticle;
9+
import io.vertx.rxjava.ext.web.Router;
10+
import io.vertx.rxjava.ext.web.RoutingContext;
11+
import io.vertx.rxjava.ext.web.client.HttpRequest;
12+
import io.vertx.rxjava.ext.web.client.HttpResponse;
13+
import io.vertx.rxjava.ext.web.client.WebClient;
14+
import io.vertx.rxjava.ext.web.codec.BodyCodec;
15+
import io.vertx.rxjava.servicediscovery.ServiceDiscovery;
16+
import io.vertx.rxjava.servicediscovery.types.HttpEndpoint;
17+
import rx.Single;
18+
19+
public class HelloConsumerWithCircuitBreakerHttpVerticle extends AbstractVerticle {
20+
21+
private WebClient hello;
22+
private CircuitBreaker circuit;
23+
24+
private boolean ready;
25+
26+
@Override
27+
public void start() {
28+
29+
circuit = CircuitBreaker.create("my-circuit", vertx,
30+
new CircuitBreakerOptions()
31+
.setFallbackOnFailure(true) // Call the fallback on failures
32+
.setTimeout(2000) // Set the operation timeout
33+
.setMaxFailures(3) // Number of failures before switching to the 'open' state
34+
.setResetTimeout(5000));
35+
36+
37+
Router router = Router.router(vertx);
38+
router.get("/").handler(this::invokeHelloMicroservice);
39+
router.get("/health").handler(HealthCheckHandler.create(vertx).register("http-server-running",
40+
future -> future.complete(ready ? Status.OK() : Status.KO())));
41+
42+
// Create the service discovery instance
43+
ServiceDiscovery.create(vertx, discovery -> {
44+
// Look for a HTTP endpoint named "hello-microservice"
45+
Single<WebClient> single = HttpEndpoint.rxGetWebClient(discovery,
46+
rec -> rec.getName().equalsIgnoreCase("hello-microservice"));
47+
48+
single.subscribe(
49+
client -> {
50+
// the configured hello to call our microservice
51+
this.hello = client;
52+
// start the HTTP server
53+
vertx.createHttpServer()
54+
.requestHandler(router::accept)
55+
.listen(8080, ar -> ready = ar.succeeded());
56+
},
57+
err -> System.out.println("Oh no, no service")
58+
);
59+
});
60+
61+
}
62+
63+
64+
private void invokeHelloMicroservice(RoutingContext rc) {
65+
circuit.rxExecuteCommandWithFallback(
66+
future -> {
67+
HttpRequest<JsonObject> request1 = hello.get("/Luke").as(BodyCodec.jsonObject());
68+
69+
HttpRequest<JsonObject> request2 = hello.get("/Leia").as(BodyCodec.jsonObject());
70+
71+
Single<HttpResponse<JsonObject>> s1 = request1.rxSend();
72+
Single<HttpResponse<JsonObject>> s2 = request2.rxSend();
73+
74+
Single
75+
.zip(s1, s2, (luke, leia) -> {
76+
// We have the result of both request in Luke and Leia
77+
return new JsonObject()
78+
.put("luke", luke.body().getString("message")
79+
+ " " + luke.body().getString("served-by"))
80+
.put("leia", leia.body().getString("message")
81+
+ " " + leia.body().getString("served-by"));
82+
})
83+
.subscribe(future::complete, future::fail);
84+
},
85+
error -> new JsonObject().put("message", "hello (fallback, " + circuit.state().toString() + ")")
86+
).subscribe(
87+
x -> rc.response().end(x.encodePrettily()),
88+
t -> rc.response().end(t.getMessage()));
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.vertx.book</groupId>
6+
<artifactId>hello-consumer</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<properties>
10+
<vertx.projectVersion>3.4.0-SNAPSHOT</vertx.projectVersion>
11+
<vertx.verticle>io.vertx.book.openshift.HelloConsumerHttpVerticle</vertx.verticle>
12+
</properties>
13+
<dependencyManagement>
14+
<dependencies>
15+
<dependency>
16+
<groupId>io.vertx</groupId>
17+
<artifactId>vertx-dependencies</artifactId>
18+
<version>${vertx.projectVersion}</version>
19+
<type>pom</type>
20+
<scope>import</scope>
21+
</dependency>
22+
</dependencies>
23+
</dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>io.vertx</groupId>
27+
<artifactId>vertx-core</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>io.vertx</groupId>
31+
<artifactId>vertx-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>io.vertx</groupId>
35+
<artifactId>vertx-rx-java</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>io.vertx</groupId>
39+
<artifactId>vertx-web-client</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.vertx</groupId>
43+
<artifactId>vertx-service-discovery</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.vertx</groupId>
47+
<artifactId>vertx-service-discovery-bridge-kubernetes</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>io.vertx</groupId>
51+
<artifactId>vertx-circuit-breaker</artifactId>
52+
<version>3.4.0-SNAPSHOT</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.slf4j</groupId>
56+
<artifactId>slf4j-api</artifactId>
57+
<version>1.7.22</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.slf4j</groupId>
61+
<artifactId>slf4j-jcl</artifactId>
62+
<version>1.7.22</version>
63+
</dependency>
64+
</dependencies>
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>io.fabric8</groupId>
69+
<artifactId>vertx-maven-plugin</artifactId>
70+
<version>1.0.4</version>
71+
<executions>
72+
<execution>
73+
<id>vmp-init-package</id>
74+
<goals>
75+
<goal>initialize</goal>
76+
<goal>package</goal>
77+
</goals>
78+
</execution>
79+
</executions>
80+
<configuration>
81+
<redeploy>true</redeploy>
82+
</configuration>
83+
</plugin>
84+
<plugin>
85+
<groupId>org.apache.maven.plugins</groupId>
86+
<artifactId>maven-compiler-plugin</artifactId>
87+
<configuration>
88+
<source>1.8</source>
89+
<target>1.8</target>
90+
</configuration>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
95+
<profiles>
96+
<profile>
97+
<id>openshift</id>
98+
<build>
99+
<plugins>
100+
<plugin>
101+
<groupId>io.fabric8</groupId>
102+
<artifactId>fabric8-maven-plugin</artifactId>
103+
<version>3.2.27</version>
104+
<executions>
105+
<execution>
106+
<id>fmp</id>
107+
<goals>
108+
<goal>resource</goal>
109+
<goal>helm</goal>
110+
<goal>build</goal>
111+
</goals>
112+
</execution>
113+
</executions>
114+
</plugin>
115+
</plugins>
116+
</build>
117+
</profile>
118+
</profiles>
119+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: Route
3+
metadata:
4+
name: ${project.artifactId}
5+
spec:
6+
to:
7+
kind: Service
8+
name: ${project.artifactId}
9+
weight: 100
10+
port:
11+
targetPort: http

0 commit comments

Comments
 (0)