JAVA-10132: Align module names, folder names and artifact id
This commit is contained in:
+76
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.spring.cloud.loadbalancer.client;
|
||||
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
|
||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ClientApplication {
|
||||
public static void main(String[] args) {
|
||||
|
||||
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(ClientApplication.class)
|
||||
.web(WebApplicationType.NONE)
|
||||
.run(args);
|
||||
|
||||
WebClient loadBalancedClient = ctx.getBean(WebClient.Builder.class).build();
|
||||
|
||||
for(int i = 1; i <= 10; i++) {
|
||||
String response =
|
||||
loadBalancedClient.get().uri("http://example-service/hello")
|
||||
.retrieve().toEntity(String.class)
|
||||
.block().getBody();
|
||||
System.out.println(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
class DemoServerInstanceConfiguration {
|
||||
@Bean
|
||||
ServiceInstanceListSupplier serviceInstanceListSupplier() {
|
||||
return new DemoInstanceSupplier("example-service");
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@LoadBalancerClient(name = "example-service", configuration = DemoServerInstanceConfiguration.class)
|
||||
class WebClientConfig {
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
WebClient.Builder webClientBuilder() {
|
||||
return WebClient.builder();
|
||||
}
|
||||
}
|
||||
|
||||
class DemoInstanceSupplier implements ServiceInstanceListSupplier {
|
||||
private final String serviceId;
|
||||
|
||||
public DemoInstanceSupplier(String serviceId) {
|
||||
this.serviceId = serviceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return serviceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<List<ServiceInstance>> get() {
|
||||
return Flux.just(Arrays
|
||||
.asList(new DefaultServiceInstance(serviceId + "1", serviceId, "localhost", 8080, false),
|
||||
new DefaultServiceInstance(serviceId + "2", serviceId, "localhost", 8081, false)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user