starlight icon indicating copy to clipboard operation
starlight copied to clipboard

支持泛化调用么

Open shangjiuliu opened this issue 4 years ago • 4 comments

在代码里没找到关于泛化调用的支持 如果目前不支持的话 有支持计划么

shangjiuliu avatar Feb 29 '20 04:02 shangjiuliu

@shangjiuliu 目前还不支持,可以近期支持。

wenweihu86 avatar Feb 29 '20 14:02 wenweihu86

@wenweihu86 请问泛化调用支持了吗,有计划什么时候支持吗?

shawn-deng avatar Jul 08 '20 09:07 shawn-deng

@shangjiuliu 目前还不支持,可以近期支持。

请问支持了么

YK-Leung avatar Jan 15 '21 09:01 YK-Leung

I hope it can be used in this way.

server: https://github.com/baidu/brpc-java/blob/master/brpc-java-examples/brpc-java-core-examples/src/main/java/com/baidu/brpc/example/jprotobuf/RpcServerTest.java

GenericService

public interface GenericService {
    
    Object $invoke(String serviceName, String methodName, String[] parameterTypes, Object[] args) throws RuntimeException;
    
    Future<Object> $invokeAsync(String serviceName, String methodName, String[] parameterTypes, Object[] args) throws RuntimeException;
}

generic call client

public class RpcClientTest {
    
    public static void main(String[] args) {
        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.setIoThreadNum(40);
        clientOption.setLoadBalanceType(LoadBalanceStrategy.LOAD_BALANCE_FAIR);
        clientOption.setCompressType(Options.CompressType.COMPRESS_TYPE_NONE);
        
        String serviceUrl = "list://127.0.0.1:8002";
        if (args.length == 1) {
            serviceUrl = args[0];
        }
        
        List<Interceptor> interceptors = new ArrayList<Interceptor>();
        interceptors.add(new CustomInterceptor());
        
        // build request
        Map<String, Object> request = new HashMap<>();
        request.put("message", "hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
        
        // async call
        RpcClient rpcClient = new RpcClient(serviceUrl, clientOption, interceptors);
        RpcCallback<EchoResponse> callback = new RpcCallback<EchoResponse>() {
            @Override
            public void success(EchoResponse response) {
                if (response != null) {
                    System.out.printf("async call EchoService.echo success, response=%s\n",
                            response.getMessage());
                } else {
                    System.out.println("async call failed, service=EchoService.echo");
                }
            }
            
            @Override
            public void fail(Throwable e) {
                System.out.printf("async call EchoService.echo failed, %s\n", e.getMessage());
            }
        };
        GenericService asyncEchoService = BrpcProxy.getProxy(rpcClient, GenericService.class);
        try {
            Future<Object> future = asyncEchoService.$invokeAsync("com.baidu.brpc.example.jprotobuf.EchoServiceAsync",
                    "echo",
                    new String[]{"com.baidu.brpc.example.jprotobuf.EchoRequest", "com.baidu.brpc.client.RpcCallback"},
                    new Object[]{request, callback});
            try {
                if (future != null) {
                    future.get();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (RpcException ex) {
            System.out.println("rpc send failed, ex=" + ex.getMessage());
        }
        rpcClient.stop();
        System.out.println("brpc client finished...");
    }
    
}

loongs-zhang avatar Aug 13 '22 08:08 loongs-zhang