#BAEL-1025 (#2445)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
706b3d6c67
commit
802c0b09e0
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandProperties;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class HystrixAsyncHttpCommand extends HystrixCommand<String> {
|
||||
|
||||
private URI uri;
|
||||
private RequestConfig requestConfig;
|
||||
|
||||
HystrixAsyncHttpCommand(URI uri, int timeoutMillis) {
|
||||
super(Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix-ratpack-async"))
|
||||
.andCommandPropertiesDefaults(HystrixCommandProperties
|
||||
.Setter()
|
||||
.withExecutionTimeoutInMilliseconds(timeoutMillis)));
|
||||
requestConfig = RequestConfig
|
||||
.custom()
|
||||
.setSocketTimeout(timeoutMillis)
|
||||
.setConnectTimeout(timeoutMillis)
|
||||
.setConnectionRequestTimeout(timeoutMillis)
|
||||
.build();
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String run() throws Exception {
|
||||
return EntityUtils.toString(HttpClientBuilder
|
||||
.create()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setDefaultHeaders(Collections.singleton(new BasicHeader("User-Agent", "Baeldung Blocking HttpClient")))
|
||||
.build()
|
||||
.execute(new HttpGet(uri))
|
||||
.getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFallback() {
|
||||
return "eugenp's async fallback profile";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandProperties;
|
||||
import com.netflix.hystrix.HystrixObservableCommand;
|
||||
import ratpack.http.client.HttpClient;
|
||||
import ratpack.rx.RxRatpack;
|
||||
import rx.Observable;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class HystrixReactiveHttpCommand extends HystrixObservableCommand<String> {
|
||||
|
||||
private HttpClient httpClient;
|
||||
private URI uri;
|
||||
|
||||
HystrixReactiveHttpCommand(HttpClient httpClient, URI uri, int timeoutMillis) {
|
||||
super(Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix-ratpack-reactive"))
|
||||
.andCommandPropertiesDefaults(HystrixCommandProperties
|
||||
.Setter()
|
||||
.withExecutionTimeoutInMilliseconds(timeoutMillis)));
|
||||
this.httpClient = httpClient;
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Observable<String> construct() {
|
||||
return RxRatpack.observe(httpClient
|
||||
.get(uri, requestSpec -> requestSpec.headers(mutableHeaders -> mutableHeaders.add("User-Agent", "Baeldung HttpClient")))
|
||||
.map(receivedResponse -> receivedResponse
|
||||
.getBody()
|
||||
.getText()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Observable<String> resumeWithFallback() {
|
||||
return Observable.just("eugenp's reactive fallback profile");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandProperties;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class HystrixSyncHttpCommand extends HystrixCommand<String> {
|
||||
|
||||
private URI uri;
|
||||
private RequestConfig requestConfig;
|
||||
|
||||
HystrixSyncHttpCommand(URI uri, int timeoutMillis) {
|
||||
super(Setter
|
||||
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix-ratpack-sync"))
|
||||
.andCommandPropertiesDefaults(HystrixCommandProperties
|
||||
.Setter()
|
||||
.withExecutionTimeoutInMilliseconds(timeoutMillis)));
|
||||
requestConfig = RequestConfig
|
||||
.custom()
|
||||
.setSocketTimeout(timeoutMillis)
|
||||
.setConnectTimeout(timeoutMillis)
|
||||
.setConnectionRequestTimeout(timeoutMillis)
|
||||
.build();
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String run() throws Exception {
|
||||
HttpGet request = new HttpGet(uri);
|
||||
return EntityUtils.toString(HttpClientBuilder
|
||||
.create()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setDefaultHeaders(Collections.singleton(new BasicHeader("User-Agent", "Baeldung Blocking HttpClient")))
|
||||
.build()
|
||||
.execute(request)
|
||||
.getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFallback() {
|
||||
return "eugenp's sync fallback profile";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.hystrix;
|
||||
|
||||
import ratpack.guice.Guice;
|
||||
import ratpack.http.client.HttpClient;
|
||||
import ratpack.hystrix.HystrixMetricsEventStreamHandler;
|
||||
import ratpack.hystrix.HystrixModule;
|
||||
import ratpack.server.RatpackServer;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
public class RatpackHystrixApp {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
final int timeout = Integer.valueOf(System.getProperty("ratpack.hystrix.timeout"));
|
||||
final URI eugenGithubProfileUri = new URI("https://api.github.com/users/eugenp");
|
||||
|
||||
RatpackServer.start(server -> server
|
||||
.registry(Guice.registry(bindingsSpec -> bindingsSpec.module(new HystrixModule().sse())))
|
||||
.handlers(chain -> chain
|
||||
.get("rx", ctx -> new HystrixReactiveHttpCommand(ctx.get(HttpClient.class), eugenGithubProfileUri, timeout)
|
||||
.toObservable()
|
||||
.subscribe(ctx::render))
|
||||
.get("async", ctx -> ctx.render(new HystrixAsyncHttpCommand(eugenGithubProfileUri, timeout)
|
||||
.queue()
|
||||
.get()))
|
||||
.get("sync", ctx -> ctx.render(new HystrixSyncHttpCommand(eugenGithubProfileUri, timeout).execute()))
|
||||
.get("hystrix", new HystrixMetricsEventStreamHandler())));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user