日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

java thrift client_使用thrift的java client调用python server

發(fā)布時間:2024/9/19 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java thrift client_使用thrift的java client调用python server 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

上面這篇文章的例子是使用java client調(diào)用python server中的helloString方法來打印client傳輸過去的字符串

thrift文件,hello.thrift

service Hello {

string helloString(1:string word)

}

Server端

生成Python server端代碼

thrift --gen py hello.thrift

python server端代碼,其中包括生成的hello文件夾中的代碼,以及server代碼

from thrift.protocol import TBinaryProtocol

from thrift.server import TServer

from thrift.transport import TSocket

from thrift.transport import TTransport

from hello import Hello

class HelloHandler:

def __init__(self):

pass

def helloString(self, word):

ret = "hello Thrift! Received: " + word

return ret

# handler processer類

handler = HelloHandler()

processor = Hello.Processor(handler)

transport = TSocket.TServerSocket("127.0.0.1", 8989)

# 傳輸方式,使用buffer

tfactory = TTransport.TBufferedTransportFactory()

# 傳輸?shù)臄?shù)據(jù)類型:二進(jìn)制

pfactory = TBinaryProtocol.TBinaryProtocolFactory()

# 創(chuàng)建一個thrift 服務(wù)~

server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)

print("Starting thrift server in python...")

server.serve()

print("done!")

Client端

生成java client代碼

thrift --gen java hello.thrift

pom文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.example

thrift-example

1.0-SNAPSHOT

org.apache.thrift

libthrift

0.10.0

java client的代碼

package com.example.tutorial;

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;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;

import java.lang.reflect.Method;

public class ThriftFactory {

private ThriftFactory() {

}

private static final Logger LOG = LoggerFactory.getLogger(ThriftFactory.class);

private static TProtocol protocol;

private static TTransport transport;

/**

* 獲取二進(jìn)制 protocol

*

* @return 二進(jìn)制 protocol

*/

public static TProtocol getTProtocol() {

// 單例獲取 protocol

if (protocol == null) {

protocol = new TBinaryProtocol(getTTransport());

}

return protocol;

}

/**

* 獲取傳輸對象

*

* @return 傳輸對象

*/

public static TTransport getTTransport() {

// 單例獲取 transport

if (transport == null) {

// 應(yīng)改成從配置文件讀取

String ip = "127.0.0.1";

Integer port = 8989;

transport = new TSocket(ip, port);

}

return transport;

}

/**

* 獲取客戶端實(shí)例

*

* @param clazz 客戶端類

* @param 泛型

* @return 客戶端實(shí)例

*/

public static T getClient(Class clazz) {

T instance = null;

try {

//獲取有參構(gòu)造器

Constructor c = clazz.getConstructor(TProtocol.class);

// 實(shí)例化客戶端,需要傳入 protocol

instance = (T) c.newInstance(getTProtocol());

} catch (Exception e) {

LOG.error("", e);

throw new RuntimeException(e.getMessage());

}

return instance;

}

/**

* 發(fā)起請求

*

* @param clazz 客戶端類

* @param methodName 方法名,客戶端中不能有重載的方法

* @param param 方法參數(shù)

* @param 泛型

* @return 方法返回值

*/

public static Object doRequest(Class clazz, String methodName, Object... param) {

Object result = null;

try {

// 獲取客戶端實(shí)例

T instance = getClient(clazz);

Method[] methods = clazz.getMethods();

for (Method method : methods) {

// 獲取指定的方法

if (method.getName().equals(methodName)) {

open();

result = method.invoke(instance, param);

close();

break;

}

}

} catch (Exception e) {

LOG.error("", e);

throw new RuntimeException(e.getMessage());

}

return result;

}

/**

* 打開傳輸

*/

public static void open() {

try {

getTTransport().open();

} catch (TTransportException e) {

LOG.error("", e);

throw new RuntimeException(e.getMessage());

}

}

/**

* 關(guān)閉傳輸

*/

public static void close() {

getTTransport().close();

}

}

client主函數(shù)

package com.example.tutorial;

public class ThriftExample {

public static void main(String[] args) {

String msg = (String) ThriftFactory.doRequest(com.example.tutorial.Hello.Client.class, "helloString", "測試");

System.out.println(msg);

}

}

運(yùn)行python server

運(yùn)行java client,調(diào)用了python的helloString方法

總結(jié)

以上是生活随笔為你收集整理的java thrift client_使用thrift的java client调用python server的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。