开发thrift接口,用的注解方式,记录一下
step 1 biz层 配置thrift client bean (一个端口支持多个服务)
@Configuration
public class ThriftServerConfig {
@Autowired
TYourServiceImpl1 tYourService1;
@Autowired
TYourServiceImpl2 tYourService2;
@Bean(name = "serverPublisher", initMethod = "publish", destroyMethod = "destroy")
public ThriftServerPublisher getThriftServerPublisher() throws Exception{
ThriftServerPublisher publisher = new ThriftServerPublisher();
String appKey = "yourAppkey";
publisher.setAppKey(appKey);
publisher.setPort(9001);
publisher.setSerializeNullStringAsBlank(true);
Map<Class<?>, ThriftServiceBean> serviceProcessorMap = new HashMap<>(4);
ThriftServiceBean thriftServiceBean1 = new ThriftServiceBean();
thriftServiceBean1.setServiceImpl(tYourService1);
serviceProcessorMap.put(TYourService1.class, thriftServiceBean1);
ThriftServiceBean thriftServiceBean2 = new ThriftServiceBean();
thriftServiceBean2.setServiceImpl(tYourService2);
serviceProcessorMap.put(TYourService2.class, thriftServiceBean2);
publisher.setServiceProcessorMap(serviceProcessorMap);
return publisher;
}
}
step 2 api层定义对外的数据结构
2.1 对外方法
@ThriftService
public interface TYourService1 {
@ThriftMethod
List<YourDataStruct> getStructs(String param) throws TException;
}
2.2 数据结构
@ThriftStruct
public class YourDataStruct implements Serializable {
private String a;
private String b;
@ThriftField(value = 1, requiredness =ThriftField.Requiredness.REQUIRED)
public String getA() {
return a;
}
@ThriftField
public void setA(String a) {
this.a = a;
}
@ThriftField(value = 2, requiredness =ThriftField.Requiredness.OPTIONAL)
public String getB() {
return b;
}
@ThriftField
public void setB(String b) {
this.b = b;
}
}
step 3 biz层定义方法实现
public class TYourServiceImpl1 implements TYourService1 {
@Override
public List<YourDataStruct> getStructs(String param) throws TException {
return xxx;
}
}
step 4 api层 pom定义包版本, deploy到maven
biz层 部署到机器
下一篇讲client端如何调用方法