From 5aa0b890d1fcb90bb3d0bf43bb1689ff27dee252 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Fri, 1 Dec 2017 13:34:00 +0200 Subject: [PATCH] refactor packages --- .../DiscoveryClientApplication.java | 30 +++++++++---------- .../ServiceDiscoveryApplication.java | 2 +- .../DistributedPropertiesApplication.java | 4 +-- .../{ => ribbon}/RibbonClientApplication.java | 14 ++++++--- 4 files changed, 26 insertions(+), 24 deletions(-) rename spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/{ => discovery}/DiscoveryClientApplication.java (64%) rename spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/{ => health}/ServiceDiscoveryApplication.java (94%) rename spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/{ => properties}/DistributedPropertiesApplication.java (90%) rename spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/{ => ribbon}/RibbonClientApplication.java (74%) diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/DiscoveryClientApplication.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientApplication.java similarity index 64% rename from spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/DiscoveryClientApplication.java rename to spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientApplication.java index e5e18d71ff..2dfd08b5c3 100644 --- a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/DiscoveryClientApplication.java +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/discovery/DiscoveryClientApplication.java @@ -1,16 +1,14 @@ -package com.baeldung.spring.cloud.consul; +package com.baeldung.spring.cloud.consul.discovery; import java.net.URI; -import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @@ -24,26 +22,26 @@ public class DiscoveryClientApplication { return new RestTemplate(); } - @Autowired - private RestTemplate restTemplate; - @Autowired private DiscoveryClient discoveryClient; - @RequestMapping("/discoveryClient") + @GetMapping("/discoveryClient") public String home() { - return this.restTemplate.getForEntity(serviceUrl().resolve("/ping"), String.class) + return restTemplate().getForEntity(serviceUrl().resolve("/ping"), String.class) .getBody(); } + + @GetMapping("/ping") + public String ping() { + return "pong"; + } public URI serviceUrl() { - List list = discoveryClient.getInstances("myApp"); - if (list != null && list.size() > 0) { - return list.get(0) - .getUri(); - } - - return null; + return discoveryClient.getInstances("myApp") + .stream() + .findFirst() + .map(si -> si.getUri()) + .orElse(null); } public static void main(String[] args) { diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ServiceDiscoveryApplication.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryApplication.java similarity index 94% rename from spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ServiceDiscoveryApplication.java rename to spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryApplication.java index 5da8105d01..7db361eb4f 100644 --- a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ServiceDiscoveryApplication.java +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryApplication.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.cloud.consul; +package com.baeldung.spring.cloud.consul.health; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/DistributedPropertiesApplication.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesApplication.java similarity index 90% rename from spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/DistributedPropertiesApplication.java rename to spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesApplication.java index 12ecd96b26..919bf08921 100644 --- a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/DistributedPropertiesApplication.java +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/DistributedPropertiesApplication.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.cloud.consul; +package com.baeldung.spring.cloud.consul.properties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -7,8 +7,6 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.baeldung.spring.cloud.consul.properties.MyProperties; - @SpringBootApplication @RestController public class DistributedPropertiesApplication { diff --git a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/RibbonClientApplication.java b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ribbon/RibbonClientApplication.java similarity index 74% rename from spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/RibbonClientApplication.java rename to spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ribbon/RibbonClientApplication.java index 620c3553ba..ca0d23b185 100644 --- a/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/RibbonClientApplication.java +++ b/spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ribbon/RibbonClientApplication.java @@ -1,10 +1,11 @@ -package com.baeldung.spring.cloud.consul; +package com.baeldung.spring.cloud.consul.ribbon; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; +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; @@ -15,16 +16,21 @@ public class RibbonClientApplication { @LoadBalanced @Bean - public RestTemplate loadbalancedRestTemplate() { + RestTemplate getRestTemplate() { return new RestTemplate(); } - + @Autowired RestTemplate restTemplate; @RequestMapping("/ribbonClient") public String home() { - return this.restTemplate.getForObject("http://myApp/ping", String.class); + return restTemplate.getForObject("http://myApp/ping", String.class); + } + + @GetMapping("/ping") + public String ping() { + return "pong"; } public static void main(String[] args) {