Search in sources :

Example 11 with HttpResourceGroup

use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.

the class RibbonTest method testObserve.

@Test
public void testObserve() throws IOException, InterruptedException {
    MockWebServer server = new MockWebServer();
    String content = "Hello world";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content));
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content));
    server.play();
    HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withMaxAutoRetriesNextServer(3).withReadTimeout(300000).withConfigurationBasedServerList("localhost:12345, localhost:10092, localhost:" + server.getPort()));
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("/").withMethod("GET").build();
    RibbonRequest<ByteBuf> request = template.requestBuilder().build();
    Observable<ByteBuf> result = request.observe();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> fromCommand = new AtomicReference<String>();
    // We need to wait until the response is received and processed by event loop
    // and make sure that subscribing to it again will not cause ByteBuf ref count issue
    result.toBlocking().last();
    result.subscribe(new Action1<ByteBuf>() {

        @Override
        public void call(ByteBuf t1) {
            try {
                fromCommand.set(t1.toString(Charset.defaultCharset()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            latch.countDown();
        }
    });
    latch.await();
    assertEquals(content, fromCommand.get());
    Observable<RibbonResponse<Observable<ByteBuf>>> metaResult = request.withMetadata().observe();
    String result2 = "";
    // We need to wait until the response is received and processed by event loop
    // and make sure that subscribing to it again will not cause ByteBuf ref count issue
    metaResult.toBlocking().last();
    result2 = metaResult.flatMap(new Func1<RibbonResponse<Observable<ByteBuf>>, Observable<ByteBuf>>() {

        @Override
        public Observable<ByteBuf> call(RibbonResponse<Observable<ByteBuf>> t1) {
            return t1.content();
        }
    }).map(new Func1<ByteBuf, String>() {

        @Override
        public String call(ByteBuf t1) {
            return t1.toString(Charset.defaultCharset());
        }
    }).toBlocking().single();
    assertEquals(content, result2);
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Observable(rx.Observable) MockWebServer(com.google.mockwebserver.MockWebServer) Func1(rx.functions.Func1) Test(org.junit.Test)

Example 12 with HttpResourceGroup

use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.

the class RibbonExamples method main.

public static void main(String[] args) {
    HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient");
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("GetUser").withResponseValidator(new ResponseValidator<HttpClientResponse<ByteBuf>>() {

        @Override
        public void validate(HttpClientResponse<ByteBuf> response) throws UnsuccessfulResponseException, ServerError {
            if (response.getStatus().code() >= 500) {
                throw new ServerError("Unexpected response");
            }
        }
    }).withFallbackProvider(new FallbackHandler<ByteBuf>() {

        @Override
        public Observable<ByteBuf> getFallback(HystrixInvokableInfo<?> t1, Map<String, Object> vars) {
            return Observable.empty();
        }
    }).withHystrixProperties((HystrixObservableCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("mygroup")).andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(2000)))).withUriTemplate("/{id}").build();
    template.requestBuilder().withRequestProperty("id", 1).build().execute();
    // example showing the use case of getting the entity with Hystrix meta data
    template.requestBuilder().withRequestProperty("id", 3).build().withMetadata().observe().flatMap(new Func1<RibbonResponse<Observable<ByteBuf>>, Observable<String>>() {

        @Override
        public Observable<String> call(RibbonResponse<Observable<ByteBuf>> t1) {
            if (t1.getHystrixInfo().isResponseFromFallback()) {
                return Observable.empty();
            }
            return t1.content().map(new Func1<ByteBuf, String>() {

                @Override
                public String call(ByteBuf t1) {
                    return t1.toString();
                }
            });
        }
    });
}
Also used : HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) FallbackHandler(com.netflix.ribbon.hystrix.FallbackHandler) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) Func1(rx.functions.Func1) Map(java.util.Map) HystrixInvokableInfo(com.netflix.hystrix.HystrixInvokableInfo)

Example 13 with HttpResourceGroup

use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.

the class ProxyHttpResourceGroupFactory method createResourceGroup.

public HttpResourceGroup createResourceGroup() {
    Class<? extends HttpResourceGroup> resourceClass = classTemplate.getResourceGroupClass();
    if (resourceClass != null) {
        return Utils.newInstance(resourceClass);
    } else {
        String name = classTemplate.getResourceGroupName();
        if (name == null) {
            name = classTemplate.getClientInterface().getSimpleName();
        }
        HttpResourceGroup.Builder builder = httpResourceGroupFactory.createHttpResourceGroupBuilder(name);
        for (AnnotationProcessor processor : processors.getProcessors()) {
            processor.process(name, builder, httpResourceGroupFactory, classTemplate.getClientInterface());
        }
        return builder.build();
    }
}
Also used : HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) AnnotationProcessor(com.netflix.ribbon.proxy.processor.AnnotationProcessor)

Aggregations

HttpResourceGroup (com.netflix.ribbon.http.HttpResourceGroup)13 ByteBuf (io.netty.buffer.ByteBuf)11 Test (org.junit.Test)10 MockResponse (com.google.mockwebserver.MockResponse)6 MockWebServer (com.google.mockwebserver.MockWebServer)6 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Map (java.util.Map)5 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Observable (rx.Observable)4 Func1 (rx.functions.Func1)4 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 HystrixInvokableInfo (com.netflix.hystrix.HystrixInvokableInfo)2 FallbackHandler (com.netflix.ribbon.hystrix.FallbackHandler)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 IClientConfig (com.netflix.client.config.IClientConfig)1 HystrixRequestContext (com.netflix.hystrix.strategy.concurrency.HystrixRequestContext)1