refactor packages
This commit is contained in:
parent
4672451564
commit
5aa0b890d1
|
@ -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();
|
||||
}
|
||||
|
||||
public URI serviceUrl() {
|
||||
List<ServiceInstance> list = discoveryClient.getInstances("myApp");
|
||||
if (list != null && list.size() > 0) {
|
||||
return list.get(0)
|
||||
.getUri();
|
||||
@GetMapping("/ping")
|
||||
public String ping() {
|
||||
return "pong";
|
||||
}
|
||||
|
||||
return null;
|
||||
public URI serviceUrl() {
|
||||
return discoveryClient.getInstances("myApp")
|
||||
.stream()
|
||||
.findFirst()
|
||||
.map(si -> si.getUri())
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
|
@ -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;
|
|
@ -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 {
|
|
@ -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,7 +16,7 @@ public class RibbonClientApplication {
|
|||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
public RestTemplate loadbalancedRestTemplate() {
|
||||
RestTemplate getRestTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
|
@ -24,7 +25,12 @@ public class RibbonClientApplication {
|
|||
|
||||
@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) {
|
Loading…
Reference in New Issue