我从互联网上尝试了这个节俭的Java示例,如下所示。客户端未连接到服务器。我有这些:
(1)节俭定义“ add.thrift”
typedef i32 int //typedefs to get convenient names for your types
service AdditionService { // defines the service to add two numbers
int add(1:int n1, 2:int n2), //defines a method
}
(2)编译
thrift --gen java add.thrift
我有gen-java / AdditionServer.java文件
(3)在AdditionServiceHandler.java中实现服务接口
import org.apache.thrift.TException;
public class AdditionServiceHandler implements AdditionService.Iface {
@Override
public int add(int n1, int n2) throws TException {
return n1 + n2;
}
}
(4)MyServer.java
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
public class MyServer {
public static void main(String[] args) {
AdditionService.Processor<AdditionServiceHandler> processor =
new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler());
try {
TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
System.out.println("Starting the simple server...");
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
}
(5)AdditionClient.java
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
public class AdditionClient {
public static void main(String[] args) {
try {
TTransport transport = new TSocket("localhost", 9090);
transport.open();
AdditionService.Client client = new AdditionService.Client(new TBinaryProtocol(transport));
//client.ping(2012);
System.out.println(client.add(100, 200));
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException x) {
x.printStackTrace();
}
}
}
(6)在“ mvn编译”之后,我执行了:
mvn exec:java -Dexec.mainClass="MyServer"
已打印
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting the simple server..
然后我启动客户端,它没有连接,几秒钟后,它超时并退出:
mvn exec:java -Dexec.mainClass="AdditionClient"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building java_local 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ java_local ---
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
org.apache.thrift.transport.TTransportException: java.net.ConnectException: Operation timed out (Connection timed out)
at org.apache.thrift.transport.TSocket.open(TSocket.java:226)
那我在哪里弄错了,怎么解决呢?