java通过xml-rpc调用Python的rpc接口

本文章的java后端是基于springboot框架实现。

一、引入相关依赖

首先pom中需加入如下依赖包:

        <!--    XMLRPC依赖    -->
        <dependency>
            <groupId>org.apache.xmlrpc</groupId>
            <artifactId>xmlrpc-server</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlrpc</groupId>
            <artifactId>xmlrpc-client</artifactId>
            <version>3.1.3</version>
        </dependency>
        <!--    httpclient(此依赖是配置接口连接超时时间所需的)    -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

二、后端实现代码

以下的代码功能为调用Python服务端的rpc接口

    @Override
    public void xmlRpcForPython() throws MalformedURLException {
     
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        //服务端url后面需要加上“/RPC2”,用于声明请求是rpc协议
        config.setServerURL(new URL("http://127.0.0.1:8000/RPC2"));
        config.setEnabledForExtensions(true);
        config.setEnabledForExceptions(true);
        //配置接口连接超时时间,目前均设置为10s
        config.setConnectionTimeout(10 * 1000);
        config.setReplyTimeout(10 * 1000);
        XmlRpcClient client = new XmlRpcClient();
        client.setTransportFactory(new XmlRpcCommonsTransportFactory(client));
        client.setConfig(config);
        // 根据不同的python函数形式,构造参数
        // 两个整形参数
        Object[] params1 = new Object[] {
     new Integer(2), new Integer(9)};

        // 单个字符串参数
        Object[] params2 = new Object[] {
     new String("aaa")};
        
        // 单个字符串参数
        Object[] params3 = new Object[] {
     new String("aaa"), new String("bbb")};

        // 无参数
        //Object[] params = null;
        try {
     
            //Integer res = (Integer) client.execute("pow", params1);
            //返回的结果是字符串类型,强制转换res为String类型
            //其中“add”为rpc接口名,params3为接口所需参数
            String res = (String) client.execute("add", params3);
            System.out.println(res);
        } catch (XmlRpcException e) {
     
            e.printStackTrace();
        }
    }

三、Python相关代码

服务端代码(以下代码就是举个栗子,具体业务自己修改)

from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler
import time

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)


# Create server
server = SimpleXMLRPCServer(("127.0.0.1", 8000),
                            requestHandler=RequestHandler)
server.register_introspection_functions()

# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)


# Register a function under a different name
def adder_function(x, y):
    #time.sleep(30)
    return x + y


server.register_function(adder_function, 'add')


# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'div').
class MyFuncs:
    def div(self, x, y):
        return x // y


server.register_instance(MyFuncs())

# Run the server's main loop
server.serve_forever()

客户端代码(用来测试你的Python服务端的rpc接口是否有效)

import xmlrpc.client

server = xmlrpc.client.ServerProxy("http://127.0.0.1:8000")

print(server.pow(2, 9))
print(server.add("abc", "32321"))
server.add(24, 11)

(注:以上的代码都是本人通过百度各种搜索,从多篇文章中摘取并做了一部分修改后汇总所得,如有侵权请联系本人!)

你可能感兴趣的:(技术文档,java,python,spring,boot)