Skip to content

Commit

Permalink
Expose Stork Registrars in Quarkus extension + integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Jun 28, 2023
1 parent fd51d93 commit 03ba2b6
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<smallrye-reactive-types-converter.version>3.0.0</smallrye-reactive-types-converter.version>
<smallrye-mutiny-vertx-binding.version>3.5.0</smallrye-mutiny-vertx-binding.version>
<smallrye-reactive-messaging.version>4.6.0</smallrye-reactive-messaging.version>
<smallrye-stork.version>2.2.0</smallrye-stork.version>
<smallrye-stork.version>2.3.0</smallrye-stork.version>
<jakarta.activation.version>2.1.2</jakarta.activation.version>
<jakarta.annotation-api.version>2.1.1</jakarta.annotation-api.version>
<jakarta.enterprise.cdi-api.version>4.0.1</jakarta.enterprise.cdi-api.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ public class ServiceConfiguration {
*/
@ConfigItem
public StorkLoadBalancerConfiguration loadBalancer;

/**
* ServiceRegistrar configuration for the service
*/
@ConfigItem
public StorkServiceRegistrarConfiguration serviceRegistrar;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static List<ServiceConfig> toStorkServiceConfig(StorkConfiguration storkC
SimpleServiceConfig.SimpleLoadBalancerConfig loadBalancerConfig = new SimpleServiceConfig.SimpleLoadBalancerConfig(
serviceConfiguration.loadBalancer.type, serviceConfiguration.loadBalancer.parameters);
builder.setLoadBalancer(loadBalancerConfig);
SimpleServiceConfig.SimpleServiceRegistrarConfig serviceRegistrarConfig = new SimpleServiceConfig.SimpleServiceRegistrarConfig(
serviceConfiguration.serviceRegistrar.type, serviceConfiguration.serviceRegistrar.parameters);
builder.setServiceRegistrar(serviceRegistrarConfig);
storkServicesConfigs.add(builder.build());
}
return storkServicesConfigs;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.stork;

import java.util.Map;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;

@ConfigGroup
public class StorkServiceRegistrarConfiguration {

/**
* Configures service registrar type, e.g. "consul".
* A ServiceRegistrarProvider for the type has to be available
*
*/
@ConfigItem
public String type;

/**
* Service Registrar parameters.
* Check the documentation of the selected registrar type for available parameters
*
*/
@ConfigItem(name = ConfigItem.PARENT)
public Map<String, String> parameters;

}
2 changes: 1 addition & 1 deletion independent-projects/resteasy-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<rest-assured.version>5.3.0</rest-assured.version>
<commons-logging-jboss-logging.version>1.0.0.Final</commons-logging-jboss-logging.version>
<jackson-bom.version>2.15.2</jackson-bom.version>
<smallrye-stork.version>2.1.0</smallrye-stork.version>
<smallrye-stork.version>2.3.0</smallrye-stork.version>
<jakarta.validation-api.version>3.0.2</jakarta.validation-api.version>
<yasson.version>3.0.3</yasson.version>
<jakarta.json.bind-api.version>3.0.0</jakarta.json.bind-api.version>
Expand Down
1 change: 1 addition & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
<module>smallrye-graphql</module>
<module>smallrye-graphql-client</module>
<module>smallrye-opentracing</module>
<module>smallrye-stork-registration</module>
<module>jpa-without-entity</module>
<module>quartz</module>
<module>redis-client</module>
Expand Down
141 changes: 141 additions & 0 deletions integration-tests/smallrye-stork-registration/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-integration-tests-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>quarkus-integration-test-smallrye-stork-registration</artifactId>
<name>Quarkus - Integration Tests - Smallrye Stork Registration</name>

<dependencies>
<!-- Client dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.stork</groupId>
<artifactId>stork-service-discovery-static-list</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>

<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
<!-- add some custom config, the rest comes from parent -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.acme;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RestClient;

/**
* A frontend API using our REST Client (which uses Stork to locate and select the service instance on each call).
*/
@Path("/api")
public class ClientCallingResource {

@RestClient
MyServiceClient service;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String invoke() {
return service.get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.acme;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

/**
* The REST Client interface.
*
* Notice the `baseUri`. It uses `stork://` as URL scheme indicating that the called service uses Stork to locate and
* select the service instance. The `my-service` part is the service name. This is used to configure Stork discovery
* and selection in the `application.properties` file.
*/
@RegisterRestClient(baseUri = "stork://my-service")
public interface MyServiceClient {

@GET
@Produces(MediaType.TEXT_PLAIN)
String get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.acme.services;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.quarkus.runtime.StartupEvent;
import io.vertx.mutiny.core.Vertx;

@ApplicationScoped
public class BlueService {

@ConfigProperty(name = "blue-service-port", defaultValue = "9000")
int port;

/**
* Start an HTTP server for the blue service.
*
* Note: this method is called on a worker thread, and so it is allowed to block.
*/
public void init(@Observes StartupEvent ev, Vertx vertx) {
vertx.createHttpServer()
.requestHandler(req -> req.response().endAndForget("Hello from Blue!"))
.listenAndAwait(port);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

package org.acme.services;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.quarkus.runtime.StartupEvent;
import io.vertx.mutiny.core.Vertx;

@ApplicationScoped
public class RedService {
@ConfigProperty(name = "red-service-port", defaultValue = "9001")
int port;

/**
* Start an HTTP server for the red service.
*
* Note: this method is called on a worker thread, and so it is allowed to block.
*/
public void init(@Observes StartupEvent ev, Vertx vertx) {
vertx.createHttpServer()
.requestHandler(req -> req.response().endAndForget("Hello from Red!"))
.listenAndAwait(port);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.acme.services;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.quarkus.runtime.StartupEvent;
import io.smallrye.stork.Stork;
import io.vertx.mutiny.core.Vertx;

@ApplicationScoped
public class Registration {

@ConfigProperty(name = "red-service-port", defaultValue = "9000")
int red;
@ConfigProperty(name = "blue-service-port", defaultValue = "9001")
int blue;

/**
* Register our two services using static list.
*
* Note: this method is called on a worker thread, and so it is allowed to block.
*/
public void init(@Observes StartupEvent ev, Vertx vertx) {
Stork.getInstance().getService("my-service").getServiceRegistrar().registerServiceInstance("my-service", "localhost",
red);
Stork.getInstance().getService("my-service").getServiceRegistrar().registerServiceInstance("my-service", "localhost",
blue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quarkus.stork.my-service.service-discovery.type=static

quarkus.stork.my-service.service-registrar.type=static
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.acme;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class ClientCallingResourceIT extends ClientCallingResourceTest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.acme;

import java.util.HashSet;
import java.util.Set;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;

@QuarkusTest
public class ClientCallingResourceTest {

@Test
public void test() {
Set<String> bodies = new HashSet<>();
for (int i = 0; i < 10; i++) {
bodies.add(RestAssured.get("/api").then().statusCode(200).extract().body().asString());
}
Assertions.assertEquals(2, bodies.size());
}

}

0 comments on commit 03ba2b6

Please sign in to comment.