Skip to content

Instantly share code, notes, and snippets.

@matlockx
Created March 5, 2016 20:24
Show Gist options
  • Save matlockx/36f888c1ab1f52d56a76 to your computer and use it in GitHub Desktop.
Save matlockx/36f888c1ab1f52d56a76 to your computer and use it in GitHub Desktop.
package io.lutterome.main;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig;
import com.netflix.ribbon.ClientOptions;
import com.netflix.ribbon.Ribbon;
import com.netflix.ribbon.http.HttpResourceGroup;
import com.netflix.ribbon.transport.netty.RibbonTransport;
import com.netflix.ribbon.transport.netty.http.LoadBalancingHttpClient;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.internal.SystemPropertyUtil;
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import io.reactivex.netty.protocol.http.client.HttpClientResponse;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import rx.Observable;
import rx.functions.Func1;
public class Main {
public static void main(String[] args) {
IClientConfig clientConfig = IClientConfig.Builder.newBuilder("ribbonTest").build();
clientConfig.set(CommonClientConfigKey.ListOfServers, "docker:2080");
LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(clientConfig);
HttpResourceGroup hrg = Ribbon.createHttpResourceGroup("test", ClientOptions.create().withConfigurationBasedServerList
("docker:2080"));
HttpClientRequest request = HttpClientRequest.create(HttpMethod.GET, "/crossword/info")
.withHeader("ContentType", "application/json");
Observable<HttpClientResponse<ByteBuf>> response = client
.submit(request);
response
.flatMap(httpClientResponse -> {
if (!httpClientResponse.getStatus().equals(HttpResponseStatus.OK)) {
throw new RuntimeException("booom");
}
return httpClientResponse.getContent();
})
.map(r -> r.toString(Charset.defaultCharset()))
.doOnError(throwable -> System.out.println("Something bad happened."))
.onErrorReturn(throwable -> "Got and error: " + throwable.getMessage())
.toBlocking().forEach(s -> System.out.println(s));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment