Skip to content

Commit 7ac699f

Browse files
authored
Merge pull request eugenp#6507 from cscib/BAEL-2460
Bael 2460
2 parents 76ff0d2 + 6f26499 commit 7ac699f

File tree

29 files changed

+871
-0
lines changed

29 files changed

+871
-0
lines changed

spring-cloud/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<module>spring-cloud-zuul-eureka-integration</module>
3333
<module>spring-cloud-contract</module>
3434
<module>spring-cloud-kubernetes</module>
35+
<module>spring-cloud-kubernetes-2</module>
3536
<module>spring-cloud-archaius</module>
3637
<module>spring-cloud-functions</module>
3738
<module>spring-cloud-vault</module>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM openjdk:8-jdk-alpine
2+
VOLUME /tmp
3+
COPY target/client-service-1.0-SNAPSHOT.jar app.jar
4+
ENV JAVA_OPTS=""
5+
ENTRYPOINT exec java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9999 -jar /app.jar
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: client-service
5+
data:
6+
application.properties: |-
7+
bean.message=Testing reload ! Message from backend is: %s <br/> Services : %s
8+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
kind: Service
2+
apiVersion: v1
3+
metadata:
4+
name: client-service
5+
spec:
6+
selector:
7+
app: client-service
8+
ports:
9+
- protocol: TCP
10+
port: 8080
11+
nodePort: 30083
12+
type: NodePort
13+
---
14+
apiVersion: apps/v1
15+
kind: Deployment
16+
metadata:
17+
name: client-service
18+
spec:
19+
selector:
20+
matchLabels:
21+
app: client-service
22+
replicas: 1
23+
template:
24+
metadata:
25+
labels:
26+
app: client-service
27+
spec:
28+
containers:
29+
- name: client-service
30+
image: client-service:latest
31+
imagePullPolicy: Never
32+
ports:
33+
- containerPort: 8080
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>client-service</artifactId>
6+
<name>client-service</name>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<parent>
10+
<groupId>com.baeldung.spring.cloud</groupId>
11+
<artifactId>spring-cloud-kubernetes-2</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
</parent>
14+
15+
<properties>
16+
<java.version>1.8</java.version>
17+
<spring-cloud-dependencies.version>Finchley.SR2</spring-cloud-dependencies.version>
18+
<spring.cloud.k8s.version>1.0.0.RELEASE</spring.cloud.k8s.version>
19+
</properties>
20+
21+
<dependencyManagement>
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.cloud</groupId>
25+
<artifactId>spring-cloud-dependencies</artifactId>
26+
<version>${spring-cloud-dependencies.version}</version>
27+
<type>pom</type>
28+
<scope>import</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.cloud</groupId>
32+
<artifactId>spring-cloud-kubernetes-dependencies</artifactId>
33+
<version>${spring.cloud.k8s.version}</version>
34+
<type>pom</type>
35+
<scope>import</scope>
36+
</dependency>
37+
</dependencies>
38+
</dependencyManagement>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>org.springframework.cloud</groupId>
43+
<artifactId>spring-cloud-kubernetes-discovery</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.cloud</groupId>
47+
<artifactId>spring-cloud-starter-kubernetes-config</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-web</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-actuator</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-test</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework.cloud</groupId>
64+
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.springframework.cloud</groupId>
68+
<artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.springframework.cloud</groupId>
72+
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-maven-plugin</artifactId>
81+
<configuration>
82+
<mainClass>com.baeldung.spring.cloud.kubernetes.client.Application</mainClass>
83+
<layout>JAR</layout>
84+
</configuration>
85+
<executions>
86+
<execution>
87+
<goals>
88+
<goal>repackage</goal>
89+
</goals>
90+
</execution>
91+
</executions>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
96+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.spring.cloud.kubernetes.client;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
6+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
7+
import org.springframework.cloud.netflix.ribbon.RibbonClient;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.web.client.RestTemplate;
10+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
11+
12+
@SpringBootApplication
13+
@EnableDiscoveryClient
14+
@EnableCircuitBreaker
15+
@RibbonClient(name = "travel-agency-service", configuration = RibbonConfiguration.class)
16+
public class Application {
17+
18+
@LoadBalanced
19+
@Bean
20+
RestTemplate restTemplate() {
21+
return new RestTemplate();
22+
}
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(Application.class, args);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.spring.cloud.kubernetes.client;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ConfigurationProperties(prefix = "bean")
8+
public class ClientConfig {
9+
10+
private String message = "Message from backend is: %s <br/> Services : %s";
11+
12+
public String getMessage() {
13+
return message;
14+
}
15+
16+
public void setMessage(String message) {
17+
this.message = message;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.spring.cloud.kubernetes.client;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.cloud.client.ServiceInstance;
5+
import org.springframework.cloud.client.discovery.DiscoveryClient;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.util.CollectionUtils;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
import org.springframework.web.client.RestTemplate;
12+
13+
import java.net.UnknownHostException;
14+
import java.util.List;
15+
16+
@RestController
17+
public class ClientController {
18+
19+
@Autowired
20+
private DiscoveryClient discoveryClient;
21+
22+
@Autowired
23+
private ClientConfig config;
24+
25+
@Autowired
26+
private TravelAgencyService travelAgencyService;
27+
28+
@RequestMapping("/deals")
29+
public String getDeals() {
30+
return travelAgencyService.getDeals();
31+
}
32+
33+
@GetMapping
34+
public String load() {
35+
36+
RestTemplate restTemplate = new RestTemplate();
37+
String resourceUrl = "http://travel-agency-service:8080";
38+
ResponseEntity<String> response = restTemplate.getForEntity(resourceUrl, String.class);
39+
40+
String serviceList = "";
41+
if (discoveryClient != null) {
42+
List<String> services = this.discoveryClient.getServices();
43+
44+
for (String service : services) {
45+
46+
List<ServiceInstance> instances = this.discoveryClient.getInstances(service);
47+
48+
serviceList += ("[" + service + " : " + ((!CollectionUtils.isEmpty(instances)) ? instances.size() : 0) + " instances ]");
49+
}
50+
}
51+
52+
return String.format(config.getMessage(), response.getBody(), serviceList);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2016 to the original 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+
* http://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 com.baeldung.spring.cloud.kubernetes.client;
18+
19+
import com.netflix.client.config.IClientConfig;
20+
import com.netflix.loadbalancer.AvailabilityFilteringRule;
21+
import com.netflix.loadbalancer.IPing;
22+
import com.netflix.loadbalancer.IRule;
23+
import com.netflix.loadbalancer.PingUrl;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Bean;
26+
27+
public class RibbonConfiguration {
28+
29+
@Autowired
30+
IClientConfig ribbonClientConfig;
31+
32+
/**
33+
* PingUrl will ping a URL to check the status of each server.
34+
* Say Hello has, as you’ll recall, a method mapped to the /path; that means that Ribbon will get an HTTP 200 response when it pings a running Backend Server
35+
*
36+
* @param config Client configuration
37+
* @return The URL to be used for the Ping
38+
*/
39+
@Bean
40+
public IPing ribbonPing(IClientConfig config) {
41+
return new PingUrl();
42+
}
43+
44+
/**
45+
* AvailabilityFilteringRule will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state:
46+
* if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally.
47+
*
48+
* @param config Client configuration
49+
* @return The Load Balancer rule
50+
*/
51+
@Bean
52+
public IRule ribbonRule(IClientConfig config) {
53+
return new AvailabilityFilteringRule();
54+
}
55+
}

0 commit comments

Comments
 (0)