Search in sources :

Example 36 with TransportClientAdapter

use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.

the class SimpleLoadBalancerStrawMan method main.

public static void main(String[] args) throws URISyntaxException, ServiceUnavailableException {
    // define the load balancing strategies that we support (round robin, etc)
    Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<>();
    loadBalancerStrategyFactories.put("rr", new RandomLoadBalancerStrategyFactory());
    loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
    // define the clients that we support (http, etc)
    Map<String, TransportClientFactory> clientFactories = new HashMap<>();
    clientFactories.put("http", new HttpClientFactory.Builder().build());
    // listen for service updates (could be a glu discovery client, zk discovery client,
    // config discovery client, etc)
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    MockStore<ServiceProperties> serviceRegistry = new MockStore<>();
    MockStore<ClusterProperties> clusterRegistry = new MockStore<>();
    MockStore<UriProperties> uriRegistry = new MockStore<>();
    SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriRegistry, clusterRegistry, serviceRegistry, clientFactories, loadBalancerStrategyFactories);
    // create the load balancer
    SimpleLoadBalancer loadBalancer = new SimpleLoadBalancer(state, executorService);
    final TransportClient tc = loadBalancer.getClient(new URIRequest("d2://browsemaps/52"), new RequestContext());
    final Client c = new TransportClientAdapter(tc, true);
    c.restRequest(null);
}
Also used : HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) MockStore(com.linkedin.d2.discovery.stores.mock.MockStore) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RequestContext(com.linkedin.r2.message.RequestContext) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) Client(com.linkedin.r2.transport.common.Client) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) URIRequest(com.linkedin.d2.balancer.util.URIRequest) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties)

Example 37 with TransportClientAdapter

use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.

the class TestMIMEChainingMultipleSources method setup.

@BeforeClass
public void setup() throws IOException {
    _scheduledExecutorService = Executors.newScheduledThreadPool(30);
    _clientFactory = new HttpClientFactory.Builder().build();
    _client = new TransportClientAdapter(_clientFactory.getClient(Collections.<String, String>emptyMap()));
    _server_A_client = new TransportClientAdapter(_clientFactory.getClient(Collections.<String, String>emptyMap()));
    final HttpServerFactory httpServerFactory = new HttpServerFactory();
    final ServerARequestHandler serverARequestHandler = new ServerARequestHandler();
    final TransportDispatcher serverATransportDispatcher = new TransportDispatcherBuilder().addStreamHandler(SERVER_A_URI, serverARequestHandler).build();
    final ServerBRequestHandler serverBRequestHandler = new ServerBRequestHandler();
    final TransportDispatcher serverBTransportDispatcher = new TransportDispatcherBuilder().addStreamHandler(SERVER_B_URI, serverBRequestHandler).build();
    _serverA = httpServerFactory.createServer(PORT_SERVER_A, serverATransportDispatcher, true);
    _serverB = httpServerFactory.createServer(PORT_SERVER_B, serverBTransportDispatcher, true);
    _serverA.start();
    _serverB.start();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) BeforeClass(org.testng.annotations.BeforeClass)

Example 38 with TransportClientAdapter

use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.

the class TestUnstructuredDataClient method init.

@BeforeClass
public void init() throws Exception {
    super.init();
    _clientFactory = new HttpClientFactory.Builder().build();
    _client = new TransportClientAdapter(_clientFactory.getClient(Collections.emptyMap()), true);
}
Also used : TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) BeforeClass(org.testng.annotations.BeforeClass)

Example 39 with TransportClientAdapter

use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.

the class RestLiFortunesClient method main.

/**
 * This stand-alone app demos the client-side Pegasus API.
 * To see the demo, run RestLiFortuneServer, then start the client
 */
public static void main(String[] args) throws Exception {
    // Create an HttpClient and wrap it in an abstraction layer
    final HttpClientFactory http = new HttpClientFactory();
    final Client r2Client = new TransportClientAdapter(http.getClient(Collections.<String, String>emptyMap()));
    // Create a RestClient to talk to localhost:8080
    RestClient restClient = new RestClient(r2Client, "http://localhost:8080/");
    // Generate a random ID for a fortune cookie, in the range 0-5
    long fortuneId = (long) (Math.random() * 5);
    // Construct a request for the specified fortune
    FortunesGetBuilder getBuilder = _fortuneBuilder.get();
    Request<Fortune> getReq = getBuilder.id(fortuneId).build();
    // Send the request and wait for a response
    final ResponseFuture<Fortune> getFuture = restClient.sendRequest(getReq);
    final Response<Fortune> resp = getFuture.getResponse();
    // Print the response
    System.out.println(resp.getEntity().getFortune());
    // shutdown
    restClient.shutdown(new FutureCallback<None>());
    http.shutdown(new FutureCallback<None>());
}
Also used : RestClient(com.linkedin.restli.client.RestClient) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) None(com.linkedin.common.util.None)

Example 40 with TransportClientAdapter

use of com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter in project rest.li by linkedin.

the class RestLiExampleBasicClient method main.

/**
 * This is a stand-alone app to demo the use of client-side Pegasus API. To run in,
 * com.linkedin.restli.example.RestLiExamplesServer has to be running.
 *
 * The only argument is the path to the resource on the photo server, e.g. /photos/1
 */
public static void main(String[] args) throws Exception {
    // create HTTP Netty client with default properties
    final HttpClientFactory http = new HttpClientFactory.Builder().build();
    final TransportClient transportClient = http.getClient(Collections.<String, String>emptyMap());
    // create an abstraction layer over the actual client, which supports both REST and RPC
    final Client r2Client = new TransportClientAdapter(transportClient);
    // REST client wrapper that simplifies the interface
    final StringBuilder serverUrlBuilder = new StringBuilder("http://").append(SERVER_HOSTNAME).append(":").append(SERVER_PORT).append("/");
    final RestClient restClient = new RestClient(r2Client, serverUrlBuilder.toString());
    final RestLiExampleBasicClient photoClient = new RestLiExampleBasicClient(restClient);
    String pathInfo = args.length == 0 ? "" : args[0];
    photoClient.sendRequest(pathInfo, new PrintWriter(System.out));
    photoClient.shutdown();
    http.shutdown(new FutureCallback<>());
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestClient(com.linkedin.restli.client.RestClient) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) PrintWriter(java.io.PrintWriter)

Aggregations

TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)42 HashMap (java.util.HashMap)21 Client (com.linkedin.r2.transport.common.Client)20 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)17 Test (org.testng.annotations.Test)15 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)14 RestRequest (com.linkedin.r2.message.rest.RestRequest)12 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)12 None (com.linkedin.common.util.None)10 FutureCallback (com.linkedin.common.callback.FutureCallback)9 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)9 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)9 BeforeClass (org.testng.annotations.BeforeClass)9 TransportDispatcherBuilder (com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder)8 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)8 RestClient (com.linkedin.restli.client.RestClient)8 URI (java.net.URI)8 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)7 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)7 CompressionConfig (com.linkedin.r2.filter.CompressionConfig)6