window go thrift

2021-07-13 21:14

阅读:585

标签:技术   ast   ice   准备   could   1.5   图片   intel   创建目录   

代码在GitHub上托管

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

环境说明

  • windows 10
  • Intellij IDEA
  • thrift-0.11.0.exe
  • 服务端用java实现
  • 客户端用go实现
  • 用例的作用是,客户端将字符串传递给服务器,服务器将字符串转换成大写后,返回给客户端
     
     
     

    创建maven工程(父模块)

    删除自带的src目录(目前没用,删掉)

    技术分享图片
     
     

    准备IDL文件

  • 创建目录thrift
  • 创建IDL文件hw.thrift

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

    技术分享图片
     
     

    创建maven模块,用于存储生成的java版本的代码库

    技术分享图片
     
     

  • 利用thrift来生成java版本的代码库
    thrift --gen java -out ../java-thrift-idl/src/main/java hw.thrift
    技术分享图片
  • 生成的代码报错,是由于缺少libthrift库引起的,因此在pom.xml文件中,添加必要的依赖

    
    
        hw-thrift
        com.test.thrift1.0-SNAPSHOT4.0.0
    
    java-thrift-idl
    
    org.apache.thrift
            libthrift
            0.11.0org.apache.maven.plugins
                maven-compiler-plugin
                3.7.01.81.8

- 添加完依赖@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  
- 编写业务逻辑

## 测试  
- 启动服务器端 
- 启动客户端   

 
   
   
   

> 将服务端更改spring-boot 版本  
> 也就是更新java-thrift-server 模块  

- 更新java-thrift-server模块的pom文件,为下面的形式  


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.bootspring-boot-starter-parent1.5.3.RELEASE4.0.0java-thrift-serverorg.springframework.bootspring-boot-startercom.test.thriftjava-thrift-idl1.0-SNAPSHOT ``` - 添加appliaction.properties配置文件 > server.name=java-thrift-service server.port=8082 - 更新FormatDataImpl文件,添加@Service注解,也就是交给spring 管理起来 - 创建thirift_socket包,创建ThriftServer文件,编写Thrift逻辑 - 可能会报错,如果是could not autowire. no bean of Iface的话,可以不用管 - 更新server文件,添加上@SpringBootApplication 注解,表明是启动类 - 测试 > 启动服务端 启动客户端

window go thrift

标签:技术   ast   ice   准备   could   1.5   图片   intel   创建目录   

原文地址:http://blog.51cto.com/xingej/2167066


评论


亲,登录后才可以留言!