com.baidu.brpc.client.BrpcProxy Java Examples
The following examples show how to use
com.baidu.brpc.client.BrpcProxy.
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 |
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: ExampleServiceImpl.java From raft-java with Apache License 2.0 | 6 votes |
@Override public ExampleProto.SetResponse set(ExampleProto.SetRequest request) { ExampleProto.SetResponse.Builder responseBuilder = ExampleProto.SetResponse.newBuilder(); // 如果自己不是leader,将写请求转发给leader if (raftNode.getLeaderId() <= 0) { responseBuilder.setSuccess(false); } else if (raftNode.getLeaderId() != raftNode.getLocalServer().getServerId()) { onLeaderChangeEvent(); ExampleService exampleService = BrpcProxy.getProxy(leaderRpcClient, ExampleService.class); ExampleProto.SetResponse responseFromLeader = exampleService.set(request); responseBuilder.mergeFrom(responseFromLeader); } else { // 数据同步写入raft集群 byte[] data = request.toByteArray(); boolean success = raftNode.replicate(data, RaftProto.EntryType.ENTRY_TYPE_DATA); responseBuilder.setSuccess(success); } ExampleProto.SetResponse response = responseBuilder.build(); LOG.info("set request, request={}, response={}", jsonFormat.printToString(request), jsonFormat.printToString(response)); return response; }
Example #3
Source File: ConcurrentClientMain.java From raft-java with Apache License 2.0 | 6 votes |
public static void main(String[] args) { if (args.length != 1) { System.out.printf("Usage: ./run_concurrent_client.sh THREAD_NUM\n"); System.exit(-1); } // parse args String ipPorts = args[0]; RpcClient rpcClient = new RpcClient(ipPorts); ExampleService exampleService = BrpcProxy.getProxy(rpcClient, ExampleService.class); ExecutorService readThreadPool = Executors.newFixedThreadPool(3); ExecutorService writeThreadPool = Executors.newFixedThreadPool(3); Future<?>[] future = new Future[3]; for (int i = 0; i < 3; i++) { future[i] = writeThreadPool.submit(new SetTask(exampleService, readThreadPool)); } }
Example #4
Source File: RpcClientTest.java From brpc-java with Apache License 2.0 | 5 votes |
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 #5
Source File: CaseController.java From skywalking with Apache License 2.0 | 5 votes |
@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; }
Example #6
Source File: Peer.java From raft-java with Apache License 2.0 | 5 votes |
public Peer(RaftProto.Server server) { this.server = server; this.rpcClient = new RpcClient(new Endpoint( server.getEndpoint().getHost(), server.getEndpoint().getPort())); raftConsensusServiceAsync = BrpcProxy.getProxy(rpcClient, RaftConsensusServiceAsync.class); isCatchUp = false; }
Example #7
Source File: RaftClientServiceProxy.java From raft-java with Apache License 2.0 | 5 votes |
public RaftClientServiceProxy(String ipPorts) { rpcClientOptions.setConnectTimeoutMillis(1000); // 1s rpcClientOptions.setReadTimeoutMillis(3600000); // 1hour rpcClientOptions.setWriteTimeoutMillis(1000); // 1s clusterRPCClient = new RpcClient(ipPorts, rpcClientOptions); clusterRaftClientService = BrpcProxy.getProxy(clusterRPCClient, RaftClientService.class); updateConfiguration(); }
Example #8
Source File: ClientMain.java From raft-java with Apache License 2.0 | 5 votes |
public static void main(String[] args) { if (args.length < 2) { System.out.printf("Usage: ./run_client.sh CLUSTER KEY [VALUE]\n"); System.exit(-1); } // parse args String ipPorts = args[0]; String key = args[1]; String value = null; if (args.length > 2) { value = args[2]; } // init rpc client RpcClient rpcClient = new RpcClient(ipPorts); ExampleService exampleService = BrpcProxy.getProxy(rpcClient, ExampleService.class); final JsonFormat jsonFormat = new JsonFormat(); // set if (value != null) { ExampleProto.SetRequest setRequest = ExampleProto.SetRequest.newBuilder() .setKey(key).setValue(value).build(); ExampleProto.SetResponse setResponse = exampleService.set(setRequest); System.out.printf("set request, key=%s value=%s response=%s\n", key, value, jsonFormat.printToString(setResponse)); } else { // get ExampleProto.GetRequest getRequest = ExampleProto.GetRequest.newBuilder() .setKey(key).build(); ExampleProto.GetResponse getResponse = exampleService.get(getRequest); System.out.printf("get request, key=%s, response=%s\n", key, jsonFormat.printToString(getResponse)); } rpcClient.stop(); }
Example #9
Source File: StargateDemoClient.java From brpc-java with Apache License 2.0 | 5 votes |
public static void main(String[] args) { RpcClientOptions options = new RpcClientOptions(); // Stargate 协议需要强指定协议类型,不可使用BRPC协议解析器 options.setProtocolType(Options.ProtocolType.PROTOCOL_STARGATE_VALUE); options.setReadTimeoutMillis(1000); options.setWriteTimeoutMillis(1000); RpcClient rpcClient = new RpcClient(StargateDemoConstant.namingUrl, options); NamingOptions namingOptions = new NamingOptions(); namingOptions.setGroup(StargateDemoConstant.group); namingOptions.setVersion(StargateDemoConstant.version); StargateDemoService proxy = BrpcProxy.getProxy(rpcClient, StargateDemoService.class, namingOptions); for (int i = 0, times = 10; i < times; i++) { RpcContext rpcContext = RpcContext.getContext(); rpcContext.reset(); rpcContext.setRequestKvAttachment("key", "value"); StargateDemoReqDto reqDto = new StargateDemoReqDto(); reqDto.setId(1000L); reqDto.setName("test"); StargateDemoResDto call = proxy.call(reqDto); System.out.println(GsonUtils.toJson(call)); if (rpcContext.getResponseKvAttachment() != null) { System.out.println(rpcContext.getResponseKvAttachment().get("resKey")); } System.out.println(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } rpcClient.stop(); }
Example #10
Source File: ServerPushTest.java From brpc-java with Apache License 2.0 | 5 votes |
@Test public void testBasic() { RpcServerOptions rpcServerOptions = RpcOptionsUtils.getRpcServerOptions(); rpcServerOptions.setProtocolType(Options.ProtocolType.PROTOCOL_SERVER_PUSH_VALUE); RpcServer rpcServer = new RpcServer(8000, rpcServerOptions); rpcServer.registerService(new EchoServiceImpl()); rpcServer.start(); RpcClientOptions rpcClientOptions = RpcOptionsUtils.getRpcClientOptions(); rpcClientOptions.setProtocolType(Options.ProtocolType.PROTOCOL_SERVER_PUSH_VALUE); rpcClientOptions.setClientName("c1"); RpcClient rpcClient = new RpcClient("list://127.0.0.1:8000", rpcClientOptions); EchoService echoService = BrpcProxy.getProxy(rpcClient, EchoService.class); rpcClient.registerPushService(new UserPushApiImpl()); Echo.EchoRequest request = Echo.EchoRequest.newBuilder().setMessage("hello").build(); Echo.EchoResponse response = echoService.echo(request); assertEquals("hello", response.getMessage()); ServerSideUserPushApi pushApi = (ServerSideUserPushApi) BrpcPushProxy.getProxy(rpcServer, ServerSideUserPushApi.class); PushData p = new PushData(); p.setData("abc"); PushResult pushResult = pushApi.clientReceive("c1", p); assertEquals("got data:abc", pushResult.getResult()); rpcClient.stop(); rpcServer.shutdown(); }
Example #11
Source File: RateLimitTest.java From brpc-java with Apache License 2.0 | 5 votes |
@Test public void test1Client2Server() { RpcClient rpcClient = new RpcClient("list://127.0.0.1:8000,127.0.0.1:8001", RpcOptionsUtils.getRpcClientOptions()); EchoService echoService = BrpcProxy.getProxy(rpcClient, EchoService.class); Echo.EchoRequest request = Echo.EchoRequest.newBuilder().setMessage("hello").build(); for (int i = 0; i < 100; i++) { echoService.echo(request); } rpcClient.stop(); }
Example #12
Source File: ServerInitTest.java From brpc-java with Apache License 2.0 | 5 votes |
@Test @Ignore("Fix this test later") public void testInitServerMultiTimes() throws Exception { RpcServer rpcServer1 = new RpcServer(8000, RpcOptionsUtils.getRpcServerOptions()); rpcServer1.registerService(new EchoServiceImpl()); rpcServer1.start(); RpcServer rpcServer2 = new RpcServer(8001, RpcOptionsUtils.getRpcServerOptions()); rpcServer2.registerService(new EchoServiceImpl(), RpcOptionsUtils.getRpcServerOptions()); rpcServer2.start(); RpcClient secondRpcClient = new RpcClient("list://127.0.0.1:8001", RpcOptionsUtils.getRpcClientOptions()); EchoService echoService = BrpcProxy.getProxy(secondRpcClient, EchoService.class); EchoRequest request = Echo.EchoRequest.newBuilder().setMessage("hello").build(); echoService.echo(request); int processor = Runtime.getRuntime().availableProcessors(); ThreadNumStat stat1 = calThreadNum(); Assert.assertEquals(2, stat1.serverWorkThreadNum); Assert.assertEquals(1, stat1.customWorkThreadNum); Assert.assertEquals(1, stat1.clientWorkThreadNum); rpcServer1.shutdown(); rpcServer2.shutdown(); Thread.sleep(3); ThreadNumStat stat2 = calThreadNum(); Assert.assertEquals(0, stat2.serverIoThreadNum); Assert.assertEquals(0, stat2.serverWorkThreadNum); Assert.assertEquals(0, stat2.customWorkThreadNum); secondRpcClient.shutdown(); }
Example #13
Source File: LoadBalanceTest.java From brpc-java with Apache License 2.0 | 5 votes |
@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 #14
Source File: LoadBalanceTest.java From brpc-java with Apache License 2.0 | 5 votes |
@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 #15
Source File: LoadBalanceTest.java From brpc-java with Apache License 2.0 | 5 votes |
@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 #16
Source File: LoadBalanceTest.java From brpc-java with Apache License 2.0 | 5 votes |
@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 #17
Source File: BaiduRpcProtocolTest.java From brpc-java with Apache License 2.0 | 5 votes |
public static RpcMethodInfo buildRpcMethodInfo() throws Exception { Class[] paramTypes = new Class[2]; paramTypes[0] = RpcClient.class; paramTypes[1] = Class.class; Constructor constructor = BrpcProxy.class.getDeclaredConstructor(paramTypes); constructor.setAccessible(true); RpcClient rpcClient = new RpcClient("list://127.0.0.1:8002", RpcOptionsUtils.getRpcClientOptions()); BrpcProxy rpcProxy = (BrpcProxy) constructor.newInstance(rpcClient, EchoService.class); rpcClient.shutdown(); Map<String, RpcMethodInfo> methodInfoMap = rpcProxy.getRpcMethodMap(); RpcMethodInfo rpcMethodInfo = methodInfoMap.entrySet().iterator().next().getValue(); return rpcMethodInfo; }
Example #18
Source File: GlobalThreadPoolSharingTest.java From brpc-java with Apache License 2.0 | 4 votes |
@Test @Ignore public void testShareGlobalThreadPool() { RpcServerOptions rpcServerOptions = RpcOptionsUtils.getRpcServerOptions(); rpcServerOptions.setProtocolType(Options.ProtocolType.PROTOCOL_SERVER_PUSH_VALUE); rpcServerOptions.setGlobalThreadPoolSharing(true); RpcServer rpcServer1 = new RpcServer(8000, rpcServerOptions); rpcServer1.registerService(new EchoServiceImpl()); rpcServer1.start(); RpcServer rpcServer2 = new RpcServer(8001, rpcServerOptions); rpcServer2.registerService(new EchoServiceImpl()); rpcServer2.start(); Assert.assertTrue(rpcServer1.getBossGroup() == rpcServer2.getBossGroup()); Assert.assertTrue(rpcServer1.getWorkerGroup() == rpcServer2.getWorkerGroup()); Assert.assertTrue(rpcServer1.getThreadPool() == rpcServer2.getThreadPool()); RpcClientOptions rpcClientOptions = RpcOptionsUtils.getRpcClientOptions(); rpcClientOptions.setProtocolType(Options.ProtocolType.PROTOCOL_SERVER_PUSH_VALUE); rpcClientOptions.setClientName("c1"); rpcClientOptions.setGlobalThreadPoolSharing(true); RpcClient rpcClient1 = new RpcClient("list://127.0.0.1:8000", rpcClientOptions); EchoService echoService1 = BrpcProxy.getProxy(rpcClient1, EchoService.class); rpcClient1.registerPushService(new UserPushApiImpl()); RpcClient rpcClient2 = new RpcClient("list://127.0.0.1:8001", rpcClientOptions); EchoService echoService2 = BrpcProxy.getProxy(rpcClient2, EchoService.class); rpcClient2.registerPushService(new UserPushApiImpl()); RpcClient rpcClient3 = new RpcClient("list://127.0.0.1:8001", rpcClientOptions); EchoService echoService3 = BrpcProxy.getProxy(rpcClient3, EchoService.class); rpcClient3.registerPushService(new UserPushApiImpl()); BrpcThreadPoolManager threadPoolManager = BrpcThreadPoolManager.getInstance(); Assert.assertTrue(threadPoolManager.getIoThreadPoolMap().size() == 1); Assert.assertTrue(threadPoolManager.getWorkThreadPoolMap().size() == 1); Echo.EchoRequest request = Echo.EchoRequest.newBuilder().setMessage("hello").build(); for (int i = 0; i < 1000; i++) { Echo.EchoResponse response = echoService1.echo(request); Echo.EchoResponse response2 = echoService2.echo(request); Echo.EchoResponse response3 = echoService3.echo(request); assertEquals("hello", response.getMessage()); assertEquals("hello", response2.getMessage()); assertEquals("hello", response3.getMessage()); } // test shutndown and stop rpcClient1.stop(); rpcClient2.stop(); rpcClient3.stop(); rpcServer1.shutdown(); rpcServer2.shutdown(); // client Assert.assertTrue(threadPoolManager.getIoThreadPoolMap().size() == 1); Assert.assertTrue(threadPoolManager.getWorkThreadPoolMap().size() == 1); // server EventLoopGroup r1BossGroup = rpcServer1.getBossGroup(); EventLoopGroup r1WorkerGroup = rpcServer1.getWorkerGroup(); ThreadPool r1ThreadPool = rpcServer1.getThreadPool(); Assert.assertFalse(r1BossGroup.isShutdown()); Assert.assertFalse(r1WorkerGroup.isShutdown()); Assert.assertFalse(r1ThreadPool.isStopped()); ShutDownManager.shutdownGlobalThreadPools(); try { Thread.sleep(5 * 1000L); } catch (InterruptedException e) { // do nothing } Assert.assertTrue(threadPoolManager.getIoThreadPoolMap().size() == 0); Assert.assertTrue(threadPoolManager.getWorkThreadPoolMap().size() == 0); Assert.assertTrue(r1BossGroup.isShutdown()); Assert.assertTrue(r1WorkerGroup.isShutdown()); Assert.assertTrue(r1ThreadPool.isStopped()); }