thrift通讯协议

总结一下2017年的时候使用的技术
1 java中的使用
1.1 生成thrift文件
thrift-generator下载一下源码,自己编译一下。
pom.xml

<dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.13.0</version>
        </dependency>

        <dependency>
            <groupId>com.sohu.thrift</groupId>
            <artifactId>thrift-generator</artifactId>
            <version>0.1.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
            <version>4.13.1</version>
        </dependency>

按照下面的代码,生成thrift文件,将输出的内容,保存到一个.thrift文件。

import com.sohu.thrift.generator.builder.ThriftFileBuilder;
import org.junit.Test;

public class ThriftGen {

    private ThriftFileBuilder fileBuilder = new ThriftFileBuilder();

    @Test
    public void toOutputstream() throws Exception {
        this.fileBuilder.buildToOutputStream(PersonService.class, System.out);
    }
}

import java.util.List;

public class Person {

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public SexEnum getSex() {
        return sex;
    }

    public void setSex(SexEnum sex) {
        this.sex = sex;
    }

    private String code;

    private String name;

    private SexEnum sex;

    private List<Favorite> favoriteList;
}


public class Favorite {

    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
}



public enum  SexEnum {
    MAN,
    WOMAN,
    ALL;
}
// 生成相应的thrift文件
namespace java com.dzmsoft.thriftdemo.api.service.thrift

enum SexEnum {
        MAN = 0,
        WOMAN = 1,
        ALL = 2
}

struct Favorite {
        1:string id,
        2:string name
}
struct Person {
        1:string code,
        2:string name,
        3:SexEnum sex,
        4:list<Favorite> favoriteList
}


service PersonService {
        bool save(1:Person arg0),
        list<Favorite> getPersons(1:string arg0)
}
	

数据类型还是要简单点好,复杂了,还是会有问题,比如下方定义的枚举类型,生成的结构就会很奇怪,这个肯定直接用不了。

package com.dzmsoft.thriftdemo.test.pojo;

public enum  SexEnum {
    MAN("男","00"),
    WOMAN("女","01"),
    ALL("全部","02");

    private final String display;
    private final String value;

    private SexEnum(String display , String value){
        this.value = value;
        this.display = display;
    }

    public String display(){
        return this.display;
    }

    public String value(){
        return this.value;
    }

    public static SexEnum getSexEnum(String value){
        SexEnum current = null;
        for (SexEnum sexEnum:SexEnum.values()){
            if (sexEnum.value().equals(value)){
                current = sexEnum;
                break;
            }
        }
        return current;
    }
}
enum SexEnum {
			MAN = 0,
			WOMAN = 1,
			ALL = 2,
			display = 3,
			value = 4
	}

1.2 根据thrift生成python的代码
根据thrift文件生成python相关的代码?
apache thrift中下载thrift压缩包,然后到thrift install教程,我这里用的mac电脑
boost library
libevent

cd /Users/dzm/Downloads/boost_1_75_0
./bootstrap.sh
sudo ./b2 threading=multi address-model=64 variant=release stage install

# 这里其实应该下载稳定的版本,直接下载对应的master中的,所以可能是alpha版本,安装需要一定时间,在mac采用下面的方式
192:libevent-2.1.12-stable dzm$ openssl version
OpenSSL 0.9.8zh 14 Jan 2016

# /usr/local/Cellar/openssl
brew update
brew install openssl
# 上面的操作,还不如执行下面的
brew reinstall openssl



# libevent依赖openssh,所以安装这个之前,先把openssl安装了
cd /Users/dzm/Downloads/libevent-2.1.12-stable

make && make install

cd /Applications/apps/thrift-0.13.0

安装libevent的时候提示的错误,这里

configure: error: openssl is a must but can not be found. You should add the directory containing `openssl.pc' to the `PKG_CONFIG_PATH' environment variable, or set `CFLAGS' and `LDFLAGS' directly for openssl, or use `--disable-openssl' to disable support for openssl encryption

更改brew的仓库路径,这里参考了mac使用brew update无反应,更新慢解决办法

# 替换brew
cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

# 替换homebrew-core
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

# 替换homebrew-bottles 访问地址
192:homebrew-core dzm$ echo $SHELL
/bin/bash

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

# 如果要撤销上面的操作
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
vi ~/.bash_profile
# 然后,删除 HOMEBREW_BOTTLE_DOMAIN 这一行配置
source ~/.bash_profile

待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
thrift-generator 是通过 Java 的接口生成 thrift 文件的工具。例子:public interface ICommonUserService {     public User login(int id, String name);     public User getUserById(long id);     public boolean saveUser(User user);     public List getUserIds(long id);      public Map getUserByIds(List ids);     public Map<String, List> getUsersByName(List names);     public Map<Long, List> getGroupUsers(List names, List userList, List lns, long ll);     public List testCase1(Map num1, List num2, List num3, long num4, String num5); } public class ThriftStructBuilderTest {     private ThriftFileBuilder fileBuilder = new ThriftFileBuilder();     @Test     public void toOutputstream() throws Exception {         this.fileBuilder.buildToOutputStream(ICommonUserService.class, System.out);     } }执行代码:mvn test -Dtest=com.sohu.thrift.generator.builder.ThriftStructBuilderTest之后控制台输出如下:namespace java com.sohu.thrift.generator.test.thrift     enum Status {             NORMAL = 0,             BLOCKED = 1     }     struct Account {             1:i32 id,             2:string name     }     struct User {             1:i32 id,             2:string name,             3:bool sex,             4:Status status,             5:list ids,             6:Account account     }     service ICommonUserService {             User login(1:i32 arg0,2:string arg1),             map<string, list> getUsersByName(1:list arg0),             bool saveUser(1:User arg0),             map getUserByIds(1:list arg0),             list getUserIds(1:i64 arg0),             map<i64, list> getGroupUsers(1:list arg0,2:list arg1,3:list arg2,4:i64 arg3),             User getUserById(1:i64 arg0),             list testCase1(1:map arg0,2:list arg1,3:list arg2,4:i64 arg3,5:string arg4)     }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

warrah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值