From 928a23494d5c3ca99d95ab2c8ed13978f01f3da1 Mon Sep 17 00:00:00 2001 From: caroline Date: Sat, 23 Feb 2019 22:49:32 +0100 Subject: [PATCH] New code for article BAEL-2460. Added maven module spring-cloud-kubernetes-project --- .../client-service/Dockerfile | 12 +++ .../client-service/pom.xml | 71 ++++++++++++++ .../services/client/ClientApplication.java | 96 +++++++++++++++++++ .../services/client/config/ClientConfig.java | 19 ++++ .../client/controller/ClientController.java | 43 +++++++++ .../service/TravelAgencyClientService.java | 45 +++++++++ .../src/main/resources/bootstrap.yml | 6 ++ .../gateway-service/Dockerfile | 12 +++ .../gateway-service/pom.xml | 76 +++++++++++++++ .../services/gateway/GatewayApplication.java | 44 +++++++++ .../src/main/resources/bootstrap.yml | 6 ++ .../spring-cloud-kubernetes-project/pom.xml | 27 ++++++ .../travel-agency-service/Dockerfile | 12 +++ .../travel-agency-service/pom.xml | 59 ++++++++++++ .../travelagency/TravelAgencyApplication.java | 24 +++++ .../controller/TravelAgencyController.java | 36 +++++++ .../src/main/resources/bootstrap.yml | 6 ++ 17 files changed, 594 insertions(+) create mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/Dockerfile create mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/pom.xml create mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/ClientApplication.java create mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/config/ClientConfig.java create mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/controller/ClientController.java create mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/service/TravelAgencyClientService.java create mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/resources/bootstrap.yml create mode 100644 spring-cloud/spring-cloud-kubernetes-project/gateway-service/Dockerfile create mode 100644 spring-cloud/spring-cloud-kubernetes-project/gateway-service/pom.xml create mode 100644 spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/gateway/GatewayApplication.java create mode 100644 spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/resources/bootstrap.yml create mode 100644 spring-cloud/spring-cloud-kubernetes-project/pom.xml create mode 100644 spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/Dockerfile create mode 100644 spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/pom.xml create mode 100644 spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/TravelAgencyApplication.java create mode 100644 spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/controller/TravelAgencyController.java create mode 100644 spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/resources/bootstrap.yml diff --git a/spring-cloud/spring-cloud-kubernetes-project/client-service/Dockerfile b/spring-cloud/spring-cloud-kubernetes-project/client-service/Dockerfile new file mode 100644 index 0000000000..93e75216db --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/client-service/Dockerfile @@ -0,0 +1,12 @@ +# requires Docker version 17.05.0-ce-rc1, build 2878a85 +FROM maven:3.5-jdk-8 as BUILDAGENCY + +COPY src /usr/src/myapp/src +COPY pom.xml /usr/src/myapp +RUN mvn -f /usr/src/myapp/pom.xml clean package -DskipTests + +FROM openjdk:alpine + +COPY --from=BUILDAGENCY /usr/src/myapp/target/*.jar /maven/ + +CMD java $JAVA_OPTS -jar maven/*.jar \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-project/client-service/pom.xml b/spring-cloud/spring-cloud-kubernetes-project/client-service/pom.xml new file mode 100644 index 0000000000..6bba5512cd --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/client-service/pom.xml @@ -0,0 +1,71 @@ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.6.RELEASE + + client-service + com.baeldung.spring.cloud + 1.0-SNAPSHOT + + + 1.8 + 2.0.1.RELEASE + Finchley.SR2 + 0.3.0.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud-dependencies.version} + pom + import + + + org.springframework.cloud + spring-cloud-kubernetes-dependencies + ${spring.cloud.k8s.version} + pom + import + + + + + + + org.springframework.cloud + spring-cloud-kubernetes-discovery + + + org.springframework.cloud + spring-cloud-starter-kubernetes-config + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/ClientApplication.java b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/ClientApplication.java new file mode 100644 index 0000000000..9044de9574 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/ClientApplication.java @@ -0,0 +1,96 @@ +package com.baeldung.spring.cloud.kubernetes.services.department; + +import com.baeldung.spring.cloud.kubernetes.services.client.config.ClientConfig; +import com.baeldung.spring.cloud.kubernetes.services.client.service.TravelAgencyClientService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.client.RestTemplate; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.List; +import java.util.Map; + +@SpringBootApplication +@EnableDiscoveryClient +@EnableCircuitBreaker +public class ClientApplication implements CommandLineRunner { + + private static final Log log = LogFactory.getLog(ClientApplication.class); + private static final String FIND_TRAVEL_DEALS_TASK = "Find new travel deals"; + + @Autowired + private DiscoveryClient discoveryClient; + + @Autowired + private TravelAgencyClientService travelAgencyClient; + + @Autowired + private ClientConfig clientConfig; + + private String task = FIND_TRAVEL_DEALS_TASK; + + @Bean + private RestTemplate restTemplate() { + return new RestTemplate(); + } + + @Value("${spring.application.name}") + private String appName; + + public static void main(String[] args) { + SpringApplication.run(ClientApplication.class, args); + } + + @Override + public void run(String... args) { + log.info("Client (" + appName + ":" + clientConfig.getType() + ")Started! "); + } + + /* + * Every 10 seconds look for new deals + */ + @Scheduled(fixedRate = 10000) + public void doSomeWork() throws UnknownHostException { + if (task.equals(FIND_TRAVEL_DEALS_TASK)) { + task = findNewDeals(); + if (task.equals(FIND_TRAVEL_DEALS_TASK)) { + log.info("NO DEAL FOUND, I will keep looking for one "); + } + } + log.info(">>> Working on " + task); + } + + + + + private String findNewDeals() throws UnknownHostException { + List services = this.discoveryClient.getServices(); + + for (String service : services) { + List instances = this.discoveryClient.getInstances(service); + for (ServiceInstance se : instances) { + Map metadata = se.getMetadata(); + String type = metadata.get("type"); + if ("deal".equals(type)) { + + String from = appName + "@" + InetAddress.getLocalHost().getHostName(); + String url = "http://" + se.getServiceId(); + return travelAgencyClient.requestDeals(url, from); + } + } + } + return FIND_TRAVEL_DEALS_TASK; + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/config/ClientConfig.java b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/config/ClientConfig.java new file mode 100644 index 0000000000..a4f43e2ddc --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/config/ClientConfig.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.cloud.kubernetes.services.client.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "client") +public class ClientConfig { + + private String type = "generic-client"; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/controller/ClientController.java b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/controller/ClientController.java new file mode 100644 index 0000000000..dad736953e --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/controller/ClientController.java @@ -0,0 +1,43 @@ +package com.baeldung.spring.cloud.kubernetes.services.client.controller; + +import java.io.UnsupportedEncodingException; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import com.baeldung.spring.cloud.kubernetes.services.client.config.ClientConfig; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +@RestController +@RefreshScope +public class ClientController { + + private static final Log log = LogFactory.getLog(ClientController.class); + + private enum ClientTravelType { + BUSINESS, + STUDENT, + COUPLE, + FRIENDS, + SINGLE, + FAMILY; + } + + @Autowired + private ClientConfig clientConfig; + + @RequestMapping(method = GET) + public String get() throws UnknownHostException, UnsupportedEncodingException { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Host: ").append(InetAddress.getLocalHost().getHostName()).append("
"); + stringBuilder.append("Client Type: ").append(clientConfig.getType()).append("
"); + stringBuilder.append("IP: ").append(InetAddress.getLocalHost().getHostAddress()).append("
"); + return stringBuilder.toString(); + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/service/TravelAgencyClientService.java b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/service/TravelAgencyClientService.java new file mode 100644 index 0000000000..ba86808cd5 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/service/TravelAgencyClientService.java @@ -0,0 +1,45 @@ +package com.baeldung.spring.cloud.kubernetes.services.client.service; + +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; +import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Service +public class TravelAgencyClientService { + + private final RestTemplate restTemplate; + + private static final Log log = LogFactory.getLog(TravelAgencyClientService.class); + + public static final String FIND_NEW_TRAVEL_DEALS = "find new travel deals"; + + public TravelAgencyClientService(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + @HystrixCommand(fallbackMethod = "getFallbackName", commandProperties = { + @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") + }) + public String requestDeals(String to, + String from) { + + String url = String.format("%s/deals/%s", + to, + from); + + log.info("--- Requesting travel deals to travel agency " + url); + + return restTemplate.getForObject(url, String.class); + } + + private String getFallbackName(String to, + String from) { + log.error("--- This travel agency (" + to + ") not available now, please come back later (Fallback) client:" + from); + return FIND_NEW_TRAVEL_DEALS; + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/resources/bootstrap.yml b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000..736a17b041 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/resources/bootstrap.yml @@ -0,0 +1,6 @@ +spring: + application: + name: client-service + cloud: + config: + uri: http://localhost:8088 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-project/gateway-service/Dockerfile b/spring-cloud/spring-cloud-kubernetes-project/gateway-service/Dockerfile new file mode 100644 index 0000000000..93e75216db --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/gateway-service/Dockerfile @@ -0,0 +1,12 @@ +# requires Docker version 17.05.0-ce-rc1, build 2878a85 +FROM maven:3.5-jdk-8 as BUILDAGENCY + +COPY src /usr/src/myapp/src +COPY pom.xml /usr/src/myapp +RUN mvn -f /usr/src/myapp/pom.xml clean package -DskipTests + +FROM openjdk:alpine + +COPY --from=BUILDAGENCY /usr/src/myapp/target/*.jar /maven/ + +CMD java $JAVA_OPTS -jar maven/*.jar \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-project/gateway-service/pom.xml b/spring-cloud/spring-cloud-kubernetes-project/gateway-service/pom.xml new file mode 100644 index 0000000000..84eb353fb9 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/gateway-service/pom.xml @@ -0,0 +1,76 @@ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.6.RELEASE + + gateway-service + com.baeldung.spring.cloud + 1.0-SNAPSHOT + + + 1.8 + 2.0.1.RELEASE + Finchley.SR2 + 0.3.0.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud-dependencies.version} + pom + import + + + org.springframework.cloud + spring-cloud-kubernetes-dependencies + ${spring.cloud.k8s.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix + + + org.springframework.cloud + spring-cloud-starter-gateway + + + org.springframework.cloud + spring-cloud-kubernetes-discovery + + + org.springframework.cloud + spring-cloud-kubernetes-ribbon + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/gateway/GatewayApplication.java b/spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/gateway/GatewayApplication.java new file mode 100644 index 0000000000..7f11906aba --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/gateway/GatewayApplication.java @@ -0,0 +1,44 @@ +package com.baeldung.spring.cloud.kubernetes.services.gateway; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator; +import org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.client.RestTemplate; + +@EnableScheduling +@EnableDiscoveryClient +@SpringBootApplication +public class GatewayApplication { + + private static final Log log = LogFactory.getLog(GatewayApplication.class); + + @Autowired + private DiscoveryClient discoveryClient; + + @Bean + public DiscoveryClientRouteDefinitionLocator discoveryClientRouteLocator(DiscoveryClient discoveryClient, + DiscoveryLocatorProperties properties) { + return new DiscoveryClientRouteDefinitionLocator(discoveryClient, + properties); + } + + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class, + args); + } + + @LoadBalanced + @Bean + RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/resources/bootstrap.yml b/spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000..5fd607d335 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/resources/bootstrap.yml @@ -0,0 +1,6 @@ +spring: + application: + name: gateway-service + cloud: + config: + uri: http://localhost:8088 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-project/pom.xml b/spring-cloud/spring-cloud-kubernetes-project/pom.xml new file mode 100644 index 0000000000..dc4e3bfe70 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + com.baeldung.spring.cloud + spring-cloud-kubernetes-project + 1.0.0-SNAPSHOT + spring-cloud-kubernetes-project + Spring Cloud Kubernetes + pom + + + travel-agency-service + client-service + gateway-service + + + + com.baeldung.spring.cloud + spring-cloud + 1.0.0-SNAPSHOT + .. + + + + + diff --git a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/Dockerfile b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/Dockerfile new file mode 100644 index 0000000000..93e75216db --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/Dockerfile @@ -0,0 +1,12 @@ +# requires Docker version 17.05.0-ce-rc1, build 2878a85 +FROM maven:3.5-jdk-8 as BUILDAGENCY + +COPY src /usr/src/myapp/src +COPY pom.xml /usr/src/myapp +RUN mvn -f /usr/src/myapp/pom.xml clean package -DskipTests + +FROM openjdk:alpine + +COPY --from=BUILDAGENCY /usr/src/myapp/target/*.jar /maven/ + +CMD java $JAVA_OPTS -jar maven/*.jar \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/pom.xml b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/pom.xml new file mode 100644 index 0000000000..69cd22ae52 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/pom.xml @@ -0,0 +1,59 @@ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.6.RELEASE + + travel-agency-service + com.baeldung.spring.cloud + 1.0-SNAPSHOT + + + 1.8 + 2.0.1.RELEASE + Finchley.SR2 + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud-dependencies.version} + pom + import + + + + + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-actuator + + + org.springframework.boot + spring-boot-actuator-autoconfigure + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/TravelAgencyApplication.java b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/TravelAgencyApplication.java new file mode 100644 index 0000000000..c96b547e18 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/TravelAgencyApplication.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.cloud.kubernetes.services.department; + +import com.baeldung.spring.cloud.kubernetes.services.travelagency.controller.TravelAgencyController; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class TravelAgencyApplication implements CommandLineRunner { + + private static final Log log = LogFactory.getLog(TravelAgencyController.class); + + public static void main(String[] args) { + SpringApplication.run(TravelAgencyApplication.class, args); + } + + @Override + public void run(String... args) throws Exception { + log.info("Travel Agency Started! "); + } + +} diff --git a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/controller/TravelAgencyController.java b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/controller/TravelAgencyController.java new file mode 100644 index 0000000000..ec84debe83 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/controller/TravelAgencyController.java @@ -0,0 +1,36 @@ +package com.baeldung.spring.cloud.kubernetes.services.travelagency.controller; + +import org.springframework.web.bind.annotation.*; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Random; +import static org.springframework.web.bind.annotation.RequestMethod.GET; +import static org.springframework.web.bind.annotation.RequestMethod.POST; + +@RestController +public class TravelAgencyController { + + private String[] deals = {"London - Paris : 25 Euro", "London - Frankfurt : 25 Euro"}; + private static final Log log = LogFactory.getLog(TravelAgencyController.class); + + + @RequestMapping(method = POST, path = "/deals/{client}") + public String deals(@PathVariable("client") String client) { + log.info("Client: " + client + " is requesting new deals!"); + int randomDeal = new Random().nextInt(deals.length); + return deals[randomDeal]; + } + + @RequestMapping(method = GET, path = "/") + @ResponseBody + public String get() throws UnknownHostException { + + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Host: ").append(InetAddress.getLocalHost().getHostName()).append("
"); + stringBuilder.append("IP: ").append(InetAddress.getLocalHost().getHostAddress()).append("
"); + stringBuilder.append("Type: ").append("Travel Agency").append("
"); + return stringBuilder.toString(); + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/resources/bootstrap.yml b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/resources/bootstrap.yml new file mode 100644 index 0000000000..d9fd0c0e76 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/resources/bootstrap.yml @@ -0,0 +1,6 @@ +spring: + application: + name: travelagency-service + cloud: + config: + uri: http://localhost:8088 \ No newline at end of file