From 928a23494d5c3ca99d95ab2c8ed13978f01f3da1 Mon Sep 17 00:00:00 2001 From: caroline Date: Sat, 23 Feb 2019 22:49:32 +0100 Subject: [PATCH 1/8] 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 From e7bbe5b8adb3af4436b2fb2777c0819e58161f40 Mon Sep 17 00:00:00 2001 From: caroline Date: Sun, 10 Mar 2019 22:24:59 +0100 Subject: [PATCH 2/8] Code changes due to BAEL-2460. Could not put them in spring-cloud-kubernetes because had to use different versions of artifacts. --- spring-cloud/pom.xml | 1 + .../client-service/.gitignore | 24 +++++ .../client-service/Dockerfile | 5 + .../client-service/client-config.yaml | 8 ++ .../client-service-deployment.yaml | 33 ++++++ .../client-service/pom.xml | 96 +++++++++++++++++ .../cloud/kubernetes/client/Application.java | 28 +++++ .../cloud/kubernetes/client/ClientConfig.java | 19 ++++ .../kubernetes/client/ClientController.java | 57 ++++++++++ .../client/RibbonConfiguration.java | 55 ++++++++++ .../client/TravelAgencyService.java | 27 +++++ .../src/main/resources/application.yaml | 16 +++ .../src/main/resources/logback.xml | 13 +++ .../SpringContextIntegrationTest.java | 18 ++++ .../deployment-all.sh | 34 ++++++ .../mongo-deployment.yaml | 45 ++++++++ .../spring-cloud-kubernetes-2/pom.xml | 32 ++++++ .../spring-cloud-kubernetes-2/secret.yaml | 7 ++ .../travel-agency-service/Dockerfile | 5 + .../travel-agency-service/pom.xml | 76 +++++++++++++ .../kubernetes/travelagency/Application.java | 23 ++++ .../controller/TravelAgencyController.java | 46 ++++++++ .../travelagency/model/TravelDeal.java | 100 ++++++++++++++++++ .../repository/TravelDealRepository.java | 13 +++ .../src/main/resources/application.properties | 14 +++ .../src/main/resources/logback-spring.xml | 17 +++ .../src/main/resources/logback.xml | 13 +++ .../travel-agency-config.yaml | 8 ++ .../travel-agency-deployment.yaml | 44 ++++++++ 29 files changed, 877 insertions(+) create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/.gitignore create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/Dockerfile create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/client-config.yaml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/client-service-deployment.yaml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/Application.java create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientConfig.java create mode 100755 spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientController.java create mode 100755 spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/RibbonConfiguration.java create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/resources/application.yaml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/resources/logback.xml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/client-service/src/test/java/org/baeldung/SpringContextIntegrationTest.java create mode 100755 spring-cloud/spring-cloud-kubernetes-2/deployment-all.sh create mode 100644 spring-cloud/spring-cloud-kubernetes-2/mongo-deployment.yaml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/pom.xml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/secret.yaml create mode 100755 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/Dockerfile create mode 100755 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/pom.xml create mode 100755 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/Application.java create mode 100644 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/controller/TravelAgencyController.java create mode 100644 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/model/TravelDeal.java create mode 100644 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/repository/TravelDealRepository.java create mode 100644 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/application.properties create mode 100644 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/logback-spring.xml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/logback.xml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/travel-agency-config.yaml create mode 100644 spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/travel-agency-deployment.yaml diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index baf86a4386..bf70e0284c 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -32,6 +32,7 @@ spring-cloud-zuul-eureka-integration spring-cloud-contract spring-cloud-kubernetes + spring-cloud-kubernetes-2 spring-cloud-archaius spring-cloud-functions spring-cloud-vault diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/.gitignore b/spring-cloud/spring-cloud-kubernetes-2/client-service/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/Dockerfile b/spring-cloud/spring-cloud-kubernetes-2/client-service/Dockerfile new file mode 100644 index 0000000000..bd8cf2918f --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/Dockerfile @@ -0,0 +1,5 @@ +FROM openjdk:8-jdk-alpine +VOLUME /tmp +COPY target/client-service-1.0-SNAPSHOT.jar app.jar +ENV JAVA_OPTS="" +ENTRYPOINT exec java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9999 -jar /app.jar \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/client-config.yaml b/spring-cloud/spring-cloud-kubernetes-2/client-service/client-config.yaml new file mode 100644 index 0000000000..329c01582e --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/client-config.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: client-service +data: + application.properties: |- + bean.message=Testing reload ! Message from backend is: %s
Services : %s + diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/client-service-deployment.yaml b/spring-cloud/spring-cloud-kubernetes-2/client-service/client-service-deployment.yaml new file mode 100644 index 0000000000..8867aecc33 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/client-service-deployment.yaml @@ -0,0 +1,33 @@ +kind: Service +apiVersion: v1 +metadata: + name: client-service +spec: + selector: + app: client-service + ports: + - protocol: TCP + port: 8080 + nodePort: 30083 + type: NodePort +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: client-service +spec: + selector: + matchLabels: + app: client-service + replicas: 1 + template: + metadata: + labels: + app: client-service + spec: + containers: + - name: client-service + image: client-service:latest + imagePullPolicy: Never + ports: + - containerPort: 8080 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml b/spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml new file mode 100644 index 0000000000..522013849b --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml @@ -0,0 +1,96 @@ + + + 4.0.0 + client-service + client-service + 1.0-SNAPSHOT + + + com.baeldung.spring.cloud + spring-cloud-kubernetes-2 + 1.0-SNAPSHOT + + + + 1.8 + Finchley.SR2 + 1.0.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.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix + + + org.springframework.cloud + spring-cloud-starter-kubernetes-ribbon + + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.spring.cloud.kubernetes.client.Application + JAR + + + + + repackage + + + + + + + + diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/Application.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/Application.java new file mode 100644 index 0000000000..de1f1ec878 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/Application.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.cloud.kubernetes.client; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.netflix.ribbon.RibbonClient; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + + +@SpringBootApplication +@EnableDiscoveryClient +@EnableCircuitBreaker +@RibbonClient(name = "travel-agency-service", configuration = RibbonConfiguration.class) +public class Application { + + @LoadBalanced + @Bean + RestTemplate restTemplate(){ + return new RestTemplate(); + } + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientConfig.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientConfig.java new file mode 100644 index 0000000000..1158f3c2d2 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientConfig.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.cloud.kubernetes.client; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "bean") +public class ClientConfig { + + private String message = "Message from backend is: %s
Services : %s"; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientController.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientController.java new file mode 100755 index 0000000000..44f412c31d --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientController.java @@ -0,0 +1,57 @@ +package com.baeldung.spring.cloud.kubernetes.client; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.http.ResponseEntity; +import org.springframework.util.CollectionUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +import java.net.UnknownHostException; +import java.util.List; + +@RestController +public class ClientController { + + @Autowired + private DiscoveryClient discoveryClient; + + @Autowired + private ClientConfig config; + + @Autowired + private TravelAgencyService travelAgencyService; + + @RequestMapping("/deals") + public String getDeals() { + return travelAgencyService.getDeals(); + } + + @GetMapping + public String load() throws UnknownHostException { + + RestTemplate restTemplate = new RestTemplate(); + String resourceUrl + = "http://travel-agency-service:8080"; + ResponseEntity response + = restTemplate.getForEntity(resourceUrl, String.class); + + String serviceList = ""; + if (discoveryClient != null) { + List services = this.discoveryClient.getServices(); + + for (String service : services) { + + List instances = this.discoveryClient + .getInstances(service); + + serviceList += ("[" + service + " : " + ((!CollectionUtils.isEmpty(instances))?instances.size():0)+ " instances ]"); + } + } + + return String.format(config.getMessage(), response.getBody(),serviceList); + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/RibbonConfiguration.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/RibbonConfiguration.java new file mode 100755 index 0000000000..a372e9357f --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/RibbonConfiguration.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2016 to the original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.baeldung.spring.cloud.kubernetes.client; + +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.AvailabilityFilteringRule; +import com.netflix.loadbalancer.IPing; +import com.netflix.loadbalancer.IRule; +import com.netflix.loadbalancer.PingUrl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; + +public class RibbonConfiguration { + + @Autowired + IClientConfig ribbonClientConfig; + + /** + * PingUrl will ping a URL to check the status of each server. + * Say Hello has, as you’ll recall, a method mapped to the /path; that means that Ribbon will get an HTTP 200 response when it pings a running Backend Server + * + * @param config Client configuration + * @return The URL to be used for the Ping + */ + @Bean + public IPing ribbonPing(IClientConfig config) { + return new PingUrl(); + } + + /** + * AvailabilityFilteringRule will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state: + * if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally. + * + * @param config Client configuration + * @return The Load Balancer rule + */ + @Bean + public IRule ribbonRule(IClientConfig config) { + return new AvailabilityFilteringRule(); + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java new file mode 100644 index 0000000000..792699696e --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.cloud.kubernetes.client; + +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; +import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Service +public class TravelAgencyService { + + private final RestTemplate restTemplate; + + public TravelAgencyService(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + @HystrixCommand(fallbackMethod = "getFallbackName", commandProperties = { + @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") + }) + public String getDeals() { + return this.restTemplate.getForObject("http://travel-agency-service:8080/deals", String.class); + } + + private String getFallbackName() { + return "Fallback"; + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/resources/application.yaml b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/resources/application.yaml new file mode 100644 index 0000000000..f966fdd7a5 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/resources/application.yaml @@ -0,0 +1,16 @@ +spring: + application.name: client-service + cloud.kubernetes.reload.enabled: true +server.port: 8080 +management: + endpoint: + restart: + enabled: true + health: + enabled: true + info: + enabled: true +ribbon: + http: + client: + enabled: true \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/resources/logback.xml b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/test/java/org/baeldung/SpringContextIntegrationTest.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/test/java/org/baeldung/SpringContextIntegrationTest.java new file mode 100644 index 0000000000..9dc2e4f714 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/test/java/org/baeldung/SpringContextIntegrationTest.java @@ -0,0 +1,18 @@ +package org.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.spring.cloud.kubernetes.client.Application; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +public class SpringContextIntegrationTest { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-cloud/spring-cloud-kubernetes-2/deployment-all.sh b/spring-cloud/spring-cloud-kubernetes-2/deployment-all.sh new file mode 100755 index 0000000000..9c088b7422 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/deployment-all.sh @@ -0,0 +1,34 @@ +### build the repository +mvn clean install + +### set docker env +eval $(minikube docker-env) + +### build the docker images on minikube +cd travel-agency-service +docker build -t travel-agency-service . +cd ../client-service +docker build -t client-service . +cd .. + +### secret and mongodb +kubectl delete -f secret.yaml +kubectl delete -f mongo-deployment.yaml + +kubectl create -f secret.yaml +kubectl create -f mongo-deployment.yaml + +### travel-agency-service +kubectl delete -f travel-agency-service/travel-agency-deployment.yaml +kubectl create -f travel-agency-service/travel-agency-deployment.yaml + + +### client-service +kubectl delete configmap client-service +kubectl delete -f client-service/client-service-deployment.yaml + +kubectl create -f client-service/client-config.yaml +kubectl create -f client-service/client-service-deployment.yaml + +# Check that the pods are running +kubectl get pods \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/mongo-deployment.yaml b/spring-cloud/spring-cloud-kubernetes-2/mongo-deployment.yaml new file mode 100644 index 0000000000..3d40581578 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/mongo-deployment.yaml @@ -0,0 +1,45 @@ +apiVersion: v1 +kind: Service +metadata: + labels: + app: mongo + name: mongodb-service +spec: + type: NodePort + ports: + - name: "http" + port: 27017 + protocol: TCP + targetPort: 27017 + selector: + service: mongo +--- +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: mongo +spec: + replicas: 1 + template: + metadata: + labels: + service: mongo + name: mongodb-service + spec: + containers: + - args: + - mongod + - --smallfiles + image: mongo:latest + name: mongo + env: + - name: MONGO_INITDB_ROOT_USERNAME + valueFrom: + secretKeyRef: + name: db-secret + key: username + - name: MONGO_INITDB_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: db-secret + key: password diff --git a/spring-cloud/spring-cloud-kubernetes-2/pom.xml b/spring-cloud/spring-cloud-kubernetes-2/pom.xml new file mode 100644 index 0000000000..1ce18a7498 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + com.baeldung.spring.cloud + spring-cloud-kubernetes-2 + 1.0-SNAPSHOT + spring-cloud-kubernetes-2 + pom + + + 2.0.6.RELEASE + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + client-service + travel-agency-service + + + diff --git a/spring-cloud/spring-cloud-kubernetes-2/secret.yaml b/spring-cloud/spring-cloud-kubernetes-2/secret.yaml new file mode 100644 index 0000000000..a813c35be7 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/secret.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: Secret +metadata: + name: db-secret +data: + username: dXNlcg== + password: cDQ1NXcwcmQ= diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/Dockerfile b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/Dockerfile new file mode 100755 index 0000000000..30b66d5eff --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/Dockerfile @@ -0,0 +1,5 @@ +FROM openjdk:8-jdk-alpine +VOLUME /tmp +COPY target/travel-agency-service-1.0-SNAPSHOT.jar app.jar +ENV JAVA_OPTS="" +ENTRYPOINT exec java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9999 -jar /app.jar \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/pom.xml b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/pom.xml new file mode 100755 index 0000000000..a375264533 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + travel-agency-service + 1.0-SNAPSHOT + + + com.baeldung.spring.cloud + spring-cloud-kubernetes-2 + 1.0-SNAPSHOT + + + + 1.8 + Finchley.SR2 + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud-dependencies.version} + pom + import + + + ch.qos.logback + logback-classic + 1.2.3 + + + + + + + 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-starter-data-mongodb + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.spring.cloud.kubernetes.travelagency.Application + JAR + + + + + repackage + + + + + + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/Application.java b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/Application.java new file mode 100755 index 0000000000..f84303a5c8 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/Application.java @@ -0,0 +1,23 @@ +package com.baeldung.spring.cloud.kubernetes.travelagency; + +import com.baeldung.spring.cloud.kubernetes.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 Application implements CommandLineRunner { + + private static final Log log = LogFactory.getLog(TravelAgencyController.class); + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + public void run(String... args) throws Exception { + log.info("Travel Agency Started! "); + } + +} diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/controller/TravelAgencyController.java b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/controller/TravelAgencyController.java new file mode 100644 index 0000000000..55a06dee77 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/controller/TravelAgencyController.java @@ -0,0 +1,46 @@ +package com.baeldung.spring.cloud.kubernetes.travelagency.controller; + +import com.baeldung.spring.cloud.kubernetes.travelagency.model.TravelDeal; +import com.baeldung.spring.cloud.kubernetes.travelagency.repository.TravelDealRepository; +import org.springframework.beans.factory.annotation.Autowired; +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.List; +import java.util.Random; +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +@RestController +public class TravelAgencyController { + + @Autowired + private TravelDealRepository travelDealRepository; + + private static final Log log = LogFactory.getLog(TravelAgencyController.class); + + @RequestMapping(method = GET, path = "/deals") + public String deals() { + log.info("Client is requesting new deals!"); + + List travelDealList = travelDealRepository.findAll(); + if (!travelDealList.isEmpty()) { + int randomDeal = new Random().nextInt(travelDealList.size()); + return travelDealList.get(randomDeal).toString(); + } else { + return "NO DEALS"; + } + } + + @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-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/model/TravelDeal.java b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/model/TravelDeal.java new file mode 100644 index 0000000000..550b2ca33c --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/model/TravelDeal.java @@ -0,0 +1,100 @@ +package com.baeldung.spring.cloud.kubernetes.travelagency.model; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.Field; + +import java.math.BigInteger; +import java.util.Date; + +@Document(collection = "travel_deal") +public class TravelDeal { + + @Id + private BigInteger id; + + private String destination; + + private String description; + + @Field("deal_price") + private double dealPrice; + + @Field("old_price") + private double oldPrice; + + @Field("departure_date") + private Date departureDate; + + @Field("arrival_date") + private Date arrivalDate; + + public BigInteger getId() { + return id; + } + + public void setId(BigInteger id) { + this.id = id; + } + + public String getDestination() { + return destination; + } + + public void setDestination(String destination) { + this.destination = destination; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public double getDealPrice() { + return dealPrice; + } + + public void setDealPrice(double dealPrice) { + this.dealPrice = dealPrice; + } + + public double getOldPrice() { + return oldPrice; + } + + public void setOldPrice(double oldPrice) { + this.oldPrice = oldPrice; + } + + public Date getDepartureDate() { + return departureDate; + } + + public void setDepartureDate(Date departureDate) { + this.departureDate = departureDate; + } + + public Date getArrivalDate() { + return arrivalDate; + } + + public void setArrivalDate(Date arrivalDate) { + this.arrivalDate = arrivalDate; + } + + @Override + public String toString() { + return "TravelDeal{" + + "id=" + id + + ", destination='" + destination + '\'' + + ", description='" + description + '\'' + + ", dealPrice=" + dealPrice + + ", oldPrice=" + oldPrice + + ", departureDate=" + departureDate + + ", arrivalDate=" + arrivalDate + + '}'; + } +} diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/repository/TravelDealRepository.java b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/repository/TravelDealRepository.java new file mode 100644 index 0000000000..f7dc5a843e --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/repository/TravelDealRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.spring.cloud.kubernetes.travelagency.repository; + +import java.util.List; + +import com.baeldung.spring.cloud.kubernetes.travelagency.model.TravelDeal; +import org.springframework.data.mongodb.repository.MongoRepository; + +public interface TravelDealRepository extends MongoRepository { + + public List findByDestination(String destination); + + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/application.properties b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/application.properties new file mode 100644 index 0000000000..ffe78f0917 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/application.properties @@ -0,0 +1,14 @@ +spring.application.name=travel-agency-service +server.port=8080 +spring.cloud.kubernetes.reload.enabled=true +spring.cloud.kubernetes.secrets.name=db-secret +spring.data.mongodb.host=mongodb-service +spring.data.mongodb.port=27017 +spring.data.mongodb.database=admin +spring.data.mongodb.username=${MONGO_USERNAME} +spring.data.mongodb.password=${MONGO_PASSWORD} +management.endpoint.health.enabled=true +management.endpoint.info.enabled=true +management.endpoint.restart.enabled=true +com.baeldung.spring.cloud.kubernetes.services=debug + diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/logback-spring.xml b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/logback-spring.xml new file mode 100644 index 0000000000..49aeda8f9f --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/logback-spring.xml @@ -0,0 +1,17 @@ + + + + + + logstash:5000 + + + + + + + + + + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/logback.xml b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/logback.xml new file mode 100644 index 0000000000..37492be5bf --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/travel-agency-config.yaml b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/travel-agency-config.yaml new file mode 100644 index 0000000000..93a67e3777 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/travel-agency-config.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Secret +metadata: + name: travel-agency-service +data: + application.properties: |- + bean.message=Testing reload ! Message from backend is: %s
Services : %s + diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/travel-agency-deployment.yaml b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/travel-agency-deployment.yaml new file mode 100644 index 0000000000..a41f13bf61 --- /dev/null +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/travel-agency-deployment.yaml @@ -0,0 +1,44 @@ +kind: Service +apiVersion: v1 +metadata: + name: travel-agency-service +spec: + selector: + app: travel-agency-service + ports: + - protocol: TCP + port: 8080 + nodePort: 30081 + type: NodePort +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: travel-agency-service +spec: + selector: + matchLabels: + app: travel-agency-service + replicas: 2 + template: + metadata: + labels: + app: travel-agency-service + spec: + containers: + - name: travel-agency-service + image: travel-agency-service:latest + imagePullPolicy: Never + ports: + - containerPort: 8080 + env: + - name: MONGO_USERNAME + valueFrom: + secretKeyRef: + name: db-secret + key: username + - name: MONGO_PASSWORD + valueFrom: + secretKeyRef: + name: db-secret + key: password \ No newline at end of file From 12b3bc1383ae32df25b43587045b0cfd1e1994a5 Mon Sep 17 00:00:00 2001 From: cscib Date: Sun, 10 Mar 2019 22:31:20 +0100 Subject: [PATCH 3/8] Delete Dockerfile --- .../client-service/Dockerfile | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/Dockerfile diff --git a/spring-cloud/spring-cloud-kubernetes-project/client-service/Dockerfile b/spring-cloud/spring-cloud-kubernetes-project/client-service/Dockerfile deleted file mode 100644 index 93e75216db..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/client-service/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -# 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 From ea3fe7aab95eb068e3255cc6da501c25e37c85b5 Mon Sep 17 00:00:00 2001 From: caroline Date: Sun, 10 Mar 2019 22:34:17 +0100 Subject: [PATCH 4/8] Removing the code of an old version of the code with deleted maven module spring-cloud-kubernetes-project --- .../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 -- 16 files changed, 582 deletions(-) delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/pom.xml delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/ClientApplication.java delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/config/ClientConfig.java delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/controller/ClientController.java delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/service/TravelAgencyClientService.java delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/resources/bootstrap.yml delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/gateway-service/Dockerfile delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/gateway-service/pom.xml delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/gateway/GatewayApplication.java delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/resources/bootstrap.yml delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/pom.xml delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/Dockerfile delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/pom.xml delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/TravelAgencyApplication.java delete mode 100644 spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/controller/TravelAgencyController.java delete 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/pom.xml b/spring-cloud/spring-cloud-kubernetes-project/client-service/pom.xml deleted file mode 100644 index 6bba5512cd..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/client-service/pom.xml +++ /dev/null @@ -1,71 +0,0 @@ - - 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 deleted file mode 100644 index 9044de9574..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/ClientApplication.java +++ /dev/null @@ -1,96 +0,0 @@ -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 deleted file mode 100644 index a4f43e2ddc..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/config/ClientConfig.java +++ /dev/null @@ -1,19 +0,0 @@ -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 deleted file mode 100644 index dad736953e..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/controller/ClientController.java +++ /dev/null @@ -1,43 +0,0 @@ -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 deleted file mode 100644 index ba86808cd5..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/client/service/TravelAgencyClientService.java +++ /dev/null @@ -1,45 +0,0 @@ -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 deleted file mode 100644 index 736a17b041..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/client-service/src/main/resources/bootstrap.yml +++ /dev/null @@ -1,6 +0,0 @@ -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 deleted file mode 100644 index 93e75216db..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/gateway-service/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -# 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 deleted file mode 100644 index 84eb353fb9..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/gateway-service/pom.xml +++ /dev/null @@ -1,76 +0,0 @@ - - 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 deleted file mode 100644 index 7f11906aba..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/gateway/GatewayApplication.java +++ /dev/null @@ -1,44 +0,0 @@ -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 deleted file mode 100644 index 5fd607d335..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/gateway-service/src/main/resources/bootstrap.yml +++ /dev/null @@ -1,6 +0,0 @@ -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 deleted file mode 100644 index dc4e3bfe70..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/pom.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - 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 deleted file mode 100644 index 93e75216db..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -# 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 deleted file mode 100644 index 69cd22ae52..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/pom.xml +++ /dev/null @@ -1,59 +0,0 @@ - - 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 deleted file mode 100644 index c96b547e18..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/TravelAgencyApplication.java +++ /dev/null @@ -1,24 +0,0 @@ -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 deleted file mode 100644 index ec84debe83..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/services/travelagency/controller/TravelAgencyController.java +++ /dev/null @@ -1,36 +0,0 @@ -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 deleted file mode 100644 index d9fd0c0e76..0000000000 --- a/spring-cloud/spring-cloud-kubernetes-project/travel-agency-service/src/main/resources/bootstrap.yml +++ /dev/null @@ -1,6 +0,0 @@ -spring: - application: - name: travelagency-service - cloud: - config: - uri: http://localhost:8088 \ No newline at end of file From 4fece2266ed1f4f60aaee822111d2c2aaec68a4e Mon Sep 17 00:00:00 2001 From: caroline Date: Mon, 18 Mar 2019 14:35:22 +0100 Subject: [PATCH 5/8] Formatting changes --- .../cloud/kubernetes/client/Application.java | 17 ++- .../cloud/kubernetes/client/ClientConfig.java | 14 +- .../kubernetes/client/ClientController.java | 53 ++++---- .../client/RibbonConfiguration.java | 48 +++---- .../client/TravelAgencyService.java | 25 ++-- .../SpringContextIntegrationTest.java | 7 +- .../kubernetes/travelagency/Application.java | 2 +- .../controller/TravelAgencyController.java | 17 ++- .../travelagency/model/TravelDeal.java | 124 ++++++++---------- .../repository/TravelDealRepository.java | 1 - 10 files changed, 151 insertions(+), 157 deletions(-) diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/Application.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/Application.java index de1f1ec878..24e562c79b 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/Application.java +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/Application.java @@ -9,20 +9,19 @@ import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; - @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker @RibbonClient(name = "travel-agency-service", configuration = RibbonConfiguration.class) public class Application { - @LoadBalanced - @Bean - RestTemplate restTemplate(){ - return new RestTemplate(); - } + @LoadBalanced + @Bean + RestTemplate restTemplate() { + return new RestTemplate(); + } - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } } diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientConfig.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientConfig.java index 1158f3c2d2..4bc8fbe327 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientConfig.java +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientConfig.java @@ -7,13 +7,13 @@ import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "bean") public class ClientConfig { - private String message = "Message from backend is: %s
Services : %s"; + private String message = "Message from backend is: %s
Services : %s"; - public String getMessage() { - return message; - } + public String getMessage() { + return message; + } - public void setMessage(String message) { - this.message = message; - } + public void setMessage(String message) { + this.message = message; + } } \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientController.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientController.java index 44f412c31d..952bccc3a8 100755 --- a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientController.java +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/ClientController.java @@ -16,42 +16,39 @@ import java.util.List; @RestController public class ClientController { - @Autowired - private DiscoveryClient discoveryClient; + @Autowired + private DiscoveryClient discoveryClient; - @Autowired - private ClientConfig config; + @Autowired + private ClientConfig config; - @Autowired - private TravelAgencyService travelAgencyService; + @Autowired + private TravelAgencyService travelAgencyService; - @RequestMapping("/deals") - public String getDeals() { - return travelAgencyService.getDeals(); - } + @RequestMapping("/deals") + public String getDeals() { + return travelAgencyService.getDeals(); + } - @GetMapping - public String load() throws UnknownHostException { + @GetMapping + public String load() { - RestTemplate restTemplate = new RestTemplate(); - String resourceUrl - = "http://travel-agency-service:8080"; - ResponseEntity response - = restTemplate.getForEntity(resourceUrl, String.class); + RestTemplate restTemplate = new RestTemplate(); + String resourceUrl = "http://travel-agency-service:8080"; + ResponseEntity response = restTemplate.getForEntity(resourceUrl, String.class); - String serviceList = ""; - if (discoveryClient != null) { - List services = this.discoveryClient.getServices(); + String serviceList = ""; + if (discoveryClient != null) { + List services = this.discoveryClient.getServices(); - for (String service : services) { + for (String service : services) { - List instances = this.discoveryClient - .getInstances(service); + List instances = this.discoveryClient.getInstances(service); - serviceList += ("[" + service + " : " + ((!CollectionUtils.isEmpty(instances))?instances.size():0)+ " instances ]"); - } - } + serviceList += ("[" + service + " : " + ((!CollectionUtils.isEmpty(instances)) ? instances.size() : 0) + " instances ]"); + } + } - return String.format(config.getMessage(), response.getBody(),serviceList); - } + return String.format(config.getMessage(), response.getBody(), serviceList); + } } diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/RibbonConfiguration.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/RibbonConfiguration.java index a372e9357f..2b81c78be4 100755 --- a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/RibbonConfiguration.java +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/RibbonConfiguration.java @@ -26,30 +26,30 @@ import org.springframework.context.annotation.Bean; public class RibbonConfiguration { - @Autowired - IClientConfig ribbonClientConfig; + @Autowired + IClientConfig ribbonClientConfig; - /** - * PingUrl will ping a URL to check the status of each server. - * Say Hello has, as you’ll recall, a method mapped to the /path; that means that Ribbon will get an HTTP 200 response when it pings a running Backend Server - * - * @param config Client configuration - * @return The URL to be used for the Ping - */ - @Bean - public IPing ribbonPing(IClientConfig config) { - return new PingUrl(); - } + /** + * PingUrl will ping a URL to check the status of each server. + * Say Hello has, as you’ll recall, a method mapped to the /path; that means that Ribbon will get an HTTP 200 response when it pings a running Backend Server + * + * @param config Client configuration + * @return The URL to be used for the Ping + */ + @Bean + public IPing ribbonPing(IClientConfig config) { + return new PingUrl(); + } - /** - * AvailabilityFilteringRule will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state: - * if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally. - * - * @param config Client configuration - * @return The Load Balancer rule - */ - @Bean - public IRule ribbonRule(IClientConfig config) { - return new AvailabilityFilteringRule(); - } + /** + * AvailabilityFilteringRule will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state: + * if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally. + * + * @param config Client configuration + * @return The Load Balancer rule + */ + @Bean + public IRule ribbonRule(IClientConfig config) { + return new AvailabilityFilteringRule(); + } } diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java index 792699696e..9e59be4805 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java @@ -8,20 +8,19 @@ import org.springframework.web.client.RestTemplate; @Service public class TravelAgencyService { - private final RestTemplate restTemplate; + private final RestTemplate restTemplate; - public TravelAgencyService(RestTemplate restTemplate) { - this.restTemplate = restTemplate; - } + public TravelAgencyService(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } - @HystrixCommand(fallbackMethod = "getFallbackName", commandProperties = { - @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") - }) - public String getDeals() { - return this.restTemplate.getForObject("http://travel-agency-service:8080/deals", String.class); - } + @HystrixCommand(fallbackMethod = "getFallbackName", commandProperties = + { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") }) + public String getDeals() { + return this.restTemplate.getForObject("http://travel-agency-service:8080/deals", String.class); + } - private String getFallbackName() { - return "Fallback"; - } + private String getFallbackName() { + return "Fallback"; + } } diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/test/java/org/baeldung/SpringContextIntegrationTest.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/test/java/org/baeldung/SpringContextIntegrationTest.java index 9dc2e4f714..a6a978a354 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/test/java/org/baeldung/SpringContextIntegrationTest.java +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/test/java/org/baeldung/SpringContextIntegrationTest.java @@ -11,8 +11,7 @@ import com.baeldung.spring.cloud.kubernetes.client.Application; @SpringBootTest(classes = Application.class) public class SpringContextIntegrationTest { - @Test - public void contextLoads() { - } - + @Test + public void contextLoads() { + } } diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/Application.java b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/Application.java index f84303a5c8..bfc7356176 100755 --- a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/Application.java +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/Application.java @@ -8,7 +8,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class Application implements CommandLineRunner { +public class Application implements CommandLineRunner { private static final Log log = LogFactory.getLog(TravelAgencyController.class); diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/controller/TravelAgencyController.java b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/controller/TravelAgencyController.java index 55a06dee77..42f001616a 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/controller/TravelAgencyController.java +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/controller/TravelAgencyController.java @@ -27,7 +27,8 @@ public class TravelAgencyController { List travelDealList = travelDealRepository.findAll(); if (!travelDealList.isEmpty()) { int randomDeal = new Random().nextInt(travelDealList.size()); - return travelDealList.get(randomDeal).toString(); + return travelDealList.get(randomDeal) + .toString(); } else { return "NO DEALS"; } @@ -38,9 +39,17 @@ public class TravelAgencyController { 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("
"); + 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-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/model/TravelDeal.java b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/model/TravelDeal.java index 550b2ca33c..2cd2bf2d45 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/model/TravelDeal.java +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/model/TravelDeal.java @@ -10,91 +10,83 @@ import java.util.Date; @Document(collection = "travel_deal") public class TravelDeal { - @Id - private BigInteger id; + @Id + private BigInteger id; - private String destination; + private String destination; - private String description; + private String description; - @Field("deal_price") - private double dealPrice; + @Field("deal_price") + private double dealPrice; - @Field("old_price") - private double oldPrice; + @Field("old_price") + private double oldPrice; - @Field("departure_date") - private Date departureDate; + @Field("departure_date") + private Date departureDate; - @Field("arrival_date") - private Date arrivalDate; + @Field("arrival_date") + private Date arrivalDate; - public BigInteger getId() { - return id; - } + public BigInteger getId() { + return id; + } - public void setId(BigInteger id) { - this.id = id; - } + public void setId(BigInteger id) { + this.id = id; + } - public String getDestination() { - return destination; - } + public String getDestination() { + return destination; + } - public void setDestination(String destination) { - this.destination = destination; - } + public void setDestination(String destination) { + this.destination = destination; + } - public String getDescription() { - return description; - } + public String getDescription() { + return description; + } - public void setDescription(String description) { - this.description = description; - } + public void setDescription(String description) { + this.description = description; + } - public double getDealPrice() { - return dealPrice; - } + public double getDealPrice() { + return dealPrice; + } - public void setDealPrice(double dealPrice) { - this.dealPrice = dealPrice; - } + public void setDealPrice(double dealPrice) { + this.dealPrice = dealPrice; + } - public double getOldPrice() { - return oldPrice; - } + public double getOldPrice() { + return oldPrice; + } - public void setOldPrice(double oldPrice) { - this.oldPrice = oldPrice; - } + public void setOldPrice(double oldPrice) { + this.oldPrice = oldPrice; + } - public Date getDepartureDate() { - return departureDate; - } + public Date getDepartureDate() { + return departureDate; + } - public void setDepartureDate(Date departureDate) { - this.departureDate = departureDate; - } + public void setDepartureDate(Date departureDate) { + this.departureDate = departureDate; + } - public Date getArrivalDate() { - return arrivalDate; - } + public Date getArrivalDate() { + return arrivalDate; + } - public void setArrivalDate(Date arrivalDate) { - this.arrivalDate = arrivalDate; - } + public void setArrivalDate(Date arrivalDate) { + this.arrivalDate = arrivalDate; + } - @Override - public String toString() { - return "TravelDeal{" + - "id=" + id + - ", destination='" + destination + '\'' + - ", description='" + description + '\'' + - ", dealPrice=" + dealPrice + - ", oldPrice=" + oldPrice + - ", departureDate=" + departureDate + - ", arrivalDate=" + arrivalDate + - '}'; - } + @Override + public String toString() { + return "TravelDeal{" + "id=" + id + ", destination='" + destination + '\'' + ", description='" + description + '\'' + ", dealPrice=" + dealPrice + ", oldPrice=" + oldPrice + ", departureDate=" + departureDate + ", arrivalDate=" + arrivalDate + '}'; + } } diff --git a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/repository/TravelDealRepository.java b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/repository/TravelDealRepository.java index f7dc5a843e..20c1e3b67a 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/repository/TravelDealRepository.java +++ b/spring-cloud/spring-cloud-kubernetes-2/travel-agency-service/src/main/java/com/baeldung/spring/cloud/kubernetes/travelagency/repository/TravelDealRepository.java @@ -9,5 +9,4 @@ public interface TravelDealRepository extends MongoRepository findByDestination(String destination); - } \ No newline at end of file From f914b19bdbcfa5e4f2e3ddbd4e3bdefab7d94ec7 Mon Sep 17 00:00:00 2001 From: caroline Date: Mon, 18 Mar 2019 14:57:31 +0100 Subject: [PATCH 6/8] Formatting changes --- .../spring/cloud/kubernetes/client/TravelAgencyService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java index 9e59be4805..5ce6d7b8a1 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/src/main/java/com/baeldung/spring/cloud/kubernetes/client/TravelAgencyService.java @@ -14,8 +14,8 @@ public class TravelAgencyService { this.restTemplate = restTemplate; } - @HystrixCommand(fallbackMethod = "getFallbackName", commandProperties = - { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") }) + @HystrixCommand(fallbackMethod = "getFallbackName", commandProperties = { + @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") }) public String getDeals() { return this.restTemplate.getForObject("http://travel-agency-service:8080/deals", String.class); } From 96b8a69f726dadae84c57700048d1dc57fa4138d Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Sat, 23 Mar 2019 13:36:10 +0100 Subject: [PATCH 7/8] Update spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml Co-Authored-By: cscib --- spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml b/spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml index 522013849b..908a545242 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml +++ b/spring-cloud/spring-cloud-kubernetes-2/client-service/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 client-service - client-service + client-service 1.0-SNAPSHOT From 6f26499f3e1a9b27ee4e62c66fc252f18cb44f11 Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Sat, 23 Mar 2019 13:36:41 +0100 Subject: [PATCH 8/8] Update spring-cloud/spring-cloud-kubernetes-2/pom.xml Co-Authored-By: cscib --- spring-cloud/spring-cloud-kubernetes-2/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud/spring-cloud-kubernetes-2/pom.xml b/spring-cloud/spring-cloud-kubernetes-2/pom.xml index 1ce18a7498..d501e8102f 100644 --- a/spring-cloud/spring-cloud-kubernetes-2/pom.xml +++ b/spring-cloud/spring-cloud-kubernetes-2/pom.xml @@ -5,7 +5,7 @@ com.baeldung.spring.cloud spring-cloud-kubernetes-2 1.0-SNAPSHOT - spring-cloud-kubernetes-2 + spring-cloud-kubernetes-2 pom