记一次Thrift+swift踩坑经历

发布时间 2024-01-09 15:25:18作者: 朱俊升

最近来了个thrift+swift的分布式架构项目、所以就研究了一下这套远程调度框架的使用、中间踩了很多坑

首先介绍一下用到的几个插件和jar包

<plugin>
<groupId>com.facebook.mojo</groupId>
<artifactId>swift-maven-plugin</artifactId>
<version>${swift-version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>false</skip>
<idlFiles>
<directory>${project.basedir}/src/main/thrift/</directory>
<includes>
<include>**/*.thrift</include>
</includes>
<!--<excludes>-->
<!--<exclude>**/other.thrift</exclude>-->
<!--</excludes>-->
</idlFiles>
<defaultPackage>${project.groupId}.thrift.hello</defaultPackage>
<outputFolder>${project.basedir}/src/main/java/</outputFolder>
</configuration>
</plugin>
该插件是生成服务端接口的插件、搭建的测试环境上用的版本是0.23.1

<dependency>
<groupId>com.facebook.swift</groupId>
<artifactId>swift-service</artifactId>
<version>${swift-version}</version>
</dependency>
<dependency>
<groupId>com.facebook.swift</groupId>
<artifactId>swift-annotations</artifactId>
<version>${swift-version}</version>
</dependency>
这两个jar包是通过注解来简化生成的服务端API代码的、这两个jar包和swift-maven-plugin插件版本保持一致。
<!--0.9.1-0.13.0-->
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.13.0</version>
</dependency>
最坑的是这个jar包版本的选择上、自测版本从0.9.1到0.13.0都可以、再往后的版本就不行了
一直报如下异常

Exception in thread "main" org.apache.thrift.transport.TTransportException: Could not find sequenceId in Thrift message
at com.facebook.nifty.client.AbstractClientChannel.extractSequenceId(AbstractClientChannel.java:102)
at com.facebook.nifty.client.AbstractClientChannel.sendAsynchronousRequest(AbstractClientChannel.java:174)
at com.facebook.swift.service.SyncClientHelpers.sendSynchronousTwoWayMessage(SyncClientHelpers.java:52)
at com.facebook.swift.service.ThriftMethodHandler.synchronousInvoke(ThriftMethodHandler.java:164)
at com.facebook.swift.service.ThriftMethodHandler.invoke(ThriftMethodHandler.java:131)
at com.facebook.swift.service.ThriftClientManager$ThriftInvocationHandler.invoke(ThriftClientManager.java:520)
at com.sun.proxy.$Proxy6.ping(Unknown Source)
at com.deye.client.ThriftClientDemo.main(ThriftClientDemo.java:16)
Caused by: java.lang.AbstractMethodError: org.apache.thrift.transport.TTransport.checkReadBytesAvailable(J)V
at org.apache.thrift.protocol.TBinaryProtocol.checkStringReadLength(TBinaryProtocol.java:500)
at org.apache.thrift.protocol.TBinaryProtocol.readStringBody(TBinaryProtocol.java:472)
at org.apache.thrift.protocol.TBinaryProtocol.readString(TBinaryProtocol.java:468)
at org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin(TBinaryProtocol.java:278)
at com.facebook.nifty.client.AbstractClientChannel.extractSequenceId(AbstractClientChannel.java:98)
... 7 more

请大家以此为戒

框架搭建以及使用方法可参考杨过大侠的这篇文章 https://www.cnblogs.com/yjmyzz/p/thrift-swift-sample.html