From 9830e3e1bf021ee29ef07934d61847962a7d9706 Mon Sep 17 00:00:00 2001 From: AleixMT Date: Tue, 16 Jan 2024 23:33:44 +0100 Subject: [PATCH] ADDED: communication with eureka --- docker-compose.yml | 14 +++++++--- pom.xml | 7 +++++ .../controller/EurekaGreetingController.java | 8 ++++++ .../EurekaGreetingControllerImpl.java | 26 +++++++++++++++++++ .../resources/application-prod.properties | 12 +++++++++ src/main/resources/application.properties | 11 ++++++++ 6 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/unir/missi/desarrollowebfullstack/bookabook/operador/controller/EurekaGreetingController.java create mode 100644 src/main/java/net/unir/missi/desarrollowebfullstack/bookabook/operador/controller/EurekaGreetingControllerImpl.java diff --git a/docker-compose.yml b/docker-compose.yml index e2dbe33..e3d4716 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,10 +1,10 @@ --- version: "2.1" services: - book-a-book-buscador: - image: aleixmt/book-a-book-buscador:latest - ports: - - "0.0.0.0:8082:8081" + #book-a-book-buscador: + # image: aleixmt/book-a-book-buscador:latest + # ports: + # - "0.0.0.0:8082:8081" #restart: unless-stopped book-a-book-operador: @@ -13,6 +13,12 @@ services: - "0.0.0.0:8084:8083" #restart: unless-stopped + book-a-book-eureka: + image: aleixmt/book-a-book-eureka:latest + ports: + - "0.0.0.0:8762:8761" + restart: unless-stopped + postgres_db: image: postgres environment: diff --git a/pom.xml b/pom.xml index 23bdebc..2a7d29e 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,13 @@ test + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + 4.1.0 + + org.springdoc diff --git a/src/main/java/net/unir/missi/desarrollowebfullstack/bookabook/operador/controller/EurekaGreetingController.java b/src/main/java/net/unir/missi/desarrollowebfullstack/bookabook/operador/controller/EurekaGreetingController.java new file mode 100644 index 0000000..57b08a8 --- /dev/null +++ b/src/main/java/net/unir/missi/desarrollowebfullstack/bookabook/operador/controller/EurekaGreetingController.java @@ -0,0 +1,8 @@ +package net.unir.missi.desarrollowebfullstack.bookabook.operador.controller; + +import org.springframework.web.bind.annotation.RequestMapping; + +public interface EurekaGreetingController { + + String greeting(); +} diff --git a/src/main/java/net/unir/missi/desarrollowebfullstack/bookabook/operador/controller/EurekaGreetingControllerImpl.java b/src/main/java/net/unir/missi/desarrollowebfullstack/bookabook/operador/controller/EurekaGreetingControllerImpl.java new file mode 100644 index 0000000..1c1def6 --- /dev/null +++ b/src/main/java/net/unir/missi/desarrollowebfullstack/bookabook/operador/controller/EurekaGreetingControllerImpl.java @@ -0,0 +1,26 @@ +package net.unir.missi.desarrollowebfullstack.bookabook.operador.controller; + +import com.netflix.discovery.EurekaClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Lazy; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class EurekaGreetingControllerImpl implements EurekaGreetingController { + + @Autowired + @Lazy + private EurekaClient eurekaClient; + + @Value("${spring.application.name}") + private String appName; + + @Override + @RequestMapping("/greeting") + public String greeting() { + return String.format( + "Hello from '%s'!", eurekaClient.getApplication(appName).getName()); + } +} \ No newline at end of file diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index e6774cc..3f9c1b8 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -19,3 +19,15 @@ spring.jpa.show-sql=true # Format of the SQL queries written in the output spring.jpa.properties.hibernate.format_sql=true +###################### +### SERVER CONFIG #### +###################### +# Random port because we will access this service by its name +server.port=0 + + +############## +### EUREKA ### +############## +# URL of the Eureka service +eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://book-a-book-eureka:8761/eureka} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c7e40ec..7b0159d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -50,3 +50,14 @@ server.port=8083 ############## # swagger-ui custom path springdoc.swagger-ui.path=/swagger-ui.html + + +############## +### EUREKA ### +############## +# Configure application name for +spring.application.name=operador +# URL of the Eureka service +eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka} +# Use IP address +eureka.instance.preferIpAddress=true