RPC hprose Java调用Python代码
模块pip install hprosepython server.py#!/usr/bin/env python# encoding: utf-8import hprosedef hello(name):return 'Hello %s!' % namedef main():server = hprose.HttpServer(port=8181)server.addFunction(hello
·
之前用的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);
}
更多推荐
已为社区贡献3条内容
所有评论(0)