commit
6d639f6864
Binary file not shown.
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/ping")
|
||||||
|
public String ping() {
|
||||||
|
return "pong";
|
||||||
|
}
|
||||||
|
|
||||||
public URI serviceUrl() {
|
public URI serviceUrl() {
|
||||||
List<ServiceInstance> list = discoveryClient.getInstances("myApp");
|
return discoveryClient.getInstances("myApp")
|
||||||
if (list != null && list.size() > 0) {
|
.stream()
|
||||||
return list.get(0)
|
.findFirst()
|
||||||
.getUri();
|
.map(si -> si.getUri())
|
||||||
}
|
.orElse(null);
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
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.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
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.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 {
|
|
@ -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,16 +16,21 @@ public class RibbonClientApplication {
|
||||||
|
|
||||||
@LoadBalanced
|
@LoadBalanced
|
||||||
@Bean
|
@Bean
|
||||||
public RestTemplate loadbalancedRestTemplate() {
|
RestTemplate getRestTemplate() {
|
||||||
return new RestTemplate();
|
return new RestTemplate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
RestTemplate restTemplate;
|
RestTemplate restTemplate;
|
||||||
|
|
||||||
@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) {
|
Loading…
Reference in New Issue