每种语言都有自己的优势和劣势 如何在一个应用里面取长补短,一个应用的一些功能可能同时需要几种不同需要语言的相互调用 这个怎么才能实现,
比如我需要在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;
}
}