Basic integration of consul registration

This commit is contained in:
2026-02-15 02:09:08 +05:00
parent e154e232df
commit 2dab1a4016
19 changed files with 195 additions and 73 deletions

45
pom.xml
View File

@@ -3,21 +3,23 @@
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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>code-with-quarkus</artifactId>
<version>1.0.0-SNAPSHOT</version>
<groupId>com.backend.metadata.kodik.service</groupId>
<artifactId>anyame-kodik-metadata-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>quarkus</packaging>
<properties>
<compiler-plugin.version>3.14.1</compiler-plugin.version>
<maven.compiler.release>21</maven.compiler.release>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.30.8</quarkus.platform.version>
<quarkus.platform.version>3.31.1</quarkus.platform.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.5.4</surefire-plugin.version>
<easy-retrofit.version>1.2.0</easy-retrofit.version>
<retrofit.version>2.11.0</retrofit.version>
</properties>
@@ -43,22 +45,30 @@
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.retrofit</groupId>
<artifactId>quarkus-easy-retrofit</artifactId>
<version>1.2.0</version>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson</artifactId>
<artifactId>quarkus-smallrye-stork</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.stork</groupId>
<artifactId>stork-service-registration-consul</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>${retrofit.version}</version>
</dependency>
<dependency>
<groupId>io.quarkiverse.retrofit</groupId>
<artifactId>quarkus-easy-retrofit</artifactId>
<version>${easy-retrofit.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<artifactId>quarkus-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@@ -75,16 +85,6 @@
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
<goal>native-image-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
@@ -97,7 +97,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
<argLine>@{argLine}</argLine>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
@@ -116,7 +116,7 @@
</execution>
</executions>
<configuration>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
<argLine>@{argLine}</argLine>
<systemPropertyVariables>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner</native.image.path>
@@ -144,3 +144,4 @@
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,23 @@
services:
kodik-metadata-service:
build:
context: ../../../.
dockerfile: src/main/docker/Dockerfile.jvm
container_name: kodik-metadata-service
restart: on-failure
env_file: ../../../.env
networks:
- anyame-consul
deploy:
resources:
limits:
memory: 512M
cpus: "1"
reservations:
memory: 128M
cpus: "0.3"
networks:
anyame-consul:
name: anyame-consul
external: true

View File

@@ -0,0 +1,23 @@
services:
kodik-metadata-service:
build:
context: ../../../.
dockerfile: src/main/docker/Dockerfile.native-micro
container_name: kodik-metadata-service
restart: on-failure
env_file: ../../../.env
networks:
- anyame-consul
deploy:
resources:
limits:
memory: 512M
cpus: "1"
reservations:
memory: 128M
cpus: "0.3"
networks:
anyame-consul:
name: anyame-consul
external: true

View File

@@ -7,11 +7,11 @@
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/code-with-quarkus-jvm .
# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/anyame-kodik-metadata-backend-jvm .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-jvm
# docker run -i --rm -p 8080:8080 quarkus/anyame-kodik-metadata-backend-jvm
#
# If you want to include the debug port into your docker image
# you will have to expose the debug port (default 5005 being the default) like this : EXPOSE 8080 5005.
@@ -20,7 +20,7 @@
#
# Then run the container using :
#
# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-jvm
# docker run -i --rm -p 8080:8080 quarkus/anyame-kodik-metadata-backend-jvm
#
# This image uses the `run-java.sh` script to run the application.
# This scripts computes the command line to execute your Java application, and
@@ -77,8 +77,10 @@
# - NO_PROXY: A comma separated lists of hosts, IP addresses or domains that can be
# accessed directly. (example: "foo.example.com,bar.example.com")
#
# You can find more information about the UBI base runtime images and their configuration here:
# https://rh-openjdk.github.io/redhat-openjdk-containers/
###
FROM registry.access.redhat.com/ubi9/openjdk-21:1.23
FROM registry.access.redhat.com/ubi9/openjdk-25:1.24
ENV LANGUAGE='en_US:en'

View File

@@ -7,11 +7,11 @@
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.legacy-jar -t quarkus/code-with-quarkus-legacy-jar .
# docker build -f src/main/docker/Dockerfile.legacy-jar -t quarkus/anyame-kodik-metadata-backend-legacy-jar .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-legacy-jar
# docker run -i --rm -p 8080:8080 quarkus/anyame-kodik-metadata-backend-legacy-jar
#
# If you want to include the debug port into your docker image
# you will have to expose the debug port (default 5005 being the default) like this : EXPOSE 8080 5005.
@@ -20,7 +20,7 @@
#
# Then run the container using :
#
# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-legacy-jar
# docker run -i --rm -p 8080:8080 quarkus/anyame-kodik-metadata-backend-legacy-jar
#
# This image uses the `run-java.sh` script to run the application.
# This scripts computes the command line to execute your Java application, and
@@ -77,8 +77,10 @@
# - NO_PROXY: A comma separated lists of hosts, IP addresses or domains that can be
# accessed directly. (example: "foo.example.com,bar.example.com")
#
# You can find more information about the UBI base runtime images and their configuration here:
# https://rh-openjdk.github.io/redhat-openjdk-containers/
###
FROM registry.access.redhat.com/ubi9/openjdk-21:1.23
FROM registry.access.redhat.com/ubi9/openjdk-25:1.23
ENV LANGUAGE='en_US:en'

View File

@@ -7,11 +7,11 @@
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.native -t quarkus/code-with-quarkus .
# docker build -f src/main/docker/Dockerfile.native -t quarkus/anyame-kodik-metadata-backend .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus
# docker run -i --rm -p 8080:8080 quarkus/anyame-kodik-metadata-backend
#
# The ` registry.access.redhat.com/ubi9/ubi-minimal:9.7` base image is based on UBI 9.
# To use UBI 8, switch to `quay.io/ubi8/ubi-minimal:8.10`.

View File

@@ -10,11 +10,11 @@
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.native-micro -t quarkus/code-with-quarkus .
# docker build -f src/main/docker/Dockerfile.native-micro -t quarkus/anyame-kodik-metadata-backend .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus
# docker run -i --rm -p 8080:8080 quarkus/anyame-kodik-metadata-backend
#
# The `quay.io/quarkus/ubi9-quarkus-micro-image:2.0` base image is based on UBI 9.
# To use UBI 8, switch to `quay.io/quarkus/quarkus-micro-image:2.0`.

View File

@@ -0,0 +1,30 @@
package com.backend.metadata.kodik.service;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import io.quarkus.arc.lookup.LookupIfProperty;
import io.vertx.core.Vertx;
import io.vertx.ext.consul.ConsulClient;
import io.vertx.ext.consul.ConsulClientOptions;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
@ApplicationScoped
public class ApplicationBeanProducer {
Logger logger = Logger.getLogger(ApplicationBeanProducer.class);
@ConfigProperty(name = "consul.host", defaultValue = "consul")
String host;
@ConfigProperty(name = "consul.port", defaultValue = "8500")
int port;
@Produces
@LookupIfProperty(name = "quarkus.stork.kodik-metadata-service.service-registrar.type", stringValue = "consul")
public ConsulClient consulClient(Vertx vertx) {
return ConsulClient.create(vertx, new ConsulClientOptions()
.setHost(host)
.setPort(port));
}
}

View File

@@ -0,0 +1,56 @@
package com.backend.metadata.kodik.service;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import io.vertx.ext.consul.ConsulClient;
import io.vertx.ext.consul.ServiceOptions;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.Instance;
@ApplicationScoped
public class ApplicationLifecycle {
@ConfigProperty(name = "quarkus.application.name")
private String appName;
@ConfigProperty(name = "quarkus.application.port", defaultValue = "8080")
private int port;
private Logger logger;
private Instance<ConsulClient> consulClient;
private ScheduledExecutorService executor;
public ApplicationLifecycle(Logger logger,
Instance<ConsulClient> consulClient,
ScheduledExecutorService executor) {
this.logger = logger;
this.consulClient = consulClient;
this.executor = executor;
}
void onStart(@Observes StartupEvent ev) {
if (consulClient.isResolvable()) {
executor.schedule(() -> {
consulClient.get().registerService(new ServiceOptions()
.setPort(port)
.setAddress(appName)
.setName(appName)
.setId(appName + "-" + port),
result -> logger.infof("Service %s-%d registered", appName, port));
}, 1000, TimeUnit.MILLISECONDS);
}
}
void onStop(@Observes ShutdownEvent ev) {
if (consulClient.isResolvable()) {
consulClient.get().deregisterService(appName + "-" + port,
result -> logger.infof("Service %s-%d deregistered", appName, port));
}
}
}

View File

@@ -1,4 +1,4 @@
package com.backend.search.kodik.service.api;
package com.backend.metadata.kodik.service.api;
import jakarta.enterprise.context.ApplicationScoped;

View File

@@ -1,6 +1,6 @@
package com.backend.search.kodik.service.api;
package com.backend.metadata.kodik.service.api;
import com.backend.search.kodik.service.api.model.KodikResponse;
import com.backend.metadata.kodik.service.api.model.KodikResponse;
import io.github.easyretrofit.core.annotation.RetrofitBuilder;
import retrofit2.Call;

View File

@@ -1,4 +1,4 @@
package com.backend.search.kodik.service.api.model;
package com.backend.metadata.kodik.service.api.model;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package com.backend.search.kodik.service.api.model;
package com.backend.metadata.kodik.service.api.model;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package com.backend.search.kodik.service.resource;
package com.backend.metadata.kodik.service.resource;
import java.io.IOException;
@@ -6,10 +6,10 @@ import javax.naming.ServiceUnavailableException;
import org.jboss.logging.Logger;
import com.backend.search.kodik.service.api.KodikAPI;
import com.backend.search.kodik.service.api.model.KodikResponse;
import com.backend.search.kodik.service.service.KodikAPITokenProvider;
import com.backend.search.kodik.service.service.KodikSearchFilterService;
import com.backend.metadata.kodik.service.api.KodikAPI;
import com.backend.metadata.kodik.service.api.model.KodikResponse;
import com.backend.metadata.kodik.service.service.KodikAPITokenProvider;
import com.backend.metadata.kodik.service.service.KodikSearchFilterService;
import io.quarkiverse.retrofit.runtime.EnableRetrofit;
import jakarta.ws.rs.BadRequestException;
@@ -18,8 +18,8 @@ import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import retrofit2.Response;
@EnableRetrofit("com.backend.search.kodik.service.api")
@Path("/search")
@EnableRetrofit("com.backend.metadata.kodik.service.api")
@Path("/kodik")
public class SearchResource {
private static final Logger LOG = Logger.getLogger(SearchResource.class);
private final KodikAPI kodikAPI;
@@ -34,6 +34,7 @@ public class SearchResource {
}
@GET
@Path("/search")
public KodikResponse search(@QueryParam("title") String title) throws ServiceUnavailableException {
try {
Response<KodikResponse> response = kodikAPI.search(tokenProvider.getKodikToken(), title, 100, 1).execute();

View File

@@ -1,4 +1,4 @@
package com.backend.search.kodik.service.service;
package com.backend.metadata.kodik.service.service;
import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.config.inject.ConfigProperty;

View File

@@ -1,6 +1,4 @@
package com.backend.search.kodik.service.service;
import jakarta.enterprise.context.ApplicationScoped;
package com.backend.metadata.kodik.service.service;
import java.util.ArrayList;
import java.util.Arrays;
@@ -11,7 +9,9 @@ import java.util.Set;
import org.jboss.logging.Logger;
import com.backend.search.kodik.service.api.model.KodikResponse;
import com.backend.metadata.kodik.service.api.model.KodikResponse;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class KodikSearchFilterService {

View File

@@ -1 +1,5 @@
quarkus.application.name=kodik-metadata-service
kodik.api.url=https://kodikapi.com/
quarkus.stork.kodik-metadata-service.service-registrar.type=consul

View File

@@ -1,8 +1,8 @@
package org.acme;
package com.backend.metadata.kodik.service;
import io.quarkus.test.junit.QuarkusIntegrationTest;
@QuarkusIntegrationTest
class GreetingResourceIT extends GreetingResourceTest {
class GreetingResourceIT {
// Execute the same tests but in packaged mode.
}

View File

@@ -1,20 +0,0 @@
package org.acme;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
@QuarkusTest
class GreetingResourceTest {
@Test
void testHelloEndpoint() {
given()
.when().get("/hello")
.then()
.statusCode(200)
.body(is("Hello from Quarkus REST"));
}
}