hprose/hprose-golang

服务器是java 客户端是go 调用无效

Anehta opened this issue · 1 comments

服务器

package RPCServer;

import hprose.server.HproseTcpServer;

import java.io.IOException;
import java.net.URISyntaxException;

public class RPCServer {
    public static String hello() {
        return "Hello boy";
    }

    public static void main(String[] args) throws URISyntaxException, IOException {
        HproseTcpServer server = new HproseTcpServer("tcp://127.0.0.1:8888");
        server.add("hello", RPCServer.class);
        server.start();
        System.out.println("START tcp");
        System.in.read();
        server.stop();
        System.out.println("STOP");
    }
}

客户端

package main

import (
	"fmt"

	"github.com/hprose/hprose-golang/rpc"
)

type RPCServer struct {
	hello func() string
}

func main() {
	client := rpc.NewClient("tcp://127.0.0.1:8888")
	fmt.Println(client.URI())
	var test *RPCServer
	client.UseService(&test)
	fmt.Println(test)
}

结果就是客户端useService test指针一直是nil

andot commented

调用应该是:

fmt.Println(test.hello())

这样吧。test 只是一个客户端代理对象,并不是hello方法本身。

另外,你客户端定义的方法字段应该大写首字母,比如上面的例子,客户端应该这样写:

package main

import (
	"fmt"

	"github.com/hprose/hprose-golang/rpc"
)

type RPCServer struct {
	Hello func() string
}

func main() {
	client := rpc.NewClient("tcp://127.0.0.1:8888")
	fmt.Println(client.URI())
	var test *RPCServer
	client.UseService(&test)
	fmt.Println(test.Hello())
}

毕竟小写开头的字段是私有的,无法通过反射来获取到它,也就不能动态生成它。服务器端的 hello 无需大写首字母。