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
This commit is contained in:
Olga Maciaszek-Sharma
2020-09-14 15:50:54 +02:00
14 changed files with 659 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
## Kubernetes Spring Cloud LoadBalancer Example
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`.
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.
### Running the example
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
to install the platform locally within a virtual machine managed by VirtualBox, Xhyve or KVM, with no fuss.
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.
### Build/Deploy using Minikube
First, create a new virtual machine provisioned with Kubernetes on your laptop using the command `minikube start`.
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)
like also to deploy the application on Kubernetes in one maven line by running:
```
mvn clean install fabric8:deploy -Dfabric8.generator.from=fabric8/java-jboss-openjdk8-jdk -Pkubernetes
```
### Call the Greeting service
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
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
check if the pods have been created using this command :
```
kc get pods
```
If the status of the Spring Boot pod application is `running` and ready state `1`, then you can
get the external address IP/Hostname to be used to call the service from your laptop
```
minikube service --url greeting-service
```
and then call the service using the curl client
```
curl https://IP_OR_HOSTNAME/greeting
```
to get a response as such
```
Hello from name-service-1-0dzb4!d
```
### Verify the load balancing
First, scale the number of pods of the `name service` to 2
```
kc scale --replicas=2 deployment name-service
```
Wait a few minutes before to issue the curl request to call the Greeting Service to let the platform to create the new pod.
```
kc get pods --selector=project=name-service
NAME READY STATUS RESTARTS AGE
name-service-1652024859-fsnfw 1/1 Running 0 33s
name-service-1652024859-wrzjs 1/1 Running 0 6m
```
If you issue the curl request to access the greeting service, you should see that the message response
contains a different id end of the message which corresponds to the name of the pod.
```
Hello from name-service-1-0ss0r!
```
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,
you should see that you will get a response from one of the 2 pods running
```
kc get endpoints/name-service
NAME ENDPOINTS AGE
name-service 172.17.0.5:8080,172.17.0.6:8080 40m
```
Here is an example about what you will get
```
curl https://IP_OR_HOSTNAME/greeting
Hello from name-service-1652024859-hf3xv!
curl https://IP_OR_HOSTNAME/greeting
Hello from name-service-1652024859-426kv!
...
```

View File

@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kubernetes-loadbalancer-example</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.7.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>greeting-service</artifactId>
<name>Spring Cloud LoadBalancer :: Greeting Service</name>
<description>Spring Cloud LoadBalancer :: Greeting Service</description>
<build>
<plugins>
<plugin>
<!--skip deploy -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<scope>import</scope>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-loadbalancer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,10 @@
apiVersion: v1
kind: Route
metadata:
name: greeting-service
spec:
port:
targetPort: 8080
to:
kind: Service
name: greeting-service

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2020-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.examples;
import reactor.core.publisher.Mono;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Greeting service controller.
*
* @author Gytis Trikleris
* @author Olga Maciaszek-Sharma
*/
@RestController
public class GreetingController {
private final NameService nameService;
public GreetingController(NameService nameService) {
this.nameService = nameService;
}
/**
* Endpoint to get a greeting. This endpoint uses a name server to get a name for the
* greeting.
*
* Request to the name service is guarded with a circuit breaker. Therefore if a name
* service is not available or is too slow to response fallback name is used.
*
* Delay parameter can me used to make name service response slower.
* @param delay Milliseconds for how long the response from name service should be
* delayed.
* @return Greeting string.
*/
@GetMapping("/greeting")
public Mono<String> getGreeting(
@RequestParam(value = "delay", defaultValue = "0") int delay) {
return nameService.getName(delay)
.map(name -> String.format("Hello from %s!", name));
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2020-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.examples;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.client.WebClient;
/**
* Entry point to the application.
*
* @author Gytis Trikleris
* @author Olga Maciaszek-Sharma
*/
@SpringBootApplication
@EnableDiscoveryClient
public class GreetingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GreetingServiceApplication.class, args);
}
@LoadBalanced
@Bean
WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2020-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.examples;
import reactor.core.publisher.Mono;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
/**
* Service invoking name-service via REST and guarded by Hystrix.
*
* @author Gytis Trikleris
* @author Olga Maciaszek-Sharma
*/
@Service
public class NameService {
private final WebClient webClient;
public NameService(WebClient.Builder webClientBuilder) {
webClient = webClientBuilder.build();
}
public Mono<String> getName(int delay) {
return webClient.get()
.uri(String.format("http://name-service/name?delay=%d", delay)).retrieve()
.bodyToMono(String.class);
}
}

View File

@@ -0,0 +1,10 @@
spring:
application:
name: greeting-service
cloud:
loadbalancer:
ribbon:
enabled: false
server:
port: 8080

View File

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kubernetes-loadbalancer-example</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.7.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>name-service</artifactId>
<name>Spring Cloud LoadBalancer :: Name Service</name>
<description>Spring Cloud LoadBalancer :: Name Service</description>
<build>
<plugins>
<plugin>
<!--skip deploy -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,10 @@
apiVersion: v1
kind: Route
metadata:
name: name-service
spec:
port:
targetPort: 8080
to:
kind: Service
name: name-service

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2020-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Name service controller.
*
* @author Gytis Trikleris
* @author Olga Maciaszek-Sharma
*/
@RestController
public class NameController {
private static final Logger LOG = LoggerFactory.getLogger(NameController.class);
private final String hostName = System.getenv("HOSTNAME");
@GetMapping("/")
public String ribbonPing() {
LOG.info("Ribbon ping");
return hostName;
}
/**
* Endpoint to get a name with a capability to delay a response for some number of
* milliseconds.
* @param delayValue Milliseconds for how long the response should be delayed.
* @return Host name.
*/
@GetMapping("/name")
public Mono<String> getName(
@RequestParam(value = "delay", defaultValue = "0") int delayValue) {
LOG.info(String.format("Returning a name '%s' with a delay '%d'", hostName,
delayValue));
delay(delayValue);
return Mono.just(hostName);
}
private void delay(int delayValue) {
try {
Thread.sleep(delayValue);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2020-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.examples;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Entry point to the application.
*
* @author Gytis Trikleris
*/
@SpringBootApplication
public class NameServiceApplication {
public static void main(String[] args) {
SpringApplication.run(NameServiceApplication.class, args);
}
}

View File

@@ -0,0 +1,6 @@
spring:
application:
name: name-service
server:
port: 8080

View File

@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-kubernetes-examples</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.1.7.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>kubernetes-loadbalancer-example</artifactId>
<name>Spring Cloud LoadBalancer Example</name>
<packaging>pom</packaging>
<profiles>
<profile>
<id>kubernetes</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>${fabric8.maven.plugin.version}</version>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<enricher>
<config>
<fmp-service>
<type>NodePort</type>
</fmp-service>
</config>
</enricher>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>${fabric8.maven.plugin.version}</version>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
<goal>helm</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>${fabric8.maven.plugin.version}</version>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<executions>
<execution>
<id>run-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<skipTests>false</skipTests>
<skipITs>false</skipITs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<modules>
<module>name-service</module>
<module>greeting-service</module>
</modules>
</project>

View File

@@ -37,6 +37,7 @@
<module>kubernetes-reload-example</module>
<module>kubernetes-hello-world-example</module>
<module>kubernetes-leader-election-example</module>
<module>kubernetes-loadbalancer-example</module>
</modules>
<build>