Basic integration of consul registration
This commit is contained in:
23
src/main/docker-compose/jvm.yml
Normal file
23
src/main/docker-compose/jvm.yml
Normal 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
|
||||
23
src/main/docker-compose/native.yml
Normal file
23
src/main/docker-compose/native.yml
Normal 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
|
||||
@@ -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'
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
|
||||
@@ -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`.
|
||||
|
||||
@@ -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`.
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.backend.search.kodik.service.api;
|
||||
package com.backend.metadata.kodik.service.api;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
@@ -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;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.backend.search.kodik.service.api.model;
|
||||
package com.backend.metadata.kodik.service.api.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.backend.search.kodik.service.api.model;
|
||||
package com.backend.metadata.kodik.service.api.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -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();
|
||||
@@ -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;
|
||||
@@ -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 {
|
||||
@@ -1 +1,5 @@
|
||||
quarkus.application.name=kodik-metadata-service
|
||||
kodik.api.url=https://kodikapi.com/
|
||||
|
||||
quarkus.stork.kodik-metadata-service.service-registrar.type=consul
|
||||
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user