Piotr Pleska 3 nedēļas atpakaļ
vecāks
revīzija
4a5fc5c49e

+ 1 - 23
pom.xml

@@ -32,10 +32,6 @@
     </dependencyManagement>
 
     <dependencies>
-        <dependency>
-            <groupId>io.quarkus</groupId>
-            <artifactId>quarkus-hibernate-orm</artifactId>
-        </dependency>
         <dependency>
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-rest</artifactId>
@@ -46,25 +42,7 @@
         </dependency>
         <dependency>
             <groupId>io.quarkus</groupId>
-            <artifactId>quarkus-logging-json</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>io.quarkus</groupId>
-            <artifactId>quarkus-jdbc-postgresql</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>io.quarkus</groupId>
-            <artifactId>quarkus-arc</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>io.quarkus</groupId>
-            <artifactId>quarkus-junit</artifactId>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>io.rest-assured</groupId>
-            <artifactId>rest-assured</artifactId>
-            <scope>test</scope>
+            <artifactId>quarkus-scheduler</artifactId>
         </dependency>
     </dependencies>
 

+ 0 - 100
src/main/docker/Dockerfile.jvm

@@ -1,100 +0,0 @@
-####
-# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
-#
-# Before building the container image run:
-#
-# ./mvnw package
-#
-# Then, build the image with:
-#
-# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/order-service-jvm .
-#
-# Then run the container using:
-#
-# docker run -i --rm -p 8080:8080 quarkus/order-service-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.
-# Additionally you will have to set -e JAVA_DEBUG=true and -e JAVA_DEBUG_PORT=*:5005
-# when running the container
-#
-# Then run the container using :
-#
-# docker run -i --rm -p 8080:8080 quarkus/order-service-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
-# includes memory/GC tuning.
-# You can configure the behavior using the following environment properties:
-# - JAVA_OPTS: JVM options passed to the `java` command (example: "-verbose:class") - Be aware that this will override
-# the default JVM options, use `JAVA_OPTS_APPEND` to append options
-# - JAVA_OPTS_APPEND: User specified Java options to be appended to generated options
-#   in JAVA_OPTS (example: "-Dsome.property=foo")
-# - JAVA_MAX_MEM_RATIO: Is used when no `-Xmx` option is given in JAVA_OPTS. This is
-#   used to calculate a default maximal heap memory based on a containers restriction.
-#   If used in a container without any memory constraints for the container then this
-#   option has no effect. If there is a memory constraint then `-Xmx` is set to a ratio
-#   of the container available memory as set here. The default is `50` which means 50%
-#   of the available memory is used as an upper boundary. You can skip this mechanism by
-#   setting this value to `0` in which case no `-Xmx` option is added.
-# - JAVA_INITIAL_MEM_RATIO: Is used when no `-Xms` option is given in JAVA_OPTS. This
-#   is used to calculate a default initial heap memory based on the maximum heap memory.
-#   If used in a container without any memory constraints for the container then this
-#   option has no effect. If there is a memory constraint then `-Xms` is set to a ratio
-#   of the `-Xmx` memory as set here. The default is `25` which means 25% of the `-Xmx`
-#   is used as the initial heap size. You can skip this mechanism by setting this value
-#   to `0` in which case no `-Xms` option is added (example: "25")
-# - JAVA_MAX_INITIAL_MEM: Is used when no `-Xms` option is given in JAVA_OPTS.
-#   This is used to calculate the maximum value of the initial heap memory. If used in
-#   a container without any memory constraints for the container then this option has
-#   no effect. If there is a memory constraint then `-Xms` is limited to the value set
-#   here. The default is 4096MB which means the calculated value of `-Xms` never will
-#   be greater than 4096MB. The value of this variable is expressed in MB (example: "4096")
-# - JAVA_DIAGNOSTICS: Set this to get some diagnostics information to standard output
-#   when things are happening. This option, if set to true, will set
-#  `-XX:+UnlockDiagnosticVMOptions`. Disabled by default (example: "true").
-# - JAVA_DEBUG: If set remote debugging will be switched on. Disabled by default (example:
-#    true").
-# - JAVA_DEBUG_PORT: Port used for remote debugging. Defaults to 5005 (example: "8787").
-# - CONTAINER_CORE_LIMIT: A calculated core limit as described in
-#   https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt. (example: "2")
-# - CONTAINER_MAX_MEMORY: Memory limit given to the container (example: "1024").
-# - GC_MIN_HEAP_FREE_RATIO: Minimum percentage of heap free after GC to avoid expansion.
-#   (example: "20")
-# - GC_MAX_HEAP_FREE_RATIO: Maximum percentage of heap free after GC to avoid shrinking.
-#   (example: "40")
-# - GC_TIME_RATIO: Specifies the ratio of the time spent outside the garbage collection.
-#   (example: "4")
-# - GC_ADAPTIVE_SIZE_POLICY_WEIGHT: The weighting given to the current GC time versus
-#   previous GC times. (example: "90")
-# - GC_METASPACE_SIZE: The initial metaspace size. (example: "20")
-# - GC_MAX_METASPACE_SIZE: The maximum metaspace size. (example: "100")
-# - GC_CONTAINER_OPTIONS: Specify Java GC to use. The value of this variable should
-#   contain the necessary JRE command-line options to specify the required GC, which
-#   will override the default of `-XX:+UseParallelGC` (example: -XX:+UseG1GC).
-# - HTTPS_PROXY: The location of the https proxy. (example: "myuser@127.0.0.1:8080")
-# - HTTP_PROXY: The location of the http proxy. (example: "myuser@127.0.0.1:8080")
-# - 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-runtime:1.24
-
-ENV LANGUAGE='en_US:en'
-
-
-# We make four distinct layers so if there are application changes the library layers can be re-used
-COPY --chown=185 target/quarkus-app/lib/ /deployments/lib/
-COPY --chown=185 target/quarkus-app/*.jar /deployments/
-COPY --chown=185 target/quarkus-app/app/ /deployments/app/
-COPY --chown=185 target/quarkus-app/quarkus/ /deployments/quarkus/
-
-EXPOSE 8080
-USER 185
-ENV JAVA_OPTS_APPEND="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
-ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"
-
-ENTRYPOINT [ "/opt/jboss/container/java/run/run-java.sh" ]
-

+ 0 - 96
src/main/docker/Dockerfile.legacy-jar

@@ -1,96 +0,0 @@
-####
-# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
-#
-# Before building the container image run:
-#
-# ./mvnw package -Dquarkus.package.jar.type=legacy-jar
-#
-# Then, build the image with:
-#
-# docker build -f src/main/docker/Dockerfile.legacy-jar -t quarkus/order-service-legacy-jar .
-#
-# Then run the container using:
-#
-# docker run -i --rm -p 8080:8080 quarkus/order-service-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.
-# Additionally you will have to set -e JAVA_DEBUG=true and -e JAVA_DEBUG_PORT=*:5005
-# when running the container
-#
-# Then run the container using :
-#
-# docker run -i --rm -p 8080:8080 quarkus/order-service-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
-# includes memory/GC tuning.
-# You can configure the behavior using the following environment properties:
-# - JAVA_OPTS: JVM options passed to the `java` command (example: "-verbose:class") - Be aware that this will override
-# the default JVM options, use `JAVA_OPTS_APPEND` to append options
-# - JAVA_OPTS_APPEND: User specified Java options to be appended to generated options
-#   in JAVA_OPTS (example: "-Dsome.property=foo")
-# - JAVA_MAX_MEM_RATIO: Is used when no `-Xmx` option is given in JAVA_OPTS. This is
-#   used to calculate a default maximal heap memory based on a containers restriction.
-#   If used in a container without any memory constraints for the container then this
-#   option has no effect. If there is a memory constraint then `-Xmx` is set to a ratio
-#   of the container available memory as set here. The default is `50` which means 50%
-#   of the available memory is used as an upper boundary. You can skip this mechanism by
-#   setting this value to `0` in which case no `-Xmx` option is added.
-# - JAVA_INITIAL_MEM_RATIO: Is used when no `-Xms` option is given in JAVA_OPTS. This
-#   is used to calculate a default initial heap memory based on the maximum heap memory.
-#   If used in a container without any memory constraints for the container then this
-#   option has no effect. If there is a memory constraint then `-Xms` is set to a ratio
-#   of the `-Xmx` memory as set here. The default is `25` which means 25% of the `-Xmx`
-#   is used as the initial heap size. You can skip this mechanism by setting this value
-#   to `0` in which case no `-Xms` option is added (example: "25")
-# - JAVA_MAX_INITIAL_MEM: Is used when no `-Xms` option is given in JAVA_OPTS.
-#   This is used to calculate the maximum value of the initial heap memory. If used in
-#   a container without any memory constraints for the container then this option has
-#   no effect. If there is a memory constraint then `-Xms` is limited to the value set
-#   here. The default is 4096MB which means the calculated value of `-Xms` never will
-#   be greater than 4096MB. The value of this variable is expressed in MB (example: "4096")
-# - JAVA_DIAGNOSTICS: Set this to get some diagnostics information to standard output
-#   when things are happening. This option, if set to true, will set
-#  `-XX:+UnlockDiagnosticVMOptions`. Disabled by default (example: "true").
-# - JAVA_DEBUG: If set remote debugging will be switched on. Disabled by default (example:
-#    true").
-# - JAVA_DEBUG_PORT: Port used for remote debugging. Defaults to 5005 (example: "8787").
-# - CONTAINER_CORE_LIMIT: A calculated core limit as described in
-#   https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt. (example: "2")
-# - CONTAINER_MAX_MEMORY: Memory limit given to the container (example: "1024").
-# - GC_MIN_HEAP_FREE_RATIO: Minimum percentage of heap free after GC to avoid expansion.
-#   (example: "20")
-# - GC_MAX_HEAP_FREE_RATIO: Maximum percentage of heap free after GC to avoid shrinking.
-#   (example: "40")
-# - GC_TIME_RATIO: Specifies the ratio of the time spent outside the garbage collection.
-#   (example: "4")
-# - GC_ADAPTIVE_SIZE_POLICY_WEIGHT: The weighting given to the current GC time versus
-#   previous GC times. (example: "90")
-# - GC_METASPACE_SIZE: The initial metaspace size. (example: "20")
-# - GC_MAX_METASPACE_SIZE: The maximum metaspace size. (example: "100")
-# - GC_CONTAINER_OPTIONS: Specify Java GC to use. The value of this variable should
-#   contain the necessary JRE command-line options to specify the required GC, which
-#   will override the default of `-XX:+UseParallelGC` (example: -XX:+UseG1GC).
-# - HTTPS_PROXY: The location of the https proxy. (example: "myuser@127.0.0.1:8080")
-# - HTTP_PROXY: The location of the http proxy. (example: "myuser@127.0.0.1:8080")
-# - 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-runtime:1.24
-
-ENV LANGUAGE='en_US:en'
-
-
-COPY target/lib/* /deployments/lib/
-COPY target/*-runner.jar /deployments/quarkus-run.jar
-
-EXPOSE 8080
-USER 185
-ENV JAVA_OPTS_APPEND="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
-ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"
-
-ENTRYPOINT [ "/opt/jboss/container/java/run/run-java.sh" ]

+ 0 - 29
src/main/docker/Dockerfile.native

@@ -1,29 +0,0 @@
-####
-# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode.
-#
-# Before building the container image run:
-#
-# ./mvnw package -Dnative
-#
-# Then, build the image with:
-#
-# docker build -f src/main/docker/Dockerfile.native -t quarkus/order-service .
-#
-# Then run the container using:
-#
-# docker run -i --rm -p 8080:8080 quarkus/order-service
-#
-# 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`.
-###
-FROM registry.access.redhat.com/ubi9/ubi-minimal:9.7
-WORKDIR /work/
-RUN chown 1001 /work \
-    && chmod "g+rwX" /work \
-    && chown 1001:root /work
-COPY --chown=1001:root --chmod=0755 target/*-runner /work/application
-
-EXPOSE 8080
-USER 1001
-
-ENTRYPOINT ["./application", "-Dquarkus.http.host=0.0.0.0"]

+ 0 - 32
src/main/docker/Dockerfile.native-micro

@@ -1,32 +0,0 @@
-####
-# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode.
-# It uses a micro base image, tuned for Quarkus native executables.
-# It reduces the size of the resulting container image.
-# Check https://quarkus.io/guides/quarkus-runtime-base-image for further information about this image.
-#
-# Before building the container image run:
-#
-# ./mvnw package -Dnative
-#
-# Then, build the image with:
-#
-# docker build -f src/main/docker/Dockerfile.native-micro -t quarkus/order-service .
-#
-# Then run the container using:
-#
-# docker run -i --rm -p 8080:8080 quarkus/order-service
-#
-# 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`.
-###
-FROM quay.io/quarkus/ubi9-quarkus-micro-image:2.0
-WORKDIR /work/
-RUN chown 1001 /work \
-    && chmod "g+rwX" /work \
-    && chown 1001:root /work
-COPY --chown=1001:root --chmod=0755 target/*-runner /work/application
-
-EXPOSE 8080
-USER 1001
-
-ENTRYPOINT ["./application", "-Dquarkus.http.host=0.0.0.0"]

+ 73 - 0
src/main/java/p/lodz/pl/EurekaRegistrar.java

@@ -0,0 +1,73 @@
+package p.lodz.pl;
+
+import io.quarkus.runtime.StartupEvent;
+import io.quarkus.scheduler.Scheduled;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.event.Observes;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+
+@ApplicationScoped
+public class EurekaRegistrar {
+
+    private final HttpClient client = HttpClient.newHttpClient();
+    private final String appName = "ORDER-SERVICE";
+    private final String instanceId = "localhost:order-service:8081";
+    private final String eurekaUrl = "http://localhost:8761/eureka/apps/" + appName;
+
+    void onStart(@Observes StartupEvent ev) {
+        String json = """
+                {
+                  "instance": {
+                    "instanceId": "%s",
+                    "hostName": "localhost",
+                    "app": "%s",
+                    "vipAddress": "order-service",
+                    "secureVipAddress": "order-service",
+                    "ipAddr": "127.0.0.1",
+                    "status": "UP",
+                    "port": {"$": 8081, "@enabled": "true"},
+                    "dataCenterInfo": {
+                      "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
+                      "name": "MyOwn"
+                    }
+                  }
+                }
+                """.formatted(instanceId, appName);
+
+        try {
+            HttpRequest request = HttpRequest.newBuilder()
+                    .uri(URI.create(eurekaUrl))
+                    .header("Content-Type", "application/json")
+                    .POST(HttpRequest.BodyPublishers.ofString(json))
+                    .build();
+
+            client.send(request, HttpResponse.BodyHandlers.ofString());
+            System.out.println("Registered to Eureka");
+        } catch (Exception e) {
+            System.err.println("There was an error with registering: " + e.getMessage());
+        }
+    }
+
+    @Scheduled(every = "30s")
+    void sendHeartbeat() {
+        try {
+            HttpRequest request = HttpRequest.newBuilder()
+                    .uri(URI.create(eurekaUrl + "/" + instanceId))
+                    .PUT(HttpRequest.BodyPublishers.noBody())
+                    .build();
+
+            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
+
+            if (response.statusCode() == 404) {
+                System.out.println("Instance not found in Eureka, re-registering...");
+                onStart(null);
+            }
+        } catch (Exception e) {
+            System.err.println("There was an error: " + e.getMessage());
+        }
+    }
+}

+ 1 - 2
src/main/java/p/lodz/pl/ExampleResource.java

@@ -11,7 +11,6 @@ public class ExampleResource {
     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String hello() {
-        return "Hello from Quarkus REST";
+        return "Hello from Quarkus Order Service!";
     }
 }
-    

+ 22 - 0
src/main/java/p/lodz/pl/GatewayFilter.java

@@ -0,0 +1,22 @@
+package p.lodz.pl;
+
+import jakarta.annotation.Priority;
+import jakarta.ws.rs.Priorities;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.core.Response;
+import jakarta.ws.rs.ext.Provider;
+
+@Provider
+@Priority(Priorities.AUTHENTICATION)
+public class GatewayFilter implements ContainerRequestFilter {
+
+    @Override
+    public void filter(ContainerRequestContext requestContext) {
+        String secret = requestContext.getHeaderString("X-Gateway-Secret");
+
+        if (!"most-secret-header-value".equals(secret)) {
+            requestContext.abortWith(Response.status(403).build());
+        }
+    }
+}

+ 0 - 12
src/main/java/p/lodz/pl/GreetingConfig.java

@@ -1,12 +0,0 @@
-package p.lodz.pl;
-
-import io.smallrye.config.ConfigMapping;
-import io.smallrye.config.WithName;
-
-@ConfigMapping(prefix = "greeting")
-public interface GreetingConfig {
-
-    @WithName("message")
-    String message();
-
-}

+ 0 - 32
src/main/java/p/lodz/pl/MyEntity.java

@@ -1,32 +0,0 @@
-//package p.lodz.pl;
-//
-//import jakarta.persistence.Entity;
-//import jakarta.persistence.GeneratedValue;
-//import jakarta.persistence.Id;
-//
-///**
-// * Example JPA entity.
-// * <p>
-// * To use it, get access to a JPA EntityManager via injection.
-// * <p>
-// * {@code
-// *
-// * @Inject EntityManager em;
-// * <p>
-// * public void doSomething() {
-// * MyEntity entity1 = new MyEntity();
-// * entity1.field = "field-1";
-// * em.persist(entity1);
-// * <p>
-// * List<MyEntity> entities = em.createQuery("from MyEntity", MyEntity.class).getResultList();
-// * }
-// * }
-// */
-//@Entity
-//public class MyEntity {
-//    @Id
-//    @GeneratedValue
-//    public Long id;
-//
-//    public String field;
-//}

+ 8 - 0
src/main/resources/application.properties

@@ -0,0 +1,8 @@
+quarkus.application.name=order-service
+quarkus.http.port=8081
+quarkus.eureka.prefer-same-zone=true
+quarkus.eureka.service-url.default=http://localhost:8761/eureka
+quarkus.eureka.metadata.app-key=order-service
+quarkus.eureka.heartbeat.enabled=true
+quarkus.eureka.heartbeat.health-path=/info/health
+quarkus.eureka.heartbeat.status-path=/info/status

+ 0 - 2
src/main/resources/application.yml

@@ -1,2 +0,0 @@
-greeting:
-  message: "hello"

+ 0 - 6
src/main/resources/import.sql

@@ -1,6 +0,0 @@
--- This file allow to write SQL commands that will be emitted in test and dev.
--- The commands are commented as their support depends of the database
--- insert into myentity (id, field) values(1, 'field-1');
--- insert into myentity (id, field) values(2, 'field-2');
--- insert into myentity (id, field) values(3, 'field-3');
--- alter sequence myentity_seq restart with 4;

+ 8 - 8
src/test/java/p/lodz/pl/ExampleResourceIT.java

@@ -1,8 +1,8 @@
-package p.lodz.pl;
-
-import io.quarkus.test.junit.QuarkusIntegrationTest;
-
-@QuarkusIntegrationTest
-class ExampleResourceIT extends ExampleResourceTest {
-    // Execute the same tests but in packaged mode.
-}
+//package p.lodz.pl;
+//
+//import io.quarkus.test.junit.QuarkusIntegrationTest;
+//
+//@QuarkusIntegrationTest
+//class ExampleResourceIT extends ExampleResourceTest {
+//    // Execute the same tests but in packaged mode.
+//}

+ 20 - 20
src/test/java/p/lodz/pl/ExampleResourceTest.java

@@ -1,20 +1,20 @@
-package p.lodz.pl;
-
-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 ExampleResourceTest {
-    @Test
-    void testHelloEndpoint() {
-        given()
-                .when().get("/hello")
-                .then()
-                .statusCode(200)
-                .body(is("Hello from Quarkus REST"));
-    }
-
-}
+//package p.lodz.pl;
+//
+//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 ExampleResourceTest {
+//    @Test
+//    void testHelloEndpoint() {
+//        given()
+//                .when().get("/hello")
+//                .then()
+//                .statusCode(200)
+//                .body(is("Hello from Quarkus REST"));
+//    }
+//
+//}