整体架构
源码—user.thrift
namespace java com.tuliing
struct User{
1:i32 id
2:string name
3:i32 age=0
}
service UserService{
User getById(1:i32 id),
bool isExist(1:string name)
}
编译
thrift -r -gen java user.thrift
生成gen-java文件夹
源码–实现
package com.tuliing;
import org.apache.thrift.TException;
public class UserServiceImpl implements UserService.Iface{
@Override
public User getById(int id) throws TException {
// TODO Auto-generated method stub
System.out.print("getById....");
User user = new User();
user.setId(11);
user.setName("this is name");
user.setAge(10);
return user;
}
@Override
public boolean isExist(String name) throws TException {
// TODO Auto-generated method stub
System.out.print("isExist ....");
return false;
}
}
源码—启动客户端
package com.tuliing;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TSimpleFileTransport;
import org.apache.thrift.transport.TTransportException;
public class simpleService {
public static void main(String [] args) throws TTransportException {
TServerTransport serverTransport = new TServerSocket(9090);
UserService.Processor processor = new UserService.Processor(new UserServiceImpl());
TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();
TSimpleServer.Args targs = new TSimpleServer.Args(serverTransport);
targs.processor(processor);
targs.protocolFactory(protocolFactory);
TServer server = new TSimpleServer(targs);
System.out.println("start simple server....");
server.serve();
}
}
配置—build path
配置–runtime
如果只是通过启动客户端的main启动,不需要这个配置,如果是多个插件项目,需要这个配置
配置–pruduct
如果你的项目是插件1,插件2,插件…
且插件1被打包成product,插件2是上述的内容,
且插件2希望被自动加载以启动服务器
此时需要配置product
打开product配置,右侧的最下面选项卡第二个contents,添加你的插件2