JAVA-11240 Moved spring-cloud to spring-cloud-modules

This commit is contained in:
Dhawal Kapil
2022-05-29 19:32:17 +05:30
parent a7cae2e766
commit 85d134952e
934 changed files with 35 additions and 230 deletions
@@ -0,0 +1,18 @@
package com.baeldung.spring.cloud.consul.discovery;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import static org.springframework.boot.WebApplicationType.NONE;
@SpringBootApplication
@EnableDiscoveryClient
public class DiscoveryClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(DiscoveryClientApplication.class)
.run(args);
}
}
@@ -0,0 +1,50 @@
package com.baeldung.spring.cloud.consul.discovery;
import java.net.URI;
import java.util.Optional;
import javax.naming.ServiceUnavailableException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@RestController
public class DiscoveryClientController {
@Autowired
private DiscoveryClient discoveryClient;
private final RestTemplate restTemplate = new RestTemplate();
@GetMapping("/discoveryClient")
public String discoveryPing() throws RestClientException, ServiceUnavailableException {
URI service = serviceUrl().map(s -> s.resolve("/ping"))
.orElseThrow(ServiceUnavailableException::new);
return restTemplate.getForEntity(service, String.class)
.getBody();
}
@GetMapping("/ping")
public String ping() {
return "pong";
}
@GetMapping("/my-health-check")
public ResponseEntity<String> myCustomCheck() {
return new ResponseEntity<>(HttpStatus.OK);
}
public Optional<URI> serviceUrl() {
return discoveryClient.getInstances("myApp")
.stream()
.findFirst()
.map(si -> si.getUri());
}
}
@@ -0,0 +1,17 @@
package com.baeldung.spring.cloud.consul.health;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import static org.springframework.boot.WebApplicationType.NONE;
@SpringBootApplication
public class ServiceDiscoveryApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ServiceDiscoveryApplication.class).web(NONE)
.run(args);
}
}
@@ -0,0 +1,22 @@
package com.baeldung.spring.cloud.consul.health;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServiceDiscoveryController {
@GetMapping("/ping")
public String ping() {
return "pong";
}
@GetMapping("/my-health-check")
public ResponseEntity<String> myCustomCheck() {
String message = "Testing my healh check function";
return new ResponseEntity<>(message, HttpStatus.FORBIDDEN);
}
}
@@ -0,0 +1,16 @@
package com.baeldung.spring.cloud.consul.leadership;
import java.util.concurrent.ExecutionException;
import net.kinguin.leadership.consul.factory.SimpleConsulClusterFactory;
public class LeadershipElection {
public static void main(String[] args) throws ExecutionException, InterruptedException {
new SimpleConsulClusterFactory()
.mode(SimpleConsulClusterFactory.MODE_MULTI)
.debug(true)
.build()
.asObservable()
.subscribe(i -> System.out.println(i));
}
}
@@ -0,0 +1,18 @@
package com.baeldung.spring.cloud.consul.properties;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.boot.WebApplicationType.NONE;
@SpringBootApplication
@RestController
public class DistributedPropertiesApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(DistributedPropertiesApplication.class).web(NONE)
.run(args);
}
}
@@ -0,0 +1,27 @@
package com.baeldung.spring.cloud.consul.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DistributedPropertiesController {
@Value("${my.prop}")
String value;
@Autowired
private MyProperties properties;
@GetMapping("/getConfigFromValue")
public String getConfigFromValue() {
return value;
}
@GetMapping("/getConfigFromProperty")
public String getConfigFromProperty() {
return properties.getProp();
}
}
@@ -0,0 +1,22 @@
package com.baeldung.spring.cloud.consul.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
@RefreshScope
@Configuration
@ConfigurationProperties("my")
public class MyProperties {
private String prop;
public String getProp() {
return prop;
}
public void setProp(String prop) {
this.prop = prop;
}
}
@@ -0,0 +1,29 @@
spring:
application:
name: myApp
cloud:
consul:
host: localhost
port: 8500
discovery:
healthCheckPath: /my-health-check
healthCheckInterval: 20s
enabled: true
instanceId: ${spring.application.name}:${random.value}
server:
port: 0
cluster:
leader:
serviceName: cluster
serviceId: node-1
consul:
host: localhost
port: 8500
discovery:
enabled: false
session:
ttl: 15
refresh: 7
election:
envelopeTemplate: services/%s/leader
@@ -0,0 +1,9 @@
spring:
application:
name: myApp
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>