refactor packages

This commit is contained in:
Loredana Crusoveanu 2017-12-01 13:34:00 +02:00
parent 4672451564
commit 5aa0b890d1
4 changed files with 26 additions and 24 deletions

View File

@ -1,16 +1,14 @@
package com.baeldung.spring.cloud.consul; package com.baeldung.spring.cloud.consul.discovery;
import java.net.URI; import java.net.URI;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; 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.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean; 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.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -24,26 +22,26 @@ public class DiscoveryClientApplication {
return new RestTemplate(); return new RestTemplate();
} }
@Autowired
private RestTemplate restTemplate;
@Autowired @Autowired
private DiscoveryClient discoveryClient; private DiscoveryClient discoveryClient;
@RequestMapping("/discoveryClient") @GetMapping("/discoveryClient")
public String home() { public String home() {
return this.restTemplate.getForEntity(serviceUrl().resolve("/ping"), String.class) return restTemplate().getForEntity(serviceUrl().resolve("/ping"), String.class)
.getBody(); .getBody();
} }
public URI serviceUrl() { @GetMapping("/ping")
List<ServiceInstance> list = discoveryClient.getInstances("myApp"); public String ping() {
if (list != null && list.size() > 0) { return "pong";
return list.get(0)
.getUri();
} }
return null; public URI serviceUrl() {
return discoveryClient.getInstances("myApp")
.stream()
.findFirst()
.map(si -> si.getUri())
.orElse(null);
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -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.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;

View File

@ -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.Autowired;
import org.springframework.beans.factory.annotation.Value; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.baeldung.spring.cloud.consul.properties.MyProperties;
@SpringBootApplication @SpringBootApplication
@RestController @RestController
public class DistributedPropertiesApplication { public class DistributedPropertiesApplication {

View File

@ -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.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -15,7 +16,7 @@ public class RibbonClientApplication {
@LoadBalanced @LoadBalanced
@Bean @Bean
public RestTemplate loadbalancedRestTemplate() { RestTemplate getRestTemplate() {
return new RestTemplate(); return new RestTemplate();
} }
@ -24,7 +25,12 @@ public class RibbonClientApplication {
@RequestMapping("/ribbonClient") @RequestMapping("/ribbonClient")
public String home() { 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) { public static void main(String[] args) {