Window Go Thrift

Source: Internet
Author: User

The code is hosted on GitHub

Https://github.com/xej520/xingej-thrift/tree/master/hw-thrift

Environment description

  • Windows 10
  • Intellij idea
  • Thrift-0.11.0.exe
  • Server-side implementation with Java
  • The client is implemented with Go
  • The purpose of the use case is that the client passes the string to the server, and the server converts the string to uppercase and returns it to the client


    Create a MAVEN project (parent module)

    Delete your own src directory (currently useless, delete)



    Preparing IDL files

  • Create Directory Thrift
  • Create IDL file Hw.thrift

    namespace java com.test.thriftnamespace  go pkg.servicestruct Data {    1: string text;}service format_data {    Data doFormat(1:Data data);}



    Create a MAVEN module to store the generated Java version of the code base



  • Using thrift to generate a Java version of the code base
    Thrift--gen Java-out. /java-thrift-idl/src/main/java Hw.thrift
  • The generated code error is caused by the lack of a libthrift library, so add the necessary dependencies in the Pom.xml file

    <?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "><parent> <artifactId>hw-thrift</artifactId> < Groupid>com.test.thrift</groupid> <version>1.0-SNAPSHOT</version></parent>< Modelversion>4.0.0</modelversion><artifactid>java-thrift-idl</artifactid><dependencies > <dependency> <groupId>org.apache.thrift</groupId> <artifactid>libthrift</ artifactid> <version>0.11.0</version> </dependency></dependencies><build> &L t;plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <arti Factid>maven-compiler-plugin</artifactid> <version>3.7.0</version> <configuration> <source>1.8</source> <targe t>1.8</target> </configuration> </plugin> </plugins></build>

</project>

- 添加完依赖@Override 注解报错的话,可能是由于thrift的版本不一致引起的。## 创建go模块,用于存储生成go版本的代码库  - 利用thrift来生成go版本的代码库      thrift --gen go  -out ../go-thrift-idl hw.thrift![](https://note.youdao.com/yws/public/resource/9c8584c6ec980aee585665c38a65bf9d/xmlnote/42EAB0BE237340D6A8989EEF383753DE/20079)- 注意生成的代码是有问题的[原因具体不详]    - 在hw.go文件中 oprot.Flush()抛异常,not enough arguments in call to oprot.Flush less... (Ctrl+F1)    - 解决措施:原因是缺少context.Context类型的参数,刚好方法中已经有,直接添加上就可以了,改成oprot.Flush(ctx)  - 将生成的代码库pkg 拷贝到gopath的src路径下,这样客户端就可以使用代码库了,不然有可能找不到生成的代码库## 创建maven模块,用于创建服务端  - 创建maven模块,java-thrift-server - 更新pom.xml文件,添加对java-thrift-idl的依赖 - 编写FormatDataImpl实现hw.thrift定义的接口- 编写sever, 实现thrift编程## 创建go模块,用于go版本的客户端  - 创建go模块,go-thrift-client  - 编写业务逻辑## 测试  - 启动服务器端 - 启动客户端   &nbsp;&nbsp;  &nbsp;  &nbsp;  > 将服务端更改spring-boot 版本  > 也就是更新java-thrift-server 模块  - 更新java-thrift-server模块的pom文件,为下面的形式  

<?xml version= "1.0" encoding= "UTF-8"?>
<project xmlns= "http://maven.apache.org/POM/4.0.0"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >;

org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE 4.0.0 Java-thrift-server org.springframework.boot spring-boot-starter com.test.thrift JAV A-thrift-idl 1.0-snapshot "-Add appliaction.properties config file > server.name=java-thrift-service server.port=8082-update Formatdataimpl file, Add @service annotations, which are given to spring to manage-create thirift_socket packages, create thriftserver files, write thrift logic-may error, if could not autowire. No bean of iface words, can not tube-update the server file, add the @springbootapplication annotation, indicating is the startup class-Test > Start Service side start client
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.