Skip to content

Commit 6ba387c

Browse files
committed
Merge remote-tracking branch 'origin/1.1.x'
# Conflicts: # docs/pom.xml # pom.xml # spring-cloud-kubernetes-config/pom.xml # spring-cloud-kubernetes-core/pom.xml # spring-cloud-kubernetes-dependencies/pom.xml # spring-cloud-kubernetes-discovery/pom.xml # spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/greeting-service/pom.xml # spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/name-service/pom.xml # spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/pom.xml # spring-cloud-kubernetes-examples/kubernetes-hello-world-example/pom.xml # spring-cloud-kubernetes-examples/kubernetes-leader-election-example/pom.xml # spring-cloud-kubernetes-examples/kubernetes-reload-example/pom.xml # spring-cloud-kubernetes-examples/kubernetes-zipkin-example/pom.xml # spring-cloud-kubernetes-examples/pom.xml # spring-cloud-kubernetes-integration-tests/discovery/discovery-client/pom.xml # spring-cloud-kubernetes-integration-tests/discovery/discovery-service-a/pom.xml # spring-cloud-kubernetes-integration-tests/discovery/discovery-service-b/pom.xml # spring-cloud-kubernetes-integration-tests/discovery/pom.xml # spring-cloud-kubernetes-integration-tests/discovery/tests/pom.xml # spring-cloud-kubernetes-integration-tests/istio/pom.xml # spring-cloud-kubernetes-integration-tests/load-balancer/pom.xml # spring-cloud-kubernetes-integration-tests/pom.xml # spring-cloud-kubernetes-integration-tests/simple-configmap/pom.xml # spring-cloud-kubernetes-integration-tests/simple-core/pom.xml # spring-cloud-kubernetes-istio/pom.xml # spring-cloud-kubernetes-leader/pom.xml # spring-cloud-kubernetes-loadbalancer/pom.xml # spring-cloud-kubernetes-ribbon/pom.xml # spring-cloud-starter-kubernetes-all/pom.xml # spring-cloud-starter-kubernetes-config/pom.xml # spring-cloud-starter-kubernetes-loadbalancer/pom.xml # spring-cloud-starter-kubernetes-ribbon/pom.xml # spring-cloud-starter-kubernetes/pom.xml
2 parents 5882245 + d301a7a commit 6ba387c

File tree

14 files changed

+659
-0
lines changed

14 files changed

+659
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
## Kubernetes Spring Cloud LoadBalancer Example
2+
3+
This example demonstrates how to use [Spring Cloud LoadBalancer](https://github.com/spring-cloud-incubator/spring-cloud-loadbalancer). In our example the REST `greeting service` calls the `name service`.
4+
As the Spring Cloud LoadBalancer for Kubernetes is configured within this example, it will fetch from the Kubernetes API Server the list of the endpoints available for the name service and load-balance the request between the IP addresses available.
5+
6+
### Running the example
7+
8+
This project example runs on ALL the Kubernetes or OpenShift environments, but for development purposes you can use [Minikube - Kubernetes](https://kubernetes.io/docs/getting-started-guides/minikube/) tool
9+
to install the platform locally within a virtual machine managed by VirtualBox, Xhyve or KVM, with no fuss.
10+
11+
IMPORTANT: In order for this setup to work, you need to grant permissions to retrieve "pods", "services" and "enpoints" to the serviceaccont that will be used with the greeting-service.
12+
13+
### Build/Deploy using Minikube
14+
15+
First, create a new virtual machine provisioned with Kubernetes on your laptop using the command `minikube start`.
16+
17+
Next, you can compile your project and generate the Kubernetes resources (yaml files containing the definition of the pod, deployment, build, service and route to be created)
18+
like also to deploy the application on Kubernetes in one maven line by running:
19+
20+
```
21+
mvn clean install fabric8:deploy -Dfabric8.generator.from=fabric8/java-jboss-openjdk8-jdk -Pkubernetes
22+
```
23+
24+
### Call the Greeting service
25+
26+
When maven has finished to compile the code but also to call the platform in order to deploy the yaml files generated and tell to the platform to start the process
27+
to build/deploy the docker image and create the containers where the Spring Boot application will run 'greeting-service" and "name-service", you will be able to
28+
check if the pods have been created using this command :
29+
30+
```
31+
kc get pods
32+
```
33+
34+
If the status of the Spring Boot pod application is `running` and ready state `1`, then you can
35+
get the external address IP/Hostname to be used to call the service from your laptop
36+
37+
```
38+
minikube service --url greeting-service
39+
```
40+
41+
and then call the service using the curl client
42+
43+
```
44+
curl https://IP_OR_HOSTNAME/greeting
45+
```
46+
47+
to get a response as such
48+
49+
```
50+
Hello from name-service-1-0dzb4!d
51+
```
52+
53+
### Verify the load balancing
54+
55+
First, scale the number of pods of the `name service` to 2
56+
57+
```
58+
kc scale --replicas=2 deployment name-service
59+
```
60+
61+
Wait a few minutes before to issue the curl request to call the Greeting Service to let the platform to create the new pod.
62+
63+
```
64+
kc get pods --selector=project=name-service
65+
NAME READY STATUS RESTARTS AGE
66+
name-service-1652024859-fsnfw 1/1 Running 0 33s
67+
name-service-1652024859-wrzjs 1/1 Running 0 6m
68+
```
69+
70+
If you issue the curl request to access the greeting service, you should see that the message response
71+
contains a different id end of the message which corresponds to the name of the pod.
72+
73+
```
74+
Hello from name-service-1-0ss0r!
75+
```
76+
77+
As Spring Cloud LoadBalancer will question the Kubernetes API to get, base on the `name-service` name, the list of IP Addresses assigned to the service as endpoints,
78+
you should see that you will get a response from one of the 2 pods running
79+
80+
```
81+
kc get endpoints/name-service
82+
NAME ENDPOINTS AGE
83+
name-service 172.17.0.5:8080,172.17.0.6:8080 40m
84+
```
85+
86+
Here is an example about what you will get
87+
88+
```
89+
curl https://IP_OR_HOSTNAME/greeting
90+
Hello from name-service-1652024859-hf3xv!
91+
curl https://IP_OR_HOSTNAME/greeting
92+
Hello from name-service-1652024859-426kv!
93+
...
94+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>kubernetes-loadbalancer-example</artifactId>
7+
<groupId>org.springframework.cloud</groupId>
8+
<version>1.1.7.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>greeting-service</artifactId>
13+
<name>Spring Cloud LoadBalancer :: Greeting Service</name>
14+
<description>Spring Cloud LoadBalancer :: Greeting Service</description>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<!--skip deploy -->
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-deploy-plugin</artifactId>
22+
<configuration>
23+
<skip>true</skip>
24+
</configuration>
25+
</plugin>
26+
27+
<plugin>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-maven-plugin</artifactId>
30+
<version>${spring-boot.version}</version>
31+
<executions>
32+
<execution>
33+
<goals>
34+
<goal>repackage</goal>
35+
</goals>
36+
</execution>
37+
</executions>
38+
</plugin>
39+
40+
<plugin>
41+
<groupId>io.fabric8</groupId>
42+
<artifactId>fabric8-maven-plugin</artifactId>
43+
<executions>
44+
<execution>
45+
<id>fmp</id>
46+
<goals>
47+
<goal>resource</goal>
48+
</goals>
49+
</execution>
50+
</executions>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
<dependencyManagement>
56+
<dependencies>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-dependencies</artifactId>
60+
<type>pom</type>
61+
<scope>import</scope>
62+
<version>${spring-boot.version}</version>
63+
</dependency>
64+
</dependencies>
65+
</dependencyManagement>
66+
67+
<dependencies>
68+
<dependency>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-starter-webflux</artifactId>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-starter-actuator</artifactId>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.springframework.cloud</groupId>
78+
<artifactId>spring-cloud-starter-kubernetes-loadbalancer</artifactId>
79+
<version>${project.version}</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.springframework.cloud</groupId>
83+
<artifactId>spring-cloud-starter-kubernetes</artifactId>
84+
<version>${project.version}</version>
85+
</dependency>
86+
<dependency>
87+
<groupId>com.squareup.okhttp3</groupId>
88+
<artifactId>okhttp</artifactId>
89+
</dependency>
90+
91+
</dependencies>
92+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: v1
2+
kind: Route
3+
metadata:
4+
name: greeting-service
5+
spec:
6+
port:
7+
targetPort: 8080
8+
to:
9+
kind: Service
10+
name: greeting-service
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2020-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.examples;
18+
19+
import reactor.core.publisher.Mono;
20+
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.RequestParam;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
/**
26+
* Greeting service controller.
27+
*
28+
* @author Gytis Trikleris
29+
* @author Olga Maciaszek-Sharma
30+
*/
31+
@RestController
32+
public class GreetingController {
33+
34+
private final NameService nameService;
35+
36+
public GreetingController(NameService nameService) {
37+
this.nameService = nameService;
38+
}
39+
40+
/**
41+
* Endpoint to get a greeting. This endpoint uses a name server to get a name for the
42+
* greeting.
43+
*
44+
* Request to the name service is guarded with a circuit breaker. Therefore if a name
45+
* service is not available or is too slow to response fallback name is used.
46+
*
47+
* Delay parameter can me used to make name service response slower.
48+
* @param delay Milliseconds for how long the response from name service should be
49+
* delayed.
50+
* @return Greeting string.
51+
*/
52+
@GetMapping("/greeting")
53+
public Mono<String> getGreeting(
54+
@RequestParam(value = "delay", defaultValue = "0") int delay) {
55+
return nameService.getName(delay)
56+
.map(name -> String.format("Hello from %s!", name));
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2020-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.examples;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
22+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.web.reactive.function.client.WebClient;
25+
26+
/**
27+
* Entry point to the application.
28+
*
29+
* @author Gytis Trikleris
30+
* @author Olga Maciaszek-Sharma
31+
*/
32+
@SpringBootApplication
33+
@EnableDiscoveryClient
34+
public class GreetingServiceApplication {
35+
36+
public static void main(String[] args) {
37+
SpringApplication.run(GreetingServiceApplication.class, args);
38+
}
39+
40+
@LoadBalanced
41+
@Bean
42+
WebClient.Builder loadBalancedWebClientBuilder() {
43+
return WebClient.builder();
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2020-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.examples;
18+
19+
import reactor.core.publisher.Mono;
20+
21+
import org.springframework.stereotype.Service;
22+
import org.springframework.web.reactive.function.client.WebClient;
23+
24+
/**
25+
* Service invoking name-service via REST and guarded by Hystrix.
26+
*
27+
* @author Gytis Trikleris
28+
* @author Olga Maciaszek-Sharma
29+
*/
30+
@Service
31+
public class NameService {
32+
33+
private final WebClient webClient;
34+
35+
public NameService(WebClient.Builder webClientBuilder) {
36+
webClient = webClientBuilder.build();
37+
}
38+
39+
public Mono<String> getName(int delay) {
40+
return webClient.get()
41+
.uri(String.format("http://name-service/name?delay=%d", delay)).retrieve()
42+
.bodyToMono(String.class);
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring:
2+
application:
3+
name: greeting-service
4+
cloud:
5+
loadbalancer:
6+
ribbon:
7+
enabled: false
8+
9+
server:
10+
port: 8080

0 commit comments

Comments
 (0)