Java使用Thrift,Thrift结构体定义

1、Thrift定义文件,Thrift常见的数据类型

1.基本类型(括号内为对应的Java类型):
bool(boolean): 布尔类型(TRUE or FALSE)
bytebyte): 8位带符号整数
i16(short): 16位带符号整数
i32(int): 32位带符号整数
i64(long): 64位带符号整数
doubledouble): 64位浮点数
string(String): 采用UTF-8编码的字符串
2.特殊类型(括号内为对应的Java类型):
    binary(ByteBuffer):未经过编码的字节流
3.Structs(结构===JAVA(对象))
4.容器 Thrift提供了3种容器类型:
List<Object>:一系列t1类型的元素组成的有序表,元素可以重复
Set<Object>:一系列t1类型的元素组成的无序表,元素唯一
Map<Object,Object>:key/value对(key的类型是t1且key唯一,value类型是t2)。
    容器中的元素类型可以是除了service意外的任何合法thrift类型(包括结构体和异常)。
5.其他**
    namespace 相当于java创建包 java com.thrift.membersheep.server 说明创建java 的包
    service 相当于java 创建Class

2、Thrift简单结构体demo,写好了过后保存文件名后缀已.thrift结尾,如(ETH_CORE.thrift)

namespace java com.thrift.eth
//交易所_创建eth账号 struct EXCHANGE_CREATE_ETH_ACCOUNT_MODEL{ 1: string path, //账号存放地址 2: string password,//密码 3: string phone_filename//账号名称 } //交易所_返回创建eth账号信息 struct EXCHANGE_RETURN_ETH_ACCOUNT{ 1: string address;//账号地址 2: string privateKey;//私钥 3: string publicKey;//公钥 4: string password;//密码 5: double coinQuantity;//货币数量 6: string message;//消息 7: i32 status;//状态 } //交易所_转账eth struct EXCHANGE_TRANSFER_ETH_MODEL{ 1: string https_web3j,//web3j url 2: string path_file,//账号文件路径 3: string address,//账号地址 4: string password,//密码 5: string to_address,//转到那个账号地址 6: double quantity//转账数量 } //交易所_返回转账信息 struct EXCHANGE_RETURN_TRANSFER{ 1: string message;//消息 2: i32 status;//状态 } //交易所_转账sheep struct EXCHANGE_TRANSFER_SHEEP_MODEL{ 1: string https_web3j,//web3j url 2: string path_file,//账号文件路径 3: string address,//账号地址 4: string password,//密码 5: string to_address,//转到那个账号地址 6: double quantity,//转账数量 7: string contract_address//合约地址 } service ETH_CORE{//===========================================exhagne eth相关操作 //创建eth账号 EXCHANGE_RETURN_ETH_ACCOUNT create_eth_address(1:EXCHANGE_CREATE_ETH_ACCOUNT_MODEL create_eth_model) //eth转账 EXCHANGE_RETURN_TRANSFER transfer_eth(1:EXCHANGE_TRANSFER_ETH_MODEL transfer_eth_model) //获取eth余额 double get_eth_balance(1:string https_web3j,2:string address) //sheep转账 EXCHANGE_RETURN_TRANSFER transfer_sheep(1:EXCHANGE_TRANSFER_SHEEP_MODEL transfer_sheep_model) //获取sheep余额 double get_sheep_balance(1:string https_web3j,2:string address,3:string contract_address) }

3、下载客服端http://thrift.apache.org/download,

4、反编译成java文件(windows版),把写好的thrift结构体放到与客服端同级

 然后点击地址栏输入cmd回车运行

 反编译成java命令:thrift-0.10.0 -gen java ETH_CORE.thrift 如:

回车过后文件夹下面就会自动生成java文件

 

然后点击去看java文件就生成好了

然后到java端使用

 1、maven jar使用

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

2、先把核心包使用,就是开始创建的结构体,单独作用于一个项目,当做jar使用

3、服务器使用 pom.xml也是要引用ThriftCore项目

<dependency>
   <groupId>com.lpizi</groupId>
   <artifactId>ThriftCore</artifactId>
    <version>1.0-SNAPSHOT</version>
    <exclusions>
        <exclusion>
            <groupId>jline</groupId>
             <artifactId>jline</artifactId>
         </exclusion>
    </exclusions>
</dependency>

 

3.1、ThriftServer类

package com.thrift.server;


import com.thrift.eth.ETH_CORE;
import com.thrift.web3j.DefaultEth;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

public class ThriftServer {public static void main(String[] args){
        try {
            // 设置服务器端口
            TServerSocket serverTransport = new TServerSocket(端口);
            // 设置二进制协议工厂
            TBinaryProtocol.Factory protocolFactory = new TBinaryProtocol.Factory();
            // 处理器关联业务实现
            ETH_CORE.Processor<ETH_CORE.Iface> processor = new ETH_CORE.Processor<ETH_CORE.Iface>((ETH_CORE.Iface) new EthServiceImpl());
            //使用单线程标准阻塞I/O模型
            TServer.Args simpleArgs = new TServer.Args(serverTransport)
                    .processor(processor)
                    .protocolFactory(protocolFactory);
            TServer server = new TSimpleServer(simpleArgs);
            System.out.println("=====>开启thrift服务器,监听端口:"+DefaultEth.ThrifProp+"<=================");
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }

}

3.2、EthServiceImpl类

package com.thrift.server;

import com.alibaba.fastjson.JSON;
import com.membersheep.ethereum.model.ConfirmModel;
import com.membersheep.ethereum.model.OrderDetailModel;
import com.membersheep.ethereum.model.exchange.AccountModel;
import com.membersheep.ethereum.model.exchange.TransferModel;
import com.membersheep.ethereum.web3j.TransctionContract;
import com.membersheep.ethereum.web3j.exchange.EthAccount;
import com.thrift.eth.*;
import org.apache.thrift.TException;

import java.util.ArrayList;
import java.util.List;

public class EthServiceImpl implements ETH_CORE.Iface {
    private EthAccount ethAccount=new EthAccount();
  /*
   注意方法里面的内容是根据你自己的需求来操作,我这里是操作eth相关的 
  */
/** * 创建ETH账号 * @param create_eth_model * @return * @throws TException */ @Override public EXCHANGE_RETURN_ETH_ACCOUNT create_eth_address(EXCHANGE_CREATE_ETH_ACCOUNT_MODEL create_eth_model) throws TException { EXCHANGE_RETURN_ETH_ACCOUNT returnEthAccount=new EXCHANGE_RETURN_ETH_ACCOUNT(); AccountModel accountModel=ethAccount.create(create_eth_model.path,create_eth_model.password,create_eth_model.phone_filename); returnEthAccount.address=accountModel.address; returnEthAccount.coinQuantity=accountModel.coinQuantity; returnEthAccount.message=accountModel.message; returnEthAccount.password=accountModel.password; returnEthAccount.privateKey=accountModel.privateKey; returnEthAccount.publicKey=accountModel.publicKey; returnEthAccount.status=accountModel.status; return returnEthAccount; } /** * eth转账 * @param transfer_eth_model * @return * @throws TException */ @Override public EXCHANGE_RETURN_TRANSFER transfer_eth(EXCHANGE_TRANSFER_ETH_MODEL transfer_eth_model) throws TException { EXCHANGE_RETURN_TRANSFER returnTransfer=new EXCHANGE_RETURN_TRANSFER(); TransferModel transferModel=ethAccount.transferETH(transfer_eth_model.https_web3j,transfer_eth_model.path_file,transfer_eth_model.address,transfer_eth_model.password,transfer_eth_model.to_address,transfer_eth_model.quantity); returnTransfer.message=transferModel.message; returnTransfer.status=transferModel.status; return returnTransfer; } /** * 获取eth余额 * @param https_web3j * @param address * @return * @throws TException */ @Override public double get_eth_balance(String https_web3j, String address) throws TException { return ethAccount.getETHBalance(https_web3j,address); } /** * sheep转账 * @param transfer_sheep_model * @return * @throws TException */ @Override public EXCHANGE_RETURN_TRANSFER transfer_sheep(EXCHANGE_TRANSFER_SHEEP_MODEL transfer_sheep_model) throws TException { EXCHANGE_RETURN_TRANSFER returnTransfer=new EXCHANGE_RETURN_TRANSFER(); TransferModel transferModel=ethAccount.transferSHEEP(transfer_sheep_model.https_web3j,transfer_sheep_model.path_file, transfer_sheep_model.address,transfer_sheep_model.password,transfer_sheep_model.to_address, transfer_sheep_model.quantity,transfer_sheep_model.contract_address); returnTransfer.message=transferModel.message; returnTransfer.status=transferModel.status; return returnTransfer; } /** * 获取sheep余额 * @param https_web3j * @param address * @param contract_address * @return * @throws TException */ @Override public double get_sheep_balance(String https_web3j, String address, String contract_address) throws TException { return ethAccount.getSHEEPBalance(https_web3j,address,contract_address); } }

end 这上面服务端就好了

4、客户端使用pom.xml也是要引用ThriftCore项目(同服务端调用)如下这个两个类就OK了

4.1、GetServer类

package com.membersheep.util.thrift;

import com.membersheep.util.proper.DefaultProp;
import com.thrift.eth.ETH_CORE;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class GetServer {
    /**
     * 获取通讯链接
     * @return
     */
    public TTransport get_transport(){
        // 设置调用的服务地址-端口
        return new TSocket(服务端URL, 服务端端口);
    }

    /**
     * 打开通道
     * @return
     * @throws TTransportException
     */
    public ETH_CORE.Client open_client(TTransport transport) throws TTransportException {
        // 使用二进制协议
        TProtocol protocol = new TBinaryProtocol(transport);
        // 使用的接口
        ETH_CORE.Client client = new ETH_CORE.Client(protocol);
        // 打开socket
        transport.open();
        return client;
    }
}

4.2、ThriftExchangeEthClient类,还是那句话方法里面的东西根据自己的需求定义

package com.membersheep.util.thrift;


import com.thrift.eth.*;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ThriftExchangeEthClient {
    private static Logger logger= LoggerFactory.getLogger(ThriftExchangeEthClient.class);

    /**
     * 创建eth账号
     * @param create_eth_account_model
     * @return
     */
    public EXCHANGE_RETURN_ETH_ACCOUNT create_eth_address(EXCHANGE_CREATE_ETH_ACCOUNT_MODEL create_eth_account_model){
        EXCHANGE_RETURN_ETH_ACCOUNT returnEthAccount=new EXCHANGE_RETURN_ETH_ACCOUNT();
        GetServer getServer=new GetServer();
        TTransport transport=getServer.get_transport();
        try {
            ETH_CORE.Client client = getServer.open_client(transport);
            returnEthAccount=client.create_eth_address(create_eth_account_model);
        } catch (TTransportException e) {
            logger.error("create_eth_address==>TTransportException====>"+e.getMessage());
        } catch (TException e) {
            logger.error("create_eth_address==>TException====>"+e.getMessage());
        }finally {
            transport.close();
        }
        return returnEthAccount;
    }

    /**
     * eth转账
     * @param transfer_eth_model
     * @return
     */
    public EXCHANGE_RETURN_TRANSFER transfer_eth(EXCHANGE_TRANSFER_ETH_MODEL transfer_eth_model){
        EXCHANGE_RETURN_TRANSFER returnTransfer=new EXCHANGE_RETURN_TRANSFER();
        GetServer getServer=new GetServer();
        TTransport transport=getServer.get_transport();
        try {
            ETH_CORE.Client client = getServer.open_client(transport);
            returnTransfer=client.transfer_eth(transfer_eth_model);
        } catch (TTransportException e) {
            logger.error("transfer_eth==>TTransportException====>"+e.getMessage());
        } catch (TException e) {
            logger.error("transfer_eth==>TException====>"+e.getMessage());
        }finally {
            transport.close();
        }
        return returnTransfer;
    }

    /**
     * 获取eth余额
     * @param https_web3j
     * @param address
     * @return
     */
    public double get_eth_balance(String https_web3j,String address){
        GetServer getServer=new GetServer();
        TTransport transport=getServer.get_transport();
        double balance=0;
        try {
            ETH_CORE.Client client = getServer.open_client(transport);
            balance=client.get_eth_balance(https_web3j,address);
        } catch (TTransportException e) {
            logger.error("get_eth_balance==>TTransportException====>"+e.getMessage());
        } catch (TException e) {
            logger.error("get_eth_balance==>TException====>"+e.getMessage());
        }finally {
            transport.close();
        }
        return balance;
    }

    /**
     * 转账sheep
     * @param transfer_sheep_model
     * @return
     */
    public EXCHANGE_RETURN_TRANSFER transfer_sheep(EXCHANGE_TRANSFER_SHEEP_MODEL transfer_sheep_model){
        EXCHANGE_RETURN_TRANSFER returnTransfer=new EXCHANGE_RETURN_TRANSFER();
        GetServer getServer=new GetServer();
        TTransport transport=getServer.get_transport();
        try {
            ETH_CORE.Client client = getServer.open_client(transport);
            returnTransfer=client.transfer_sheep(transfer_sheep_model);
        } catch (TTransportException e) {
            logger.error("transfer_sheep==>TTransportException====>"+e.getMessage());
        } catch (TException e) {
            logger.error("transfer_sheep==>TException====>"+e.getMessage());
        }finally {
            transport.close();
        }
        return returnTransfer;
    }

    /**
     * 获取sheep余额
     * @param https_web3j
     * @param address
     * @param contract_address
     * @return
     */
    public double get_sheep_balance(String https_web3j,String address,String contract_address){
        GetServer getServer=new GetServer();
        TTransport transport=getServer.get_transport();
        double balance=0;
        try {
            ETH_CORE.Client client = getServer.open_client(transport);
            balance=client.get_sheep_balance(https_web3j,address,contract_address);
        } catch (TTransportException e) {
            logger.error("get_sheep_balance==>TTransportException====>"+e.getMessage());
        } catch (TException e) {
            logger.error("get_sheep_balance==>TException====>"+e.getMessage());
        }finally {
            transport.close();
        }
        return balance;
    }
}

这几OJBK了

 

转载于:https://www.cnblogs.com/longpizi/p/10728034.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值