thrift通讯协议
程序员文章站
2022-03-27 08:53:27
总结一下2017年的时候使用的技术1 java中的使用1.1 生成thrift文件thrift-generator下载一下源码,自己编译一下。pom.xml org.apache.thrift libthrift 0.10.0&...
总结一下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
待续。。。
本文地址:https://blog.csdn.net/warrah/article/details/111997815
上一篇: java_自定义异常类