jprotobuf-rpc-http 是应用jprotobuf类库实现基于http协议的RPC开发组件。 目前1.0提供可以直接把Google protobuf的IDL定义语言发布成RPC服务,客户端也可以直接应用IDL定义语言进行动态创建,帮助开发完全省去了手工编译protobuf IDL语言的麻烦。
jprotobuf-rpc-http文档: https://github.com/jhunters/JProtobuf-rpc-http
jprotobuf文档:https://github.com/jhunters/jprotobuf
环境要求
JDK 6 或以上版本 Spring 3.0+
API使用说明
RPC服务的发布
a1 在Spring配置文件,定义IDLServiceExporter服务发布配置 (直接由IDL定义发布)
<property name="serviceName" value="SimpleIDLTest"></property>
<property name="invoker" ref="simpleIDLInvoker"></property>
<property name="inputIDL" value="classpath:/simplestring.proto"></property>
<property name="outputIDL" value="classpath:/simplestring.proto"></property>
</bean>
<bean id="simpleIDLInvoker" class="com.baidu.bjf.SimpleIDLInvoker"></bean>
inputIDL 属性表示接收的protobuf协议定义 outputIDL 属性表示返回的protobuf协议定义 serviceName 服务名称,必须填写。 在服务的servlet发布后,服务名称会以path路径方式查找 invoker 服务回调实现,必须实现 com.baidu.jprotobuf.rpc.server.ServerInvoker接口
a2 在Spring配置文件,定义AnnotationServiceExporter服务发布配置 (通过Jprotobuf注解类发布)
<property name="serviceName" value="SimpleIDLTest2"></property>
<property name="invoker" ref="simpleIDLInvoker"></property>
<property name="inputClass" value="com.baidu.bjf.StringMessagePOJO"></property>
<property name="outputClass" value="com.baidu.bjf.StringMessagePOJO"></property>
</bean>
<bean id="simpleIDLInvoker" class="com.baidu.bjf.SimpleIDLInvoker"></bean>
inputClass 属性表示接收的JProtobuf注解POJO类 outputClass 属性表示返回的JProtobuf注解POJO类 serviceName 服务名称,必须填写。 在服务的servlet发布后,服务名称会以path路径方式查找 invoker 服务回调实现,必须实现 com.baidu.jprotobuf.rpc.server.ServerInvoker接口
/**
* RPC service call back method.
*
* @param input request IDL proxy object by protobuf deserialized
* @param output return back IDL proxy object to serialized
* @throws Exception in case of any exception
*/
void invoke(IDLProxyObject input, IDLProxyObject output) throws Exception;
}
@Override
public void invoke(IDLProxyObject input, IDLProxyObject output) throws Exception {
if (input != null) {
System.out.println(input.get("list"));
}
if (output != null) {
output.put("list", "hello world");
}
}
}
simplestring.proto 文件定义:
option java_package = "com.baidu.bjf.remoting.protobuf.simplestring";
option java_outer_classname = "StringTypeClass";
message StringMessage {
required string list = 1;
}
b web.xml文件配置服务发布servlet
<servlet-name>protobufExporter</servlet-name>
<servlet-class>com.baidu.jprotobuf.rpc.server.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>protobufExporter</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
RPC客户端的API开发
RPC客户端使用IDLProxyFactoryBean进行访问,示例代码如下:
public void testProxyFactoryBean() throws Exception {
String idl = "package pkg; " +
"option java_package = \"com.baidu.bjf.remoting.protobuf.simplestring\";" +
"option java_outer_classname = \"StringTypeClass\";" +
"message StringMessage { required string list = 1;} ";
ByteArrayResource resource = new ByteArrayResource(idl.getBytes());
IDLProxyFactoryBean proxyFactoryBean = new IDLProxyFactoryBean();
proxyFactoryBean.setServiceUrl("http://localhost:8080/myfirstproject/remoting/SimpleIDLTest");
proxyFactoryBean.setInputIDL(resource);
proxyFactoryBean.setOutputIDL(resource);
proxyFactoryBean.afterPropertiesSet();
ClientInvoker invoker = proxyFactoryBean.getObject();
//set request param
IDLProxyObject input = invoker.getInput();
input.put("list", "how are you!");
IDLProxyObject output = invoker.invoke(input);
System.out.println(output.get("list"));
}
RPC客户端使用AnnotationProxyFactoryBean进行访问,示例代码如下:
public void testClientProxy() throws Exception {
AnnotationProxyFactoryBean<StringMessagePOJO, StringMessagePOJO> factoryBean;
factoryBean = new AnnotationProxyFactoryBean<StringMessagePOJO, StringMessagePOJO>();
factoryBean.setServiceUrl("http://localhost:8080/myfirstproject/remoting/SimpleIDLTest");
factoryBean.setInputClass(StringMessagePOJO.class);
factoryBean.setOutputClass(StringMessagePOJO.class);
factoryBean.afterPropertiesSet();
ClientInvoker<StringMessagePOJO, StringMessagePOJO> invoker = factoryBean.getObject();
StringMessagePOJO input = invoker.getInput();
if (input != null) {
input.setList("how are you!");
}
StringMessagePOJO output = invoker.invoke(input);
if (output != null) {
System.out.println(output.getList());
}
}
StringMessagePOJO对象代码:
@Protobuf(fieldType = FieldType.STRING, order = 1, required = true)
private String list;
/**
* get the list
* @return the list
*/
public String getList() {
return list;
}
/**
* set list value to list
* @param list the list to set
*/
public void setList(String list) {
this.list = list;
}
}
RPC客户端Spring配置
RPC客户端使用IDLProxyFactoryBean进行访问
<property name="inputIDL" value="classpath:/simplestring.proto"></property>
<property name="outputIDL" value="classpath:/simplestring.proto"></property>
<property name="inputIDLObjectName" value="StringMessage"></property>
<property name="serviceUrl" value="http://localhost:8080/myfirstproject/remoting/SimpleIDLTest"></property>
</bean>
RPC客户端使用AnnotationProxyFactoryBean进行访问
<property name="inputClass" value="com.baidu.bjf.remoting.protobuf.FieldType.StringMessagePOJO"></property>
<property name="outputClass" value="com.baidu.bjf.remoting.protobuf.FieldType.StringMessagePOJO"></property>
<property name="serviceUrl" value="http://localhost:8080/myfirstproject/remoting/SimpleIDLTest"></property>
</bean>
多个IDL message定义解决方案
例如下面定义了多个message定义时,则在服务发布以及客户连接时,需要指定objectName
option java_package = "com.baidu.bjf.remoting.protobuf.simplestring";
option java_outer_classname = "StringTypeClass";
message StringMessage {
required string list = 1;
optional StringMessage2 msg = 2;
}
message StringMessage2 {
required string name = 1;
}
下面示例只演示了客户端的配置,服务发布的配置也是相同
<property name="inputIDL" value="classpath:/simplestring.proto"></property>
<property name="outputIDL" value="classpath:/simplestring.proto"></property>
<property name="inputIDLObjectName" value="StringMessage"></property>
<property name="serviceUrl" value="http://localhost:8080/myfirstproject/remoting/SimpleIDLTest"></property>
</bean>
联系我们
email: rigel-opensource@baidu.com
相关推荐
现可支持直接把Google protobuf的IDL定义语言发布成RPC服务,客户端也可以直接应用IDL定义语言进行动态创建,帮助开发完全省去了手工编译protobuf IDL语言的麻烦。 环境要求:JDK 6 或以上版本 Spring 3.0 ...
Jprotobuf-rpc-socket Build status Protobuf RPC是一种基于TCP协议的二进制高性能RPC通信协议实现。它以Protobuf作为基本的数据交换格式,支持完全基于POJO的发布方式,极大的简化了开发复杂性。 Features: 完全...
protobuf-rpc是一个基于的远程方法调用(RPC))实现,服务器端当前仅支持Java(JDK 1.7及以上),客户端支持Objective-C(cocoa/iOS)和Java(J2SE/Android)。 Why protobuf-rpc? 对于纯的基于Windows或者Linux的服务器来...
A java ProtocolBuffers RPC implementation featuring bidirectional calls.
标签:activemq-protobuf-test-1.0.jar,activemq,protobuf,test,1.0,jar包下载,依赖包
标签:activemq-protobuf-pom-1.0-source-release.zip,activemq,protobuf,pom,1.0,source,release.zip包下载,依赖包
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.1 Created-By: 16.2-b04 (Sun Microsystems Inc.)
标签:activemq-protobuf-1.0.jar,activemq,protobuf,1.0,jar包下载,依赖包
具有protobuf / gPRC IDL定义的RSocket服务基于Protobuf / gRPC接口定义开发RSocket服务,主要特性如下:使用proto文件定义通讯接口,兼容gRPC服务接口定义使用Protobuf作为数据序列化格式,满足高级的要求使用...
标签:activemq-protobuf-test-1.0-javadoc.jar,activemq,protobuf,test,1.0,javadoc,jar包下载,依赖包
标签:activemq-protobuf-test-1.0-sources.jar,activemq,protobuf,test,1.0,sources,jar包下载,依赖包
基于C++ module库 Protobuf Zookeeper 实现的Rpc框架.zip基于C++ module库 Protobuf Zookeeper 实现的Rpc框架.zip基于C++ module库 Protobuf Zookeeper 实现的Rpc框架.zip基于C++ module库 Protobuf Zookeeper 实现...
标签:activemq-protobuf-pom-1.0-source-release.tar.gz,activemq,protobuf,pom,1.0,source,release.tar.gz包下载,依赖包
cocolian-rpc-server: 使用Apache Thrift 作为容器,Protobuf Message作为输入输出参数的RPC服务器;cocolian-rpc-client: 客户端的实现,提供一个连接池。cocolian-rpc-service: 服务器端和客户端共用的一些服务,...
带有Protobuf Services hrpc的简单RPC带有Protobuf Services npm install hrpc的简单RPC用法首先定义一个RPC服务消息Echo {必需的字符串值= 1; }服务示例{rpc Echo(Echo)返回(Echo){}}然后使用hrpc编译器npm ...
PHP调用protobuf通信接口的使用示例,采用rpc通信。
Twirp 一个带有protobuf服务定义的简单RPC框架
标签:activemq-protobuf-1.0-sources.jar,activemq,protobuf,1.0,sources,jar包下载,依赖包
标签:activemq-protobuf-1.0-javadoc.jar,activemq,protobuf,1.0,javadoc,jar包下载,依赖包
安装protobuf各个版本的编译代码,从protobuf-2.4.1到protobuf-3.0.0-Release之间的20个版本,在Mac上protobuf-2.6.1、protobuf-3.0.0-alpha-1、protobuf-3.0.0-beta-2、protobuf-3.0.0测试全部成功!解决了从GitHub...