我的项目简要描述: 我正在写一个名为“GreetingsNode”的java类,它在分布式环境中工作,其中有一个“managementNode”作为服务存储库并接收和存储其他节点的信息(主机端口号和所提供的服务),并分发由注册服务提供的方法的RPC。如果一个节点可以回答RPC,则打开一个节点套接字并在主叫节点和应答节点之间建立连接,并且应答节点返回结果。Apache Thrift RPC在分布式环境中的Java方法实现
我使用Apache thrift作为IDL和RPC的框架。
现在的问题。 My GreetingsNodeHandler类实现了一个包含单个方法“getHello(user)”(用户是包含节点名称的结构,它是GreetingsNode类的构造函数的参数)的简单节点接口。 当连接到管理节点的GreetingsNode X创建该方法的RPC时,另一个注册的GreetingsNode必须回答消息“hello X”。
我不明白如何实现返回结果的处理程序部分,因此我无法理解应该如何编写junit测试来检查方法实现是否正常工作。
断言像 的assertEquals(client.getHello(用户).getMessage(), “John Doe:您好”)
会的工作,但我不明白怎么回事,在我的情况,我应该把客户端部分...
为GreetingService的节俭服务代码:
struct Message {
1: string message
}
struct User {
1: string name
}
service GreetingsService {
Message getHello(1: User user)
}
代码GreetingsServiceHandler必须实现GreetingsService方法getHello()
public class GreetingsServiceHandler implements GreetingsService.Iface {
private static Random random = new Random(10);
private ManagementService.Client managementClient;
private GreetingsService.Client helloClient;
@Override
public Message getHello(User user) throws TException {
Message answer = null;
// class ServiceProvider is generated by thrift, part of ManagementService thrift service
ServiceProvider provider = null;
List<ServiceProvider>providers = managementClient.getProvidersForService(user.name);
if (providers.isEmpty())
throw new NoProviderAvailableException(); //separate file contains Exception
else {
provider = providers.get(random.nextInt(providers.size()));
//connection between nodes is established here
TTransport helloTransport = new TSocket(provider.getHostName(), provider.getPort());
TProtocol helloProtocol = new TBinaryProtocol(helloTransport);
helloClient = new GreetingsService.Client(helloProtocol);
helloTransport.open();
// here lies my problem
answer = helloClient.getHello(user);
//if I use this instead, then helloClient variable is clearly not used, but of course I need it to answer the method call
answer = answer.setMessage("Ciao " + user.getName() + ", welcome among us!");
}
return answer;
}
和GreetingsNode代码如下:
public class GreetingsNode implements NodeIface {
private ThriftServer helloServer;
private ManagementService.Client managementClient;
private NodeManifest nodeManifest;
private User user;
private String name;
public GreetingsNode(NodeManifest nodeManifest, String name) {
this.nodeManifest = nodeManifest;
this.helloServer = new ThriftServer(GreetingsServiceHandler.class);
this.name = name;
}
@Override
public void turnOn() throws TException {
helloServer.start();
TSocket helloServerTransport = new TSocket("localhost", Constants.SERVER_PORT);
TBinaryProtocol helloServerProtocol = new TBinaryProtocol(helloServerTransport);
managementClient = new ManagementService.Client(helloServerProtocol);
this.setUser(new User(name));
helloServerTransport.open();
helloServer = new ThriftServer(GreetingsServiceHandler.class);
//portNegotiator is a class described in a separate file, that handles the registration of other nodes to the managementNode. NodeManifest is a file generated by thrift, part of managementService thrift file, describing a struct that contains hostname and port number of nodes.
PortNegotiator negotiator = new PortNegotiator(managementClient);
negotiator.negotiate(nodeManifest, helloServer);
}
@Override
public void turnOff() {
helloServer.stop();
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
你有看过[教程代码](https:// thrift。apache.org/tutorial/java)? – JensG
当然我做了,我已经实现了一个函数算术计算器,其中“算法节点”应答由另一个节点所做的“executeOperation”方法的调用。 当我使用字符串时,我无法翻译这种行为...不知道为什么实际上因为这个概念应该是非常相似,如果不相同 – Gaspare79