Skip to content

Get Started

Ranger edited this page Dec 24, 2020 · 5 revisions

dubbo service version 1.0.0

package com.example.dubbo;

public interface PersonService {
	public Person findById(long id);
	public void save(Person person);
}

grpc-java client

ManagedChannel channel = NettyChannelBuilder.forAddress("127.0.0.1", 5050)
		.negotiationType(NegotiationType.PLAINTEXT).build();

Object[] paramValues = new Object[] { 12345 };
ProxyRequest request = ProxyRequest.newBuilder().setInterfaceName("com.example.dubbo.PersonService")
		.setMethod("findById").setGroup("")
		.setVersion("1.0.0")
		.addTypes("long")
		.setValues(new Gson().toJson(paramValues)).build();

ProxyServiceGrpc.ProxyServiceBlockingStub blockingStub = ProxyServiceGrpc.newBlockingStub(channel)
		.withDeadlineAfter(60, TimeUnit.SECONDS).withCompression("gzip");
ProxyReply rpcResp = blockingStub.invokeProxy(request);
System.out.println(rpcResp.getResult());

grpc-node client

npm install --save grpc  @grpc/proto-loader


    const values = [12345];
    const version ='1.0.0';
    const request = {
        interfaceName:'com.example.dubbo.PersonService',
        version: version,
        //group :'',
        method:'findById',
        types :['long'],
        values:JSON.stringify(values)
    };
const PROTO_PATH = __dirname + '/clazz_method_param.proto';

const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });
const dubbo = grpc.loadPackageDefinition(packageDefinition).dubbo;
// https://github.com/grpc/grpc/blob/master/examples/node/dynamic_codegen/greeter_client.js
  const client = new dubbo.ProxyService('127.0.0.1:5050',
                                       grpc.credentials.createInsecure());
    client.InvokeProxy(request, function(err, response) {
    console.log('result:', response);
  });


grpc-go client

	c := dubbo.NewProxyServiceClient(GrpcClient)
	types := [] string {"long"}
	value := fmt.Sprintf("[%d]", sceneId)

	request := dubbo.ProxyRequest{
		InterfaceName: "com.example.dubbo.PersonService",
		Version:       "1.0.0",
		Method:        "findById",
		Types: 		   types,
		Values:        value,
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
	defer cancel()

	reply, err := c.InvokeProxy(ctx, &request) 

grpc-python client

import  grpc

MAX_MESSAGE_LENGTH = 100 << 10 << 10
options = [
            ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
            ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)
]

channel = grpc.insecure_channel("127.0.0.1", options = options, compression = grpc.Compression.Gzip)

def grpc_stub():
    return  clazz_method_param_pb2_grpc.ProxyServiceStub(channel)

import atexit

def exit_handler():
    if channel:
        channel.close()
atexit.register(exit_handler)

    values = f"[{pageId}]"
    
    req = ProxyRequest(interfaceName='com.example.dubbo.PersonService',
                       version = '1.0.0',
                       method='findById',
                       types=['long'],
                       values=values
                       )
    
    reply = grpc_stub().InvokeProxy(req, compression = grpc.Compression.Gzip, timeout = 30)
    print(ujson.loads(reply.result))
Clone this wiki locally