Skip to content

Commit 802c0b0

Browse files
Tian Baoqiangpivovarit
authored andcommitted
#BAEL-1025 (#2445)
1 parent 706b3d6 commit 802c0b0

File tree

7 files changed

+305
-0
lines changed

7 files changed

+305
-0
lines changed

ratpack/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,38 @@
4040
<artifactId>ratpack-hikari</artifactId>
4141
<version>${ratpack.version}</version>
4242
</dependency>
43+
<dependency>
44+
<groupId>io.ratpack</groupId>
45+
<artifactId>ratpack-hystrix</artifactId>
46+
<version>${ratpack.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.ratpack</groupId>
50+
<artifactId>ratpack-rx</artifactId>
51+
<version>${ratpack.version}</version>
52+
</dependency>
4353
<dependency>
4454
<groupId>io.ratpack</groupId>
4555
<artifactId>ratpack-test</artifactId>
4656
<version>${ratpack.version}</version>
57+
<scope>test</scope>
4758
</dependency>
4859
<dependency>
4960
<groupId>com.h2database</groupId>
5061
<artifactId>h2</artifactId>
5162
<version>1.4.193</version>
5263
</dependency>
64+
65+
<dependency>
66+
<groupId>org.apache.httpcomponents</groupId>
67+
<artifactId>httpclient</artifactId>
68+
<version>4.5.3</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.apache.httpcomponents</groupId>
72+
<artifactId>httpcore</artifactId>
73+
<version>4.4.6</version>
74+
</dependency>
5375
</dependencies>
5476

5577
<build>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.hystrix;
2+
3+
import com.netflix.hystrix.HystrixCommand;
4+
import com.netflix.hystrix.HystrixCommandGroupKey;
5+
import com.netflix.hystrix.HystrixCommandProperties;
6+
import org.apache.http.client.config.RequestConfig;
7+
import org.apache.http.client.methods.HttpGet;
8+
import org.apache.http.impl.client.HttpClientBuilder;
9+
import org.apache.http.message.BasicHeader;
10+
import org.apache.http.util.EntityUtils;
11+
12+
import java.net.URI;
13+
import java.util.Collections;
14+
15+
/**
16+
* @author aiet
17+
*/
18+
public class HystrixAsyncHttpCommand extends HystrixCommand<String> {
19+
20+
private URI uri;
21+
private RequestConfig requestConfig;
22+
23+
HystrixAsyncHttpCommand(URI uri, int timeoutMillis) {
24+
super(Setter
25+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix-ratpack-async"))
26+
.andCommandPropertiesDefaults(HystrixCommandProperties
27+
.Setter()
28+
.withExecutionTimeoutInMilliseconds(timeoutMillis)));
29+
requestConfig = RequestConfig
30+
.custom()
31+
.setSocketTimeout(timeoutMillis)
32+
.setConnectTimeout(timeoutMillis)
33+
.setConnectionRequestTimeout(timeoutMillis)
34+
.build();
35+
this.uri = uri;
36+
}
37+
38+
@Override
39+
protected String run() throws Exception {
40+
return EntityUtils.toString(HttpClientBuilder
41+
.create()
42+
.setDefaultRequestConfig(requestConfig)
43+
.setDefaultHeaders(Collections.singleton(new BasicHeader("User-Agent", "Baeldung Blocking HttpClient")))
44+
.build()
45+
.execute(new HttpGet(uri))
46+
.getEntity());
47+
}
48+
49+
@Override
50+
protected String getFallback() {
51+
return "eugenp's async fallback profile";
52+
}
53+
54+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.hystrix;
2+
3+
import com.netflix.hystrix.HystrixCommandGroupKey;
4+
import com.netflix.hystrix.HystrixCommandProperties;
5+
import com.netflix.hystrix.HystrixObservableCommand;
6+
import ratpack.http.client.HttpClient;
7+
import ratpack.rx.RxRatpack;
8+
import rx.Observable;
9+
10+
import java.net.URI;
11+
12+
/**
13+
* @author aiet
14+
*/
15+
public class HystrixReactiveHttpCommand extends HystrixObservableCommand<String> {
16+
17+
private HttpClient httpClient;
18+
private URI uri;
19+
20+
HystrixReactiveHttpCommand(HttpClient httpClient, URI uri, int timeoutMillis) {
21+
super(Setter
22+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix-ratpack-reactive"))
23+
.andCommandPropertiesDefaults(HystrixCommandProperties
24+
.Setter()
25+
.withExecutionTimeoutInMilliseconds(timeoutMillis)));
26+
this.httpClient = httpClient;
27+
this.uri = uri;
28+
}
29+
30+
@Override
31+
protected Observable<String> construct() {
32+
return RxRatpack.observe(httpClient
33+
.get(uri, requestSpec -> requestSpec.headers(mutableHeaders -> mutableHeaders.add("User-Agent", "Baeldung HttpClient")))
34+
.map(receivedResponse -> receivedResponse
35+
.getBody()
36+
.getText()));
37+
}
38+
39+
@Override
40+
protected Observable<String> resumeWithFallback() {
41+
return Observable.just("eugenp's reactive fallback profile");
42+
}
43+
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.hystrix;
2+
3+
import com.netflix.hystrix.HystrixCommand;
4+
import com.netflix.hystrix.HystrixCommandGroupKey;
5+
import com.netflix.hystrix.HystrixCommandProperties;
6+
import org.apache.http.client.config.RequestConfig;
7+
import org.apache.http.client.methods.HttpGet;
8+
import org.apache.http.impl.client.HttpClientBuilder;
9+
import org.apache.http.message.BasicHeader;
10+
import org.apache.http.util.EntityUtils;
11+
12+
import java.net.URI;
13+
import java.util.Collections;
14+
15+
/**
16+
* @author aiet
17+
*/
18+
public class HystrixSyncHttpCommand extends HystrixCommand<String> {
19+
20+
private URI uri;
21+
private RequestConfig requestConfig;
22+
23+
HystrixSyncHttpCommand(URI uri, int timeoutMillis) {
24+
super(Setter
25+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix-ratpack-sync"))
26+
.andCommandPropertiesDefaults(HystrixCommandProperties
27+
.Setter()
28+
.withExecutionTimeoutInMilliseconds(timeoutMillis)));
29+
requestConfig = RequestConfig
30+
.custom()
31+
.setSocketTimeout(timeoutMillis)
32+
.setConnectTimeout(timeoutMillis)
33+
.setConnectionRequestTimeout(timeoutMillis)
34+
.build();
35+
this.uri = uri;
36+
}
37+
38+
@Override
39+
protected String run() throws Exception {
40+
HttpGet request = new HttpGet(uri);
41+
return EntityUtils.toString(HttpClientBuilder
42+
.create()
43+
.setDefaultRequestConfig(requestConfig)
44+
.setDefaultHeaders(Collections.singleton(new BasicHeader("User-Agent", "Baeldung Blocking HttpClient")))
45+
.build()
46+
.execute(request)
47+
.getEntity());
48+
}
49+
50+
@Override
51+
protected String getFallback() {
52+
return "eugenp's sync fallback profile";
53+
}
54+
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.hystrix;
2+
3+
import ratpack.guice.Guice;
4+
import ratpack.http.client.HttpClient;
5+
import ratpack.hystrix.HystrixMetricsEventStreamHandler;
6+
import ratpack.hystrix.HystrixModule;
7+
import ratpack.server.RatpackServer;
8+
9+
import java.net.URI;
10+
11+
public class RatpackHystrixApp {
12+
13+
public static void main(String[] args) throws Exception {
14+
final int timeout = Integer.valueOf(System.getProperty("ratpack.hystrix.timeout"));
15+
final URI eugenGithubProfileUri = new URI("https://api.github.com/users/eugenp");
16+
17+
RatpackServer.start(server -> server
18+
.registry(Guice.registry(bindingsSpec -> bindingsSpec.module(new HystrixModule().sse())))
19+
.handlers(chain -> chain
20+
.get("rx", ctx -> new HystrixReactiveHttpCommand(ctx.get(HttpClient.class), eugenGithubProfileUri, timeout)
21+
.toObservable()
22+
.subscribe(ctx::render))
23+
.get("async", ctx -> ctx.render(new HystrixAsyncHttpCommand(eugenGithubProfileUri, timeout)
24+
.queue()
25+
.get()))
26+
.get("sync", ctx -> ctx.render(new HystrixSyncHttpCommand(eugenGithubProfileUri, timeout).execute()))
27+
.get("hystrix", new HystrixMetricsEventStreamHandler())));
28+
29+
}
30+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.hystrix;
2+
3+
import org.junit.AfterClass;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import ratpack.test.MainClassApplicationUnderTest;
7+
8+
import static org.hamcrest.CoreMatchers.containsString;
9+
import static org.junit.Assert.assertThat;
10+
11+
/**
12+
* @author aiet
13+
*/
14+
public class RatpackHystrixAppFallbackLiveTest {
15+
16+
static MainClassApplicationUnderTest appUnderTest;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
System.setProperty("ratpack.hystrix.timeout", "10");
21+
appUnderTest = new MainClassApplicationUnderTest(RatpackHystrixApp.class);
22+
}
23+
24+
@Test
25+
public void whenFetchReactive_thenGotFallbackProfile() {
26+
assertThat(appUnderTest
27+
.getHttpClient()
28+
.getText("rx"), containsString("reactive fallback profile"));
29+
}
30+
31+
@Test
32+
public void whenFetchAsync_thenGotFallbackProfile() {
33+
assertThat(appUnderTest
34+
.getHttpClient()
35+
.getText("async"), containsString("async fallback profile"));
36+
}
37+
38+
@Test
39+
public void whenFetchSync_thenGotFallbackProfile() {
40+
assertThat(appUnderTest
41+
.getHttpClient()
42+
.getText("sync"), containsString("sync fallback profile"));
43+
}
44+
45+
@AfterClass
46+
public static void clean() {
47+
appUnderTest.close();
48+
}
49+
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.hystrix;
2+
3+
import org.junit.AfterClass;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import ratpack.test.MainClassApplicationUnderTest;
7+
8+
import static org.hamcrest.CoreMatchers.containsString;
9+
import static org.junit.Assert.assertThat;
10+
11+
/**
12+
* @author aiet
13+
*/
14+
public class RatpackHystrixAppLiveTest {
15+
16+
static MainClassApplicationUnderTest appUnderTest;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
System.setProperty("ratpack.hystrix.timeout", "5000");
21+
appUnderTest = new MainClassApplicationUnderTest(RatpackHystrixApp.class);
22+
}
23+
24+
@Test
25+
public void whenFetchReactive_thenGotEugenProfile() {
26+
assertThat(appUnderTest
27+
.getHttpClient()
28+
.getText("rx"), containsString("www.baeldung.com"));
29+
}
30+
31+
@Test
32+
public void whenFetchAsync_thenGotEugenProfile() {
33+
assertThat(appUnderTest
34+
.getHttpClient()
35+
.getText("async"), containsString("www.baeldung.com"));
36+
}
37+
38+
@Test
39+
public void whenFetchSync_thenGotEugenProfile() {
40+
assertThat(appUnderTest
41+
.getHttpClient()
42+
.getText("sync"), containsString("www.baeldung.com"));
43+
}
44+
45+
@AfterClass
46+
public static void clean() {
47+
appUnderTest.close();
48+
}
49+
50+
}

0 commit comments

Comments
 (0)