Skip to content

Commit ad2c573

Browse files
committed
tests spring cloud netfilx ribbon
1 parent 3051ca8 commit ad2c573

25 files changed

+1333
-29
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@
9191

9292
- <a href="springboot-cloud-loadbalancer-demo">springboot-cloud-loadbalancer-demo</a>
9393
; client side loadbalancer with spring cloud loadbalancer
94+
95+
- <a href="springboot-cloud-ribbon-demo">springboot-cloud-ribbon-demo</a>
96+
; client side loadbalancer with spring cloud ribbon
97+
98+

springboot-cloud-loadbalancer-demo/debug.txt

+610
Large diffs are not rendered by default.

springboot-cloud-loadbalancer-demo/src/main/java/demo/DemoApplication.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,14 @@ public static void main(String[] args) {
2525
}
2626

2727
private static void displayBeans(ConfigurableApplicationContext ctx) {
28-
final String[] activeProfiles = ctx.getEnvironment().getActiveProfiles();
29-
30-
for (String activeProfile : activeProfiles) {
31-
if ("real".equals(activeProfile)) {
32-
return;
33-
}
34-
}
35-
3628
final String[] beanNames = ctx.getBeanDefinitionNames();
3729
final StringBuilder builder = new StringBuilder();
3830

3931
for (String beanName : beanNames) {
4032
final Object beanObj = ctx.getBean(beanName);
4133

4234
if (isDisplayBean(beanObj.getClass().getName())) {
43-
builder.append('[').append(beanName).append("] : ")
35+
builder.append("- ").append(beanName).append("\n : ")
4436
.append(beanObj.getClass().getName()).append('\n');
4537
}
4638
}
@@ -50,6 +42,6 @@ private static void displayBeans(ConfigurableApplicationContext ctx) {
5042
}
5143

5244
private static boolean isDisplayBean(String className) {
53-
return className.startsWith("org.springframework.cloud.loadbalancer");
45+
return className.startsWith("org.springframework.cloud");
5446
}
5547
}

springboot-cloud-loadbalancer-demo/src/main/java/demo/client/ClientController.java

+20-14
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,32 @@ public class ClientController {
2020

2121
private final WebClient.Builder loadBalancedWebClientBuilder;
2222
private final ReactorLoadBalancerExchangeFilterFunction lbFunction;
23+
private final LogExchangeFilterFunction logFilter = new LogExchangeFilterFunction();
2324

2425
@GetMapping("/hi")
2526
public Mono<String> hi(@RequestParam(value = "name", defaultValue = "Mary") String name) {
26-
return loadBalancedWebClientBuilder.build()
27-
.get()
28-
.uri("http://say-hello/greeting")
29-
.retrieve()
30-
.bodyToMono(String.class)
31-
.map(greeting -> String.format("%s, %s!", greeting, name));
27+
WebClient webClient = loadBalancedWebClientBuilder.build();
28+
return webClient
29+
.get()
30+
.uri("http://say-hello/greeting")
31+
.retrieve()
32+
.bodyToMono(String.class)
33+
.map(greeting -> String.format("%s, %s!", greeting, name));
3234
}
3335

3436
@GetMapping("/hello")
3537
public Mono<String> hello(@RequestParam(value = "name", defaultValue = "John") String name) {
36-
return loadBalancedWebClientBuilder
37-
.filter(lbFunction)
38-
.build()
39-
.get()
40-
.uri("http://say-hello/greeting")
41-
.retrieve()
42-
.bodyToMono(String.class)
43-
.map(greeting -> String.format("%s, %s!", greeting, name));
38+
WebClient webClient = loadBalancedWebClientBuilder
39+
.filter(logFilter)
40+
// .filter(lbFunction)
41+
// .filter(logFilter)
42+
.build();
43+
44+
return webClient
45+
.get()
46+
.uri("http://say-hello/greeting")
47+
.retrieve()
48+
.bodyToMono(String.class)
49+
.map(greeting -> String.format("%s, %s!", greeting, name));
4450
}
4551
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package demo.client;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.reactive.function.client.ClientRequest;
5+
import org.springframework.web.reactive.function.client.ClientResponse;
6+
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
7+
import org.springframework.web.reactive.function.client.ExchangeFunction;
8+
9+
import lombok.extern.slf4j.Slf4j;
10+
import reactor.core.publisher.Mono;
11+
12+
@Slf4j
13+
public class LogExchangeFilterFunction implements ExchangeFilterFunction {
14+
15+
@Override
16+
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
17+
logger.info("do filter : {}", request);
18+
// return Mono.just(ClientResponse.create(HttpStatus.BAD_REQUEST)
19+
// .body("forced exception")
20+
// .build());
21+
return next.exchange(request);
22+
}
23+
}

springboot-cloud-loadbalancer-demo/src/main/java/demo/config/ClientConfiguration.java

+17
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
import javax.annotation.PostConstruct;
44

5+
import org.springframework.cloud.client.ServiceInstance;
6+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
57
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
8+
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
69
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
10+
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
711
import org.springframework.context.annotation.Bean;
812
import org.springframework.context.annotation.ComponentScan;
913
import org.springframework.context.annotation.Configuration;
1014
import org.springframework.context.annotation.Profile;
15+
import org.springframework.core.env.Environment;
1116
import org.springframework.web.reactive.function.client.WebClient;
1217

18+
import lombok.RequiredArgsConstructor;
1319
import lombok.extern.slf4j.Slf4j;
1420

1521
/**
@@ -19,12 +25,23 @@
1925
@Profile("client")
2026
@ComponentScan("demo.client")
2127
@LoadBalancerClient(name = "say-hello", configuration = SayHelloConfiguration.class)
28+
@EnableDiscoveryClient
29+
@RequiredArgsConstructor
2230
@Slf4j
2331
public class ClientConfiguration {
2432

33+
private final LoadBalancerClientFactory loadBalancerClientFactory;
34+
private final ReactiveLoadBalancer.Factory loadBalancerFactory;
35+
private final Environment environment;
36+
2537
@PostConstruct
2638
private void setUp() {
2739
logger.info("## Enable client");
40+
logger.info(">> Check loadbalancer factory : {}-{}", loadBalancerFactory.getClass().getName(),
41+
loadBalancerFactory);
42+
43+
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
44+
System.out.println("## " + LoadBalancerClientFactory.PROPERTY_NAME + " ==> " + name);
2845
}
2946

3047
@LoadBalanced

springboot-cloud-loadbalancer-demo/src/main/java/demo/config/SayHelloConfiguration.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,17 @@ public String getServiceId() {
4444

4545
@Override
4646
public Flux<List<ServiceInstance>> get() {
47+
//final String host = "192.168.79.130";
48+
final String host = "localhost";
4749
logger.info("DemoServiceInstanceListSuppler::get() is called");
48-
return Flux.just(Arrays.asList(
49-
new DefaultServiceInstance(serviceId + "1", serviceId, "localhost", 3000, false),
50-
new DefaultServiceInstance(serviceId + "2", serviceId, "localhost", 3001, false),
51-
new DefaultServiceInstance(serviceId + "3", serviceId, "localhost", 3002, false)));
50+
51+
final List<ServiceInstance> servers = Arrays.asList(
52+
new DefaultServiceInstance(serviceId + "1", serviceId, host, 3000, false)
53+
, new DefaultServiceInstance(serviceId + "2", serviceId, host, 3001, false)
54+
, new DefaultServiceInstance(serviceId + "3", serviceId, host, 3002, false)
55+
);
56+
57+
return Flux.just(servers);
5258
}
5359
}
5460
}

springboot-cloud-loadbalancer-demo/src/main/resources/application.yaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@ server:
33

44
spring:
55
application:
6-
name: say-hello
6+
name: say-hello
7+
8+
logging:
9+
level:
10+
root: warn
11+
demo: trace
12+
org:
13+
springframework:
14+
cloud: debug
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**
6+
!**/src/test/**
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
17+
### IntelliJ IDEA ###
18+
.idea
19+
*.iws
20+
*.iml
21+
*.ipr
22+
out/
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
31+
### VS Code ###
32+
.vscode/
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Client loadbalancer with spring cloud ribbon
2+
; this is a demo project for client side loadbalancer with spring cloud ribbon from <a href="https://spring.io/guides/gs/client-side-load-balancing">Spring getting started</a>
3+
4+
## Getting started
5+
6+
> Start server1,2,3
7+
8+
```aidl
9+
// start server1
10+
$ ./gradlew bootRun -PjvmArgs="-Dserver.port=3000 -Dspring.profiles.active=server"
11+
12+
// start server2
13+
$ ./gradlew bootRun -PjvmArgs="-Dserver.port=3001 -Dspring.profiles.active=server"
14+
15+
// start server3
16+
$ ./gradlew bootRun -PjvmArgs="-Dserver.port=3002 -Dspring.profiles.active=server"
17+
```
18+
19+
> Start client
20+
21+
```aidl
22+
$ ./gradlew bootRun -PjvmArgs="-Dserver.port=8080 -Dspring.profiles.active=client"
23+
```
24+
25+
> Request to client repeatedly
26+
27+
```aidl
28+
$ curl -XGET http://127.0.0.1:8080/hello
29+
$ curl -XGET http://127.0.0.1:8080/hello
30+
$ curl -XGET http://127.0.0.1:8080/hello
31+
$ curl -XGET http://127.0.0.1:8080/hello
32+
$ curl -XGET http://127.0.0.1:8080/hello
33+
$ curl -XGET http://127.0.0.1:8080/hello
34+
```
35+
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
plugins {
2+
id 'org.springframework.boot' version '2.2.2.RELEASE'
3+
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
4+
id 'java'
5+
}
6+
7+
group = 'me.zaccoding'
8+
version = '0.0.1-SNAPSHOT'
9+
sourceCompatibility = '1.8'
10+
11+
configurations {
12+
developmentOnly
13+
runtimeClasspath {
14+
extendsFrom developmentOnly
15+
}
16+
compileOnly {
17+
extendsFrom annotationProcessor
18+
}
19+
}
20+
21+
repositories {
22+
mavenCentral()
23+
}
24+
25+
ext {
26+
set('springCloudVersion', "Hoxton.SR1")
27+
}
28+
29+
dependencies {
30+
implementation 'org.springframework.boot:spring-boot-starter-web'
31+
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-ribbon'
32+
33+
compileOnly 'org.projectlombok:lombok'
34+
developmentOnly 'org.springframework.boot:spring-boot-devtools'
35+
annotationProcessor 'org.projectlombok:lombok'
36+
37+
testImplementation('org.springframework.boot:spring-boot-starter-test') {
38+
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
39+
}
40+
}
41+
42+
dependencyManagement {
43+
imports {
44+
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
45+
}
46+
}
47+
48+
test {
49+
useJUnitPlatform()
50+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sun Jan 05 01:17:48 KST 2020
2+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip
3+
distributionBase=GRADLE_USER_HOME
4+
distributionPath=wrapper/dists
5+
zipStorePath=wrapper/dists
6+
zipStoreBase=GRADLE_USER_HOME

0 commit comments

Comments
 (0)