Merge pull request #3171 from eugenp/BAEL-1174

refactor packages
This commit is contained in:
Loredana Crusoveanu 2017-12-01 16:12:51 +02:00 committed by GitHub
commit 6d639f6864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 24 deletions

Binary file not shown.

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.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<ServiceInstance> 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) {

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.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.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 {

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.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) {