Apache Thrift 初识 (3) - 实战Demo

Wu Jun 2019-12-25 15:59:03
Categories: > > Tags:

1 下载Thrift编译器

2 创建.thrift脚本

使用 IDL 描述语言建立 query_media_service.thrift 脚本

namespace java com.xxxx.auto.db.api.thrift.service

/**
 * 视频&图片查询服务定义
 */
service AutoDBQueryMediaService {
    /**
     * 根据文件id查询媒体文件
     * @param appKey APPKEY
     * @param sign 签名串
     * @param time 当前时间
     * @param fileId 视频fileid
     * @return
     */
    string getMediaByFileId(1:string appKey, 2:string sign, 3:i64 time, 4:i64 fileId),
}

3 生成代码

将 .thrift 文件与 thrift-0.9.2.exe 放在同一目录

在命令提示符 CMD 中进入文件目录所在目录,执行代码生成命令:thrift-0.9.2.exe -gen java query_media_service.thrift

当前目录下会生成一个gen-java文件夹,里面是生成的 java 代码AutoDBQueryMediaService.java

4 创建Maven工程

4.1 加载依赖

在 pom.xml 中加载 Thrift 的依赖

<!-- https://mvnrepository.com/artifact/org.apache.thrift/libthrift -->
  <dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.9.2</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
  <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.7</version>
  </dependency>
4.2 拷贝代码

将上一步骤生成的gen-java文件夹中AutoDBQueryMediaService.java代码拷贝到项目

4.3 实现接口

创建LoginServiceImpl.java类,实现在LoginService.Iface接口

/**
 * @author wujun
 * @date 2018-03-27 15:35
 */
@ApiClass(name = "query-Media", description = "媒体查询服务")
@Service("queryMediaService")
public class QueryMediaService extends BaseService implements AutoDBQueryMediaService.Iface {

    @Override
    public String getMediaByFileId(String appKey, String sign, long time, long fileId) throws TException {
        return null;
    }
}
4.4 实现服务端
public class LoginMain {
    public static void main(String[] args) throws Exception {
        // Transport
        ServerSocket socket = new ServerSocket(8888);
        TServerSocket serverTransport = new TServerSocket(socket);
        // Processor
        LoginService.Processor processor = new Processor(new LoginServiceImpl());
        TServer.Args tServerArgs = new TServer.Args(serverTransport);
        tServerArgs.processor(processor);
        // Server
        TServer server = new TSimpleServer(tServerArgs);
        System.out.println("Starting the simple server...");
        server.serve();
    }
}
4.5 实现客户端
public class ClientMain {
    public static void main(String[] args) throws Exception {
        TTransport transport = null;
        try {
            // 创建TTransport
            transport = new TSocket("localhost", 8888);
            // 创建TProtocol 协议要与服务端一致
            TProtocol protocol = new TBinaryProtocol(transport);
            // 创建client
            LoginService.Client client = new LoginService.Client(protocol);
            transport.open();  // 建立连接
            
            Request request = new Request().setUsername("liulongling").setPassword("123456");
             
            // client调用server端方法
            System.out.println(client.doAction(request));
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            transport.close();  // 请求结束,断开连接
        }
    }
}

参考文档