您的位置:首页 > 理论基础 > 计算机网络

jprotobuf-rpc-http 超简单的Google Protobuf RPC应用实现

2020-01-11 20:35 537 查看

jprotobuf-rpc-http 是应用jprotobuf类库实现基于http协议的RPC开发组件。

目前1.0提供可以直接把Google protobuf的IDL定义语言发布成RPC服务,客户端也可以直接应用IDL定义语言进行动态创建,帮助开发完全省去了手工编译protobuf IDL语言的麻烦。

应用过protobuf的同学有知道,使用protobuf需要先定义protobuf IDL 脚本,由protoc工具编译生成指定语言的代码。
jprotobuf-rpc-http 针对java开发同学来讲,完全可以不需要使用protoc工具编译,就可以直接把protobuf IDL 脚本发布成RPC的服务,相当简单易用。

jprotobuf-rpc-http 基于Spring3 JDK6基础上进行开发,下面演示的发布RPC服务的示例代码:

 

  • 在Spring配置文件,定义IDLServiceExporter服务发布配置 (直接由IDL定义发布)
<bean class="com.baidu.jprotobuf.rpc.server.IDLServiceExporter">
        <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接口

上面示例相关的代码如下:

public interface 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;
}

 

public class SimpleIDLInvoker implements ServerInvoker {

    @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 文件定义:

package pkg;  

option java_package = "com.baidu.bjf.remoting.protobuf.simplestring";

option java_outer_classname = "StringTypeClass";  

message StringMessage {  
  required string list = 1;

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>

 

 

 

  • 在Spring配置文件,定义IDLServiceExporter服务发布配置 (直接由IDL定义发布)

RPC客户端使用IDLProxyFactoryBean进行访问,示例代码如下:

 

@Test
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"));

}

 


相关资料链接:

jprotobuf-rpc-http文档: https://github.com/jhunters/JProtobuf-rpc-http

jprotobuf文档:https://github.com/jhunters/jprotobuf

 

  • 点赞
  • 收藏
  • 分享
  • 文章举报
Rigel_Team 发布了3 篇原创文章 · 获赞 0 · 访问量 293 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: