Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Mar 13, 2024
2 parents bf61054 + fa07ce6 commit f059697
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

import java.util.Map;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -37,8 +41,12 @@ class KubernetesClientLoadBalancerApplication {

private final WebClient.Builder client;

KubernetesClientLoadBalancerApplication(WebClient.Builder client) {
private final ObjectProvider<LoadBalancerClientFactory> loadBalancerClientFactory;

KubernetesClientLoadBalancerApplication(WebClient.Builder client,
ObjectProvider<LoadBalancerClientFactory> loadBalancerClientFactory) {
this.client = client;
this.loadBalancerClientFactory = loadBalancerClientFactory;
}

public static void main(String[] args) {
Expand All @@ -52,4 +60,14 @@ Map<String, Object> greeting() {
.block();
}

@GetMapping("/loadbalancer-it/supplier")
String supplier() {
ServiceInstanceListSupplier supplier = loadBalancerClientFactory.getIfAvailable()
.getProvider("service-wiremock", ServiceInstanceListSupplier.class).getIfAvailable();
if (supplier instanceof CachingServiceInstanceListSupplier cachingSupplier) {
return cachingSupplier.getDelegate().getClass().getSimpleName();
}
return supplier.getClass().getSimpleName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import reactor.netty.http.client.HttpClient;

import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.cloud.kubernetes.client.loadbalancer.KubernetesClientServicesListSupplier;
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
import org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util;
import org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
Expand Down Expand Up @@ -76,6 +78,8 @@ class LoadBalancerIT {

private static final String SERVICE_URL = "http://localhost:80/loadbalancer-it/service";

private static final String SERVICE_INSTANCE_LIST_SUPPLIER_URL = "http://localhost:80/loadbalancer-it/supplier";

private static final String SPRING_CLOUD_K8S_LOADBALANCER_APP_NAME = "spring-cloud-kubernetes-k8s-client-loadbalancer";

private static final String NAMESPACE = "default";
Expand Down Expand Up @@ -114,17 +118,17 @@ void afterEach() {
@Test
@Order(1)
void testLoadBalancerPodMode() {
testLoadBalancer();
testLoadBalancer(true);
}

@Test
@Order(2)
void testLoadBalancerServiceMode() {
patchForServiceMode();
testLoadBalancer();
testLoadBalancer(false);
}

private void testLoadBalancer() {
private void testLoadBalancer(boolean podMode) {

WebClient.Builder builder = builder();
WebClient serviceClient = builder.baseUrl(SERVICE_URL).build();
Expand All @@ -133,6 +137,18 @@ private void testLoadBalancer() {
Assertions.assertThat(BASIC_JSON_TESTER.from(result)).extractingJsonPathArrayValue("$.mappings").isEmpty();
Assertions.assertThat(BASIC_JSON_TESTER.from(result)).extractingJsonPathNumberValue("$.meta.total")
.isEqualTo(0);

WebClient.Builder supplierBuilder = builder();
WebClient supplierServiceClient = supplierBuilder.baseUrl(SERVICE_INSTANCE_LIST_SUPPLIER_URL).build();
String supplierResult = supplierServiceClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
.block();
if (podMode) {
Assertions.assertThat(supplierResult)
.isEqualTo(DiscoveryClientServiceInstanceListSupplier.class.getSimpleName());
}
else {
Assertions.assertThat(supplierResult).isEqualTo(KubernetesClientServicesListSupplier.class.getSimpleName());
}
}

private static void loadbalancerIt(Phase phase) {
Expand Down

0 comments on commit f059697

Please sign in to comment.