之前用的xmlrpc配置过多,数据类型还要自己强制转化,现在换成hprose。

模块

pip install hprose

python server.py

#!/usr/bin/env python
# encoding: utf-8

import hprose


def hello(name):
    return 'Hello %s!' % name


def main():
    server = hprose.HttpServer(port=8181)
    server.addFunction(hello)
    server.handle('RPC')
    server.start()


if __name__ == '__main__':
    main()


maven环境

<dependency>
	<groupId>org.hprose</groupId>
	<artifactId>hprose-java</artifactId>
	<version>2.0.32</version>
</dependency>

Java client

    @RequestMapping("hprose")
    public void hprose(){
        HproseHttpClient client = new HproseHttpClient();
        client.useService("http://127.0.0.1:8181/RPC");

        //通过接口调用
        IService service = client.useService(IService.class);
        String content = service.hello("Jack");
        System.out.println("rpc调用,返回:" + content);

//        try {
//            //通过invoke调用
//            String content = client.invoke("hello", new Object[]{"Jack"}, String.class);
//            System.out.println("rpc调用,返回:" + content);
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }

    }

IService.java

package com.service;

public interface IService {
    public String hello(String name);
}

参考:1.Hprose轻松实现远程过程调用(RPC)

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐