美文网首页
thirft 使用

thirft 使用

作者: 该账号已被注销_e09b | 来源:发表于2020-12-13 16:55 被阅读0次

每种语言都有自己的优势和劣势 如何在一个应用里面取长补短,一个应用的一些功能可能同时需要几种不同需要语言的相互调用 这个怎么才能实现,
比如我需要在java里面同时调用Python,以及node 的一些具有语言特性的功能 有两种办法 HTTP 和 远程RPC
thirft可以说是跨语言的远程PRC调用我认为很好的了 他可以根据文件自动生成 java Python PHP node C++ 等一系列的语言的代码 保证了语言之间可以统一性
写好文件 rpc.thirft

//这里指明了代码生成之后,所处的文件路径
namespace java com.data.resources.dto
namespace py resources.dto
//将shrift的数据类型格式转换为java习惯的格式
typedef i16 short
typedef i32 int
typedef i64 long
typedef string String
typedef bool boolean

////定义数据异常
//exception DataException {
//    //optional 可选 非必传
//    1:optional int code,
//    2:optional String message,
//    3:optional String dateTime
//}

service rpcService {
    //required 必传项
    String getFont(1:required String data),

}

然后输入

thirft --gen java rpc.thirft 
thrift --gen python rpc.thirft
thirift --gen nodejs rpc.thirft

就可以的得到三种不同的文件
以下是的简单调用代码

服务端

import json
from resources.dto import rpcService
from decrypt.Dzdpfont import dataAnalysis
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
import socket


class TransmitHandler:
    def __init__(self):
        self.log = {}

    def getFont(self, font):
        data = json.loads(font)
        type_ = data['type']
        result = ""
        if type_ == 'dzdp':
            result = dataAnalysis(data)
        return result


if __name__ == "__main__":
    handler = TransmitHandler()
    processor = rpcService.Processor(handler)
    transport = TSocket.TServerSocket('127.0.0.1', 8000)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
    print("Starting python server...")
    server.serve()

调用端

import json
import sys
from resources.dto import rpcService
from resources.dto.ttypes import *
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol


transport = TSocket.TSocket('127.0.0.1', 8000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = rpcService.Client(protocol)
# Connect!
transport.open()

cmd = 2
token = '1111-2222-3333-4444'
data = json.dumps({"type":"dzdp","url":""})
msg = client.getFont(data)
print(msg)
transport.close()

以java为核心的相互调用

package com.data.resources.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.PostConstruct;

/**
 * @author Administrator
 * thrift配置文件
 */
@Configuration
public class ThriftNodeClientConfig {
    /**
     * Node服务端的地址
     */
    @Value("${server.thrift.NodeHost}")
    private String NodeHost;

    /**
     * Node服务端的端口
     */
    @Value("${server.thrift.NodePort}")
    private Integer NodePort;

    /**
     * Node服务端的地址
     */
    @Value("${server.thrift.PyHost}")
    private String PyHost;

    /**
     * Node服务端的端口
     */
    @Value("${server.thrift.PyPort}")
    private Integer PyPort;

    private static ThriftNodeClientConfig clientConfig;


    @PostConstruct
    public void init(){
        clientConfig=this;
        clientConfig.PyHost=this.PyHost;
        clientConfig.PyPort=this.PyPort;
        clientConfig.NodePort=this.NodePort;
        clientConfig.NodeHost=this.NodeHost;
    }

    /**
     * 每次请求实例化一个新的ThriftClient连接对象
     * @return
     */
    @Bean(initMethod = "init")
    @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public NodeThriftClient NodeInit() {
        NodeThriftClient thriftClient = new NodeThriftClient();
        thriftClient.setHost(NodeHost);
        thriftClient.setPort(NodePort);
        return thriftClient;
    }


    /**
     * 每次请求实例化一个新的ThriftClient连接对象
     * @return
     */
    @Bean(initMethod = "init")
    @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public PyThriftClient PyInit() {
        PyThriftClient thriftClient = new PyThriftClient();
        thriftClient.setHost(PyHost);
        thriftClient.setPort(PyPort);
        return thriftClient;
    }
    public static PyThriftClient getPyThriftClient(){
        PyThriftClient thriftClient = new PyThriftClient(clientConfig.PyHost,clientConfig.PyPort);
        return thriftClient;
    }
}

相关文章

  • thirft 使用

    每种语言都有自己的优势和劣势 如何在一个应用里面取长补短,一个应用的一些功能可能同时需要几种不同需要语言的相互调用...

  • Thirft

    一、About thrift二、什么是thrift,怎么工作?三、Thrift IDL四、Thrift D...

  • golang thirft

    mac安装 查看命令帮助

  • MAC Homebrew

    Homebrew安装查看版本Thirft安装node.js安装http-server 安装 Homebrew安装 ...

  • RPC 之 thirft

    1 概述 rpc(Remote Procedure Call) 即远程过程调用,直白一点就是一个节点调用另一个节点...

  • Thirft安装与验证

    我们一般在开发微服务的时候,都要首先开发那些依赖最少的服务,这里我们首先选择了信息服务进行开发,它只有发送短信与发...

  • 微服务学习 之 thrift安装

    学生课程管理系统简单微服务架构图: Thirft下载安装:安装的是thrift tarball,即tar包: 可以...

  • 使用使用

    1234546464

  • C++ STL 练手(vector的使用)

    vector的使用 list的使用 deque的使用 set的使用 map的使用 multiset的使用 mult...

  • C++ STL 练手(multimap的使用)

    vector的使用 list的使用 deque的使用 set的使用 map的使用 multiset的使用 mult...

网友评论

      本文标题:thirft 使用

      本文链接:https://www.haomeiwen.com/subject/hildmktx.html