【JAVA】thrift接口开发手把手教程(注解方式)server端

开发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端如何调用方法

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java thrift开发通常需要以下步骤: 1. 安装thrift编译器:Thrift编译器可用于将thrift文件编译成Java源代码。 2. 编写thrift文件:thrift文件定义了数据类型和服务接口。可以使用thrift IDL语言编写thrift文件。 3. 编译thrift文件:使用thrift编译器将thrift文件编译成Java源代码。 4. 实现服务:实现thrift生成的服务接口和处理程序。 5. 实现客户:使用thrift生成的客户代码调用服务。 下面是一个简单的Java thrift开发示例: 1. 编写thrift文件,定义一个服务接口和一个相关的数据类型: ``` namespace java com.example struct Person { 1: required string name 2: optional i32 age } service PersonService { void addPerson(1: Person person) Person getPerson(1: string name) } ``` 2. 使用thrift编译器将thrift文件编译成Java源代码: ``` thrift --gen java person.thrift ``` 3. 实现服务: ``` public class PersonServiceImpl implements PersonService.Iface { @Override public void addPerson(Person person) throws TException { // 实现添加人员的逻辑 } @Override public Person getPerson(String name) throws TException { // 实现获取人员信息的逻辑 } } ``` 4. 实现客户: ``` public class PersonServiceClient { public static void main(String[] args) throws TException { TTransport transport = new TSocket("localhost", 9090); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); PersonService.Client client = new PersonService.Client(protocol); Person person = new Person(); person.setName("张三"); person.setAge(20); client.addPerson(person); Person result = client.getPerson("张三"); System.out.println(result.getName() + " " + result.getAge()); transport.close(); } } ``` 以上就是一个简单的Java thrift开发示例。当然,在实际开发中可能会涉及到更复杂的数据类型和服务接口,需要更多的代码和实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值