Java Code Examples for com.baidu.brpc.client.RpcClientOptions#setLoadBalanceType()

The following examples show how to use com.baidu.brpc.client.RpcClientOptions#setLoadBalanceType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Client.java    From rpc-benchmark with Apache License 2.0 6 votes vote down vote up
public Client() {
    RpcClientOptions clientOption = new RpcClientOptions();
    clientOption.setProtocolType(com.baidu.brpc.protocol.Options.ProtocolType.PROTOCOL_HTTP_JSON_VALUE);
    clientOption.setWriteTimeoutMillis(1000);
    clientOption.setReadTimeoutMillis(1000);
    clientOption.setMaxTotalConnections(1000);
    clientOption.setMinIdleConnections(10);
    clientOption.setLoadBalanceType(LoadBalanceStrategy.LOAD_BALANCE_RANDOM);
    clientOption.setCompressType(com.baidu.brpc.protocol.Options.CompressType.COMPRESS_TYPE_ZLIB);


    String serviceUrl = "list://benchmark-server:8002";

    this.rpcClient = new RpcClient(serviceUrl, clientOption);
    this.userService = BrpcProxy.getProxy(rpcClient, UserService.class);
}
 
Example 2
Source File: LoadBalanceTest.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testRandomStrategy() {
    RpcClientOptions clientOption = RpcOptionsUtils.getRpcClientOptions();
    clientOption.setLoadBalanceType(LoadBalanceStrategy.LOAD_BALANCE_RANDOM);
    RpcClient rpcClient = new RpcClient(serviceUrl, clientOption);
    final Echo.EchoRequest request = Echo.EchoRequest.newBuilder().setMessage("hello").build();
    final EchoService echoService = BrpcProxy.getProxy(rpcClient, EchoService.class);
    for (int i = 0; i < 10; i++) {
        echoService.echo(request);
    }
    rpcClient.stop();
}
 
Example 3
Source File: LoadBalanceTest.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoundRobinStrategy() {
    RpcClientOptions clientOption = RpcOptionsUtils.getRpcClientOptions();
    clientOption.setLoadBalanceType(LoadBalanceStrategy.LOAD_BALANCE_ROUND_ROBIN);
    RpcClient rpcClient = new RpcClient(serviceUrl, clientOption, null);
    final Echo.EchoRequest request = Echo.EchoRequest.newBuilder().setMessage("hello").build();
    final EchoService echoService = BrpcProxy.getProxy(rpcClient, EchoService.class);
    for (int i = 0; i < 10; i++) {
        echoService.echo(request);
    }
    rpcClient.stop();
}
 
Example 4
Source File: LoadBalanceTest.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testWeightStrategy() {
    RpcClientOptions clientOption = RpcOptionsUtils.getRpcClientOptions();
    clientOption.setLoadBalanceType(LoadBalanceStrategy.LOAD_BALANCE_WEIGHT);
    RpcClient rpcClient = new RpcClient(serviceUrl, clientOption, null);
    final Echo.EchoRequest request = Echo.EchoRequest.newBuilder().setMessage("hello").build();
    final EchoService echoService = BrpcProxy.getProxy(rpcClient, EchoService.class);
    for (int i = 0; i < 10; i++) {
        echoService.echo(request);
    }
    rpcClient.stop();
}
 
Example 5
Source File: LoadBalanceTest.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testFairStrategy() {
    RpcClientOptions clientOption = RpcOptionsUtils.getRpcClientOptions();
    clientOption.setLatencyWindowSizeOfFairLoadBalance(10);
    clientOption.setLoadBalanceType(LoadBalanceStrategy.LOAD_BALANCE_FAIR);
    RpcClient rpcClient = new RpcClient(serviceUrl, clientOption, null);
    final Echo.EchoRequest request = Echo.EchoRequest.newBuilder().setMessage("hello").build();
    final EchoService echoService = BrpcProxy.getProxy(rpcClient, EchoService.class);
    for (int i = 0; i < 20; i++) {
        echoService.echo(request);
    }
    rpcClient.stop();
}
 
Example 6
Source File: RpcClientTest.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        RpcClientOptions clientOption = new RpcClientOptions();
        clientOption.setProtocolType(ProtocolType.PROTOCOL_NSHEAD_PROTOBUF_VALUE);
        // clientOption.setProtocolType(ProtocolType.PROTOCOL_NSHEAD_JSON_VALUE);
        clientOption.setWriteTimeoutMillis(1000);
        clientOption.setReadTimeoutMillis(5000);
        clientOption.setLoadBalanceType(LoadBalanceStrategy.LOAD_BALANCE_FAIR);
        clientOption.setEncoding("gbk");

        // 高端口,在开发机上测试
        String serviceUrl = "list://localhost:8080";

        RpcClient rpcClient = new RpcClient(serviceUrl, clientOption);

        // sync call
        EchoService echoService = BrpcProxy.getProxy(rpcClient, EchoService.class);

        RpcContext.getContext().setLogId(1234L);
        Echo.EchoRequest request = Echo.EchoRequest.newBuilder().setMessage("hello world").build();
        EchoResponse response = echoService.echo(request);
        System.out.println("--------nshead protobuf sync call response-----------------");
        System.out.println(response.getMessage());
        rpcClient.stop();


    }
 
Example 7
Source File: SingleConnectionClientTest.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
private static RpcClientOptions getRpcClientOptions() {
    RpcClientOptions clientOption = new RpcClientOptions();
    clientOption.setProtocolType(Options.ProtocolType.PROTOCOL_BAIDU_STD_VALUE);
    clientOption.setWriteTimeoutMillis(1000);
    clientOption.setReadTimeoutMillis(1000);
    clientOption.setMaxTotalConnections(1000);
    clientOption.setMinIdleConnections(10);
    clientOption.setLoadBalanceType(LoadBalanceStrategy.LOAD_BALANCE_ROUND_ROBIN);
    clientOption.setCompressType(Options.CompressType.COMPRESS_TYPE_NONE);
    clientOption.setChannelType(ChannelType.SINGLE_CONNECTION);
    clientOption.setKeepAliveTime(25);
    return clientOption;
}
 
Example 8
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/brpc")
@ResponseBody
public String brpc() {
    RpcClientOptions clientOption = new RpcClientOptions();
    clientOption.setProtocolType(Options.ProtocolType.PROTOCOL_BAIDU_STD_VALUE);
    clientOption.setWriteTimeoutMillis(1000);
    clientOption.setReadTimeoutMillis(5000);
    clientOption.setMaxTotalConnections(1000);
    clientOption.setMinIdleConnections(10);
    clientOption.setLoadBalanceType(LoadBalanceStrategy.LOAD_BALANCE_FAIR);
    clientOption.setCompressType(Options.CompressType.COMPRESS_TYPE_NONE);

    String serviceUrl = "list://127.0.0.1:1118";
    Echo.EchoRequest request = Echo.EchoRequest.newBuilder()
            .setMessage("helloooooooooooo")
            .build();

    RpcClient rpcClient = new RpcClient(serviceUrl, clientOption);
    EchoService echoService = BrpcProxy.getProxy(rpcClient, EchoService.class);
    try {
        EchoResponse response = echoService.echo(request);
        System.out.println(response.getMessage());
    } catch (RpcException ex) {
    }
    rpcClient.stop();
    return SUCCESS;
}