提交 4fbdadf7 编写于 作者: A Allen Wang

Modified examples.

上级 18772728
package com.netflix.ribbon.examples.netty.http;
import com.netflix.ribbon.examples.ExampleAppWithLocalResource;
public class EntityDeserializationExample extends ExampleAppWithLocalResource {
@Override
public void run() throws Exception {
/*
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person");
NettyHttpClient observableClient = NettyHttpClientBuilder.newBuilder()
.build();
// deserialize using the default Jackson deserializer
observableClient.createEntityObservable("localhost", port, request, TypeDef.fromClass(Person.class), null).toBlockingObservable().forEach(new Action1<Person>() {
@Override
public void call(Person t1) {
try {
System.out.println("Person: " + t1);
} catch (Exception e) {
e.printStackTrace();
}
}
});
// deserialize as Map using the default Jackson deserializer
observableClient.createEntityObservable("localhost", port, request, new TypeDef<Map<String, Object>>(){}, null)
.toBlockingObservable()
.forEach(new Action1<Map<String, Object>>() {
@Override
public void call(Map<String, Object> t1) {
try {
System.out.println("Map: " + t1);
} catch (Exception e) {
e.printStackTrace();
}
}
});
// deserialize using Xml deserializer
IClientConfig requestConfig = DefaultClientConfigImpl.getEmptyConfig()
.setPropertyWithType(IClientConfigKey.CommonKeys.Deserializer, XmlCodec.<Person>getInstance());
request = HttpRequest.createGet(SERVICE_URI + "testAsync/getXml");
observableClient.createEntityObservable("localhost", port, request, TypeDef.fromClass(Person.class), requestConfig)
.toBlockingObservable()
.forEach(new Action1<Person>() {
@Override
public void call(Person t1) {
try {
System.out.println("Person: " + t1);
} catch (Exception e) {
e.printStackTrace();
}
}
});
// URI does not exist, will get UnexpectedResponseException
request = HttpRequest.createGet(SERVICE_URI + "testAsync/NotFound");
observableClient.createEntityObservable("localhost", port, request, TypeDef.fromClass(Person.class), null)
.subscribe(new Action1<Person>() {
@Override
public void call(Person t1) {
try {
System.out.println("Person: " + t1);
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable t1) {
if (t1 instanceof UnexpectedHttpResponseException) {
UnexpectedHttpResponseException ex = (UnexpectedHttpResponseException) t1;
System.out.println(ex.getStatusCode());
}
}
});
Thread.sleep(2000);
*/
}
public static void main(String[] args) throws Exception {
new EntityDeserializationExample().runApp();
}
}
package com.netflix.ribbon.examples.netty.http;
import com.netflix.ribbon.examples.ExampleAppWithLocalResource;
public class HttpResponseDeserialization extends ExampleAppWithLocalResource {
@Override
public void run() throws Exception {
// TODO Auto-generated method stub
}
/*
@Override
public void run() throws Exception {
HttpRequest<ByteBuf> request = HttpRequest.createGet(SERVICE_URI + "testAsync/getXml");
NettyHttpClient observableClient = NettyHttpClientBuilder.newBuilder()
.build();
observableClient.createFullHttpResponseObservable("localhost", port, request)
.flatMap(new Func1<HttpResponse<ByteBuf>, Observable<Person>>() {
@Override
public Observable<Person> call(HttpResponse<ByteBuf> t1) {
return t1.getContent().map(new Func1<ByteBuf, Person>() {
@Override
public Person call(ByteBuf t1) {
try {
return XmlCodec.<Person>getInstance().deserialize(new ByteBufInputStream(t1), TypeDef.fromClass(Person.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
}).toBlockingObservable()
.forEach(new Action1<Person>() {
@Override
public void call(Person t1) {
try {
System.out.println(t1);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void main(String[] args) throws Exception {
new HttpResponseDeserialization().runApp();
}
*/
}
package com.netflix.ribbon.examples.netty.http; package com.netflix.ribbon.examples.netty.http;
import io.netty.buffer.ByteBuf;
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import io.reactivex.netty.protocol.http.client.HttpClientResponse;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import rx.Observer;
import com.google.common.collect.Lists;
import com.netflix.client.netty.http.NettyHttpClient;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.LoadBalancerBuilder;
import com.netflix.loadbalancer.Server;
public class LoadBalancingExample { public class LoadBalancingExample {
/*
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
NettyHttpLoadBalancingClient client = NettyHttpClientBuilder.newBuilder() List<Server> servers = Lists.newArrayList(new Server("www.google.com:80"), new Server("www.examples.com:80"), new Server("www.wikipedia.org:80"));
.withFixedServerList(Lists.newArrayList(new Server("www.google.com:80"), new Server("www.microsoft.com:80"), new Server("www.yahoo.com:80"))) BaseLoadBalancer lb = LoadBalancerBuilder.newBuilder()
.build(); .buildFixedServerListLoadBalancer(servers);
final CountDownLatch latch = new CountDownLatch(3);
Observer<HttpResponse<ByteBuf>> observer = new Observer<HttpResponse<ByteBuf>>() { NettyHttpClient<ByteBuf, ByteBuf> client = NettyHttpClient.createDefaultHttpClient(lb);
final CountDownLatch latch = new CountDownLatch(servers.size());
Observer<HttpClientResponse<ByteBuf>> observer = new Observer<HttpClientResponse<ByteBuf>>() {
@Override @Override
public void onCompleted() { public void onCompleted() {
} }
...@@ -19,20 +35,16 @@ public class LoadBalancingExample { ...@@ -19,20 +35,16 @@ public class LoadBalancingExample {
} }
@Override @Override
public void onNext(HttpResponse<ByteBuf> args) { public void onNext(HttpClientResponse<ByteBuf> args) {
latch.countDown(); latch.countDown();
System.err.println("Got response: " + args.getStatus()); System.out.println("Got response: " + args.getStatus());
} }
}; };
for (int i = 0; i < 3; i++) { for (int i = 0; i < servers.size(); i++) {
// The request is not reusable in RxNetty as its state will be altered. Hence create new HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/");
// request for each server before the issue is addressed in RxNetty client.submit(request).subscribe(observer);
HttpRequest<ByteBuf> request = HttpRequest.createGet("/");
client.createFullHttpResponseObservable(request, null, null).subscribe(observer);
} }
latch.await(); latch.await();
NettyHttpLoadBalancingClient lbClient = (NettyHttpLoadBalancingClient) client; System.out.println(lb.getLoadBalancerStats());
System.out.println(((AbstractLoadBalancer) lbClient.getLoadBalancer()).getLoadBalancerStats());
} }
*/
} }
package com.netflix.ribbon.examples.netty.http;
import com.netflix.ribbon.examples.ExampleAppWithLocalResource;
public class PostExample extends ExampleAppWithLocalResource {
@Override
public void run() throws Exception {
/*
Person myPerson = new Person("netty", 5);
HttpRequest<ByteBuf> request = HttpRequest.createPost(SERVICE_URI + "testAsync/person").withHeader("Content-type", "application/json")
.withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), myPerson, null));
NettyHttpClient observableClient = NettyHttpClientBuilder.newBuilder().withClientConfig(DefaultClientConfigImpl.getClientConfigWithDefaultValues()
.setPropertyWithType(IClientConfigKey.CommonKeys.ReadTimeout, 10000)
.setPropertyWithType(IClientConfigKey.CommonKeys.ConnectTimeout, 2000))
.build();
observableClient.createEntityObservable("localhost", port, request, TypeDef.fromClass(Person.class), null).toBlockingObservable().forEach(new Action1<Person>() {
@Override
public void call(Person t1) {
try {
System.out.println(t1);
} catch (Exception e) { // NOPMD
}
}
}); */
}
public static void main(String[] args) throws Exception {
new PostExample().runApp();
}
}
package com.netflix.ribbon.examples.netty.http;
import com.netflix.ribbon.examples.ExampleAppWithLocalResource;
public class SeverSentEventExample extends ExampleAppWithLocalResource {
@Override
public void run() throws Exception {
/*
// Get the events and parse each data line using Jackson deserializer
IClientConfig overrideConfig = new DefaultClientConfigImpl().setPropertyWithType(CommonClientConfigKey.Deserializer, JacksonCodec.getInstance());
HttpRequest<ByteBuf> request = HttpRequest.createGet(SERVICE_URI + "testAsync/personStream");
NettyHttpClient observableClient = NettyHttpClientBuilder.newBuilder().build();
final List<Person> result = Lists.newArrayList();
observableClient.createServerSentEventEntityObservable("localhost", port, request, TypeDef.fromClass(Person.class), overrideConfig)
.subscribe(new Action1<ServerSentEventWithEntity<Person>>() {
@Override
public void call(ServerSentEventWithEntity<Person> t1) {
// System.out.println(t1);
result.add(t1.getEntity());
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable t1) {
t1.printStackTrace();
}
});
Thread.sleep(2000);
System.out.println(result);
// Get the events as raw string
request = HttpRequest.createGet(SERVICE_URI + "testAsync/stream");
observableClient.createServerSentEventObservable("localhost", port, request)
.flatMap(new Func1<HttpResponse<ServerSentEvent>, Observable<ServerSentEvent>>() {
@Override
public Observable<ServerSentEvent> call(
HttpResponse<ServerSentEvent> t1) {
return t1.getContent();
}
}).toBlockingObservable()
.forEach(new Action1<ServerSentEvent>(){
@Override
public void call(ServerSentEvent t1) {
System.out.println(t1);
}
}); */
}
public static void main(String[] args) throws Exception {
new SeverSentEventExample().runApp();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册