一个基于Netty的RPC框架


利用netty写了一个简单的RPC框架,纯粹是为了理解RPC框架。

使用protostuff 1.07 序列化,以netty 3.2.1 网络框架。

下载地址:https://github.com/stefzhlg/snrpc


##How to use

e.g.

1,server class;   interface and implementor        // define an interface:        

  

  public interface SnRpcInterface {
      
     public String getMessage(String param);
    
   }
    // implement interface
    public class SnRpcImpl implements SnRpcInterface {
        public String getMessage(String param) {
            return "hi,it is message from server...param+" + param;
        }
    }

2, start server

    SnRpcInterface inter = new SnRpcImpl();
    SnRpcServer server = new SnNettyRpcServer(new Object[] { inter });
    try {
        server.start();
    } catch (Throwable e) {
        e.printStackTrace();
    }

3,config file

snrpcserver.properties

#tcpNoDelay 
snrpc.tcp.nodelay=true
#call the bind method as many times as you want
snrpc.tcp.reuseAddress=true
#ISDEBUG
snrpc.dev=true
#TCP timeout
snrpc.read.timeout=25000
#server port
snrpc.http.port=8080

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<application>
 <!-- rpc interface services -->    
 <rpcServices>
        <rpcService name="SnRpcInterface" interface="org.stefan.snrpc.server.SnRpcInterface" overload="true">
           <rpcImplementor  class="org.stefan.snrpc.server.SnRpcImpl"/> 
        </rpcService>
    </rpcServices>
</application>

4, client invoker

    SnRpcConnectionFactory factory = new SnNettyRpcConnectionFactory(
                "localhost", 8080);
    factory = new PoolableRpcConnectionFactory(factory);
    SnRpcClient client = new CommonSnRpcClient(factory);
    try {
        SnRpcInterface clazz = client.proxy(SnRpcInterface.class);
        String message = clazz.getMessage("come on");
        System.out.println("client receive message .... : " + message);
    } catch (Throwable e) {
        e.printStackTrace();
    }

Pre-requirement

  • JDK6+

  • Maven 2

Dependency

  • reflectasm-1.07.jar

  • asm-4.0.jar

  • log4j-1.2.16.jar

  • dom4j-1.6.1.jar

  • xml-apis-1.0.b2.jar

  • slf4j-api-1.6.6.jar

  • netty-3.2.1.Final.jar

  • jaxen-1.1.6.jar

  • protostuff-core-1.0.7.jar

  • protostuff-api-1.0.7.jar

  • protostuff-runtime-1.0.7.jar

  • protostuff-collectionschema-1.0.7.jar

  • commons-pool-1.6.jar


转载于:https://my.oschina.net/stefanzhlg/blog/352799

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值