thrift协议

Server(single-threaded, event-driven etc)
服务器进程调度
Processor(compiler generated)
RPC接口处理函数分发,IDL定义接口的实现将挂接到这里面
Protocol (JSON, compact etc)
协议
Transport(raw TCP, HTTP etc)
网络传输

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Line thrift 协议的示例代码: ```thrift namespace java com.example struct Person { 1: required string name; 2: optional i32 age; } service PersonService { Person getPerson(1: string name); } ``` 通过上述 thrift 定义文件,可以生成对应的 Java 类和接口代码。假设生成的 Java 代码包名为 `com.example`,则 `Person` 类和 `PersonService` 接口的代码如下: ```java package com.example; 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 Person { private String name; private Integer age; public Person() {} public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } public interface PersonService { public Person getPerson(String name) throws TException; } public class PersonServiceClient implements PersonService { private TTransport transport; private TProtocol protocol; private PersonService.Client client; public PersonServiceClient() throws TTransportException { transport = new TSocket("localhost", 9090); protocol = new TBinaryProtocol(transport); client = new PersonService.Client(protocol); transport.open(); } public void close() { transport.close(); } public Person getPerson(String name) throws TException { return client.getPerson(name); } } public class PersonServiceHandler implements PersonService.Iface { public Person getPerson(String name) throws TException { // TODO: implement the logic to retrieve the person data return new Person(name, 30); } } ``` 在上述代码中,`Person` 类表示一个人的信息,包括姓名和年龄。`PersonService` 接口定义了一个获取人员信息的方法 `getPerson`。`PersonServiceClient` 类是一个 thrift 客户端,用于调用 `getPerson` 方法。`PersonServiceHandler` 类是一个 thrift 服务端,实现了 `getPerson` 方法的具体逻辑。这些类的代码都是根据上述 thrift 定义文件自动生成的。同时,代码中也包含了使用 thrift 进行网络通信的相关代码,如 `TTransport`、`TProtocol` 和 `TSocket` 等类。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值