Skip to content

Commit dbce014

Browse files
author
Martin Tošovský
committed
Initial commit
raw structure of the project, 3 modules - load balancer client, server and hysterix client
0 parents  commit dbce014

20 files changed

Lines changed: 749 additions & 0 deletions

File tree

.gitignore

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2+
hs_err_pid*
3+
4+
# Compiled source #
5+
###################
6+
*.com
7+
*.class
8+
*.dll
9+
*.exe
10+
*.o
11+
*.so
12+
13+
# Packages #
14+
############
15+
# it's better to unpack these files and commit the raw source
16+
# git has its own built in compression methods
17+
*.7z
18+
*.dmg
19+
*.gz
20+
*.iso
21+
*.jar
22+
*.rar
23+
*.tar
24+
*.zip
25+
26+
# Logs and databases #
27+
######################
28+
*.log
29+
30+
# OS generated files #
31+
######################
32+
.DS_Store*
33+
ehthumbs.db
34+
Icon?
35+
Thumbs.db
36+
37+
# Editor Files #
38+
################
39+
*~
40+
*.swp
41+
42+
# Gradle Files #
43+
################
44+
.gradle
45+
.m2
46+
47+
# Build output directies
48+
/target
49+
*/target
50+
/build
51+
*/build
52+
/bin
53+
*/bin
54+
#
55+
# # IntelliJ specific files/directories
56+
57+
# IntelliJ specific files/directories
58+
out
59+
.idea
60+
*.ipr
61+
*.iws
62+
*.iml
63+
atlassian-ide-plugin.xml
64+
65+
# Eclipse specific files/directories
66+
.classpath
67+
.project
68+
.settings
69+
.metadata
70+
71+
# NetBeans specific files/directories
72+
.nbattrs
73+
74+

JavaHystrixClient/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
6+
<groupId>org.gooddata</groupId>
7+
<artifactId>java-hystrix-client</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>JavaHystrixClient</name>
12+
<description>Resilient hystrix-based HTTP client.</description>
13+
14+
<parent>
15+
<groupId>org.gooddata</groupId>
16+
<artifactId>load-balance-client-parent</artifactId>
17+
<version>0.0.1.BUILD-SNAPSHOT</version>
18+
</parent>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-actuator</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.cloud</groupId>
39+
<artifactId>spring-cloud-starter-feign</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.cloud</groupId>
44+
<artifactId>spring-cloud-starter-hystrix</artifactId>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-web</artifactId>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>com.netflix.archaius</groupId>
54+
<artifactId>archaius-core</artifactId>
55+
</dependency>
56+
</dependencies>
57+
58+
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.toda.hystrixclient;
2+
3+
import static com.toda.hystrixclient.JavaHystrixClientApplication.UPSTREAM_SERVER_CLIENT;
4+
import static org.slf4j.LoggerFactory.getLogger;
5+
6+
import org.slf4j.Logger;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.CommandLineRunner;
9+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
10+
import org.springframework.boot.builder.SpringApplicationBuilder;
11+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.core.annotation.Order;
14+
import org.springframework.http.HttpMethod;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.stereotype.Component;
17+
import org.springframework.web.client.RestTemplate;
18+
19+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
20+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
21+
22+
23+
@Configuration
24+
@EnableAutoConfiguration
25+
@EnableCircuitBreaker
26+
public class JavaHystrixClientApplication {
27+
28+
public static final String UPSTREAM_SERVER_CLIENT = "upstreamServerClient";
29+
30+
public static void main(String[] args) {
31+
new SpringApplicationBuilder(JavaHystrixClientApplication.class).web(false).run(args);
32+
}
33+
}
34+
35+
@Order(1)
36+
@Component
37+
class RestTemplateExample implements CommandLineRunner {
38+
39+
private Logger log = getLogger(RestTemplateExample.class);
40+
41+
@Autowired
42+
private RestTemplate restTemplate;
43+
44+
@Override
45+
public void run(String... strings) throws Exception {
46+
log.info("------------------------------");
47+
log.info("RestTemplate Example");
48+
49+
for (int i = 0; i < 10; i++) {
50+
try {
51+
log.info(getMessage());
52+
}
53+
catch (RuntimeException e) {
54+
log.error("Exception in run {}. -> {}", i, e.getMessage());
55+
}
56+
Thread.sleep(2000);
57+
}
58+
}
59+
60+
@HystrixCommand(fallbackMethod = "stubForGetMessage", commandProperties = {
61+
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "3"),//How many requests are needed in the time span to trigger the circuit breaker?
62+
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "3000"), //After what time should the circuit breaker try a single request
63+
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"), // After what time should the circuit breaker try a single request?
64+
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50") //How many errors are allowed before the circuit breaker is activated ?
65+
})
66+
private String getMessage() {
67+
ResponseEntity<String> exchange = this.restTemplate.exchange(
68+
"http://" + UPSTREAM_SERVER_CLIENT + "/message",
69+
HttpMethod.GET, null, String.class);
70+
log.info(String.valueOf(exchange.getStatusCode()));
71+
return exchange.getBody();
72+
}
73+
74+
private String stubForGetMessage() {
75+
return "FooResult";
76+
}
77+
}
78+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
upstreamServerClient.ribbon.listOfServers=localhost:8070,localhost:8080
2+
3+
# Max number of retries on the same server (excluding the first try)
4+
upstreamServerClient.ribbon.MaxAutoRetries=1
5+
6+
# Max number of next servers to retry (excluding the first server)
7+
upstreamServerClient.ribbon.MaxAutoRetriesNextServer=1
8+
9+
# Whether all operations can be retried for this client
10+
upstreamServerClient.ribbon.OkToRetryOnAllOperations=true
11+
12+
# Connect timeout used by Apache HttpClient
13+
upstreamServerClient.ribbon.ConnectTimeout=3000
14+
15+
# Read timeout used by Apache HttpClient
16+
upstreamServerClient.ribbon.ReadTimeout=3000
17+
18+
# Interval to refresh the server list from the source- refresh every 2 sec
19+
upstreamServerClient.ribbon.ServerListRefreshInterval=2000
20+
21+
upstreamServerClient.ribbon.NFLoadBalancerClassName=com.netflix.loadbalancer.DynamicServerListLoadBalancer
22+
23+
#ZoneAwareNIWSDiscoveryLoadBalancer.enabled=false
24+
25+
26+
#ribbon.client.name=riboon

JavaLoadBalancerClient/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Upstream load-balancing client
2+
3+
Settings of the upstreams servers is in the /resources/application.properties file.
4+
The structure of settings for ribbon is 'name-of-the-client'.ribbon.'specific-property'=value.
5+
6+
Example:
7+
upstreamServerClient.ribbon.listOfServers=localhost:8080,localhost:8070

JavaLoadBalancerClient/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
6+
<groupId>org.gooddata</groupId>
7+
<artifactId>java-load-balancer-client</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>JavaLoadBalancerClient</name>
12+
<description>Load balancing java HTTP client.</description>
13+
14+
<parent>
15+
<groupId>org.gooddata</groupId>
16+
<artifactId>load-balance-client-parent</artifactId>
17+
<version>0.0.1.BUILD-SNAPSHOT</version>
18+
</parent>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-actuator</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.cloud</groupId>
39+
<artifactId>spring-cloud-starter-feign</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.cloud</groupId>
44+
<artifactId>spring-cloud-starter-hystrix</artifactId>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-web</artifactId>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>com.netflix.archaius</groupId>
54+
<artifactId>archaius-core</artifactId>
55+
</dependency>
56+
</dependencies>
57+
58+
</project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.toda.javaloadbalancerclient;
2+
3+
import static com.toda.javaloadbalancerclient.JavaLoadBalancerClientApplication.UPSTREAM_SERVER_CLIENT;
4+
import static org.slf4j.LoggerFactory.getLogger;
5+
6+
import org.slf4j.Logger;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.CommandLineRunner;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.boot.builder.SpringApplicationBuilder;
11+
import org.springframework.cloud.netflix.ribbon.RibbonClient;
12+
import org.springframework.context.annotation.ComponentScan;
13+
import org.springframework.context.annotation.ComponentScan.Filter;
14+
import org.springframework.context.annotation.FilterType;
15+
import org.springframework.core.annotation.Order;
16+
import org.springframework.http.HttpMethod;
17+
import org.springframework.http.ResponseEntity;
18+
import org.springframework.stereotype.Component;
19+
import org.springframework.web.client.RestTemplate;
20+
21+
import com.toda.javaloadbalancerclient.config.ExcludeFromComponentScan;
22+
import com.toda.javaloadbalancerclient.config.clientsconfig.LoadBalancerConfig;
23+
24+
@SpringBootApplication
25+
@ComponentScan(excludeFilters={
26+
@Filter(type = FilterType.ANNOTATION, value = ExcludeFromComponentScan.class)})
27+
@RibbonClient(name = JavaLoadBalancerClientApplication.UPSTREAM_SERVER_CLIENT, configuration = LoadBalancerConfig.class)
28+
public class JavaLoadBalancerClientApplication {
29+
30+
public static final String UPSTREAM_SERVER_CLIENT = "upstreamServerClient";
31+
32+
public static void main(String[] args) {
33+
new SpringApplicationBuilder(JavaLoadBalancerClientApplication.class).web(false).run(args);
34+
}
35+
}
36+
37+
@Order(1)
38+
@Component
39+
class RestTemplateExample implements CommandLineRunner {
40+
41+
private Logger log = getLogger(RestTemplateExample.class);
42+
43+
@Autowired
44+
private RestTemplate restTemplate;
45+
46+
@Override
47+
public void run(String... strings) throws Exception {
48+
log.info("------------------------------");
49+
log.info("RestTemplate Example");
50+
51+
for (int i = 0; i < 20; i++) {
52+
try {
53+
log.info(getMessage());
54+
}
55+
catch (RuntimeException e) {
56+
log.error("Exception in run {}. -> {}", i, e.getMessage());
57+
}
58+
Thread.sleep(1000);
59+
}
60+
}
61+
62+
private String getMessage() {
63+
ResponseEntity<String> exchange = this.restTemplate.exchange(
64+
"http://" + UPSTREAM_SERVER_CLIENT + "/message",
65+
HttpMethod.GET, null, String.class);
66+
log.info(String.valueOf(exchange.getStatusCode()));
67+
return exchange.getBody();
68+
}
69+
70+
private String stubForGetMessage() {
71+
return "FooResult";
72+
}
73+
}
74+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.toda.javaloadbalancerclient.config;
2+
3+
/**
4+
* Marker interface to exclude from component scan
5+
*/
6+
public @interface ExcludeFromComponentScan {
7+
}

0 commit comments

Comments
 (0)