HRPC —— 轻量级高性能 RPC 框架
TonHoppe
8年前
<h2><strong>HRPC</strong></h2> <p>HRPC是一款基于Netty和Zookeeper设计的轻量级高性能RPC框架。</p> <h2><strong>特性</strong></h2> <ul> <li> <p>采用Protostuff序列化;</p> </li> <li> <p>高性能,负载均衡;</p> </li> <li> <p>支持服务的注册和订阅;</p> </li> <li> <p>支持同步及异步2种调用方式;</p> </li> <li> <p>长连接,自动重连;</p> </li> <li> <p>采用cglib动态代理;</p> </li> <li> <p>代码简答,易上手;</p> </li> <li> <p>支持Spring;</p> </li> </ul> <h2><strong>HRPC结构图</strong></h2> <p style="text-align: center;"><img src="https://simg.open-open.com/show/d9b2132aec02568211428aa3f4815aaf.png"></p> <h2><strong>服务注册中心</strong></h2> <p><img src="https://simg.open-open.com/show/db91be5177f8af6eb8bbb6cdda0e325c.png"></p> <h2><strong>服务端</strong></h2> <p><strong>1. 通过Spring配置</strong></p> <pre> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描需求发布的服务所在的包--> <context:component-scan base-package="com.yingjun.rpc.service.impl"/> <context:property-placeholder location="classpath:system.properties"/> <!--服务端配置--> <bean id="rpcServer" class="com.yingjun.rpc.server.RPCServer"> <constructor-arg name="zookeeper" value="${zookeeper.address}"/> <constructor-arg name="serverAddress" value="${server.address}"/> </bean> </beans></pre> <p><strong>2. 服务接口</strong></p> <pre> public interface UserService { public User getUser(String phone); public User updateUser(User user); }</pre> <p><strong>3. 注册服务实现</strong></p> <pre> @HRPCService(UserService.class) public class UserServiceImpl implements UserService { @Override public User getUser(String phone) { User user =new User(111,"yingjun",phone); return user; } @Override public User updateUser(User user) { user.setName("yingjun@update"); return user; } }</pre> <h2><strong>客户端</strong></h2> <p><strong>1. Spring配置</strong></p> <pre> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:property-placeholder location="classpath:system.properties"/> <!--客户端配置--> <bean id="rpcClient" class="com.yingjun.rpc.client.RPCClient"> <constructor-arg name="zookeeper" value="${zookeeper.address}"/> <!--订阅需要用到的接口--> <constructor-arg name="interfaces"> <list> <value>com.yingjun.rpc.service.OrderService</value> <value>com.yingjun.rpc.service.UserService</value> <value>com.yingjun.rpc.service.GoodsService</value> </list> </constructor-arg> </bean> </beans></pre> <p><strong>2. 同步调用</strong></p> <pre> UserService userService = rpcClient.createProxy(UserService.class); User user1 = userService.getUser("188888888"); logger.info("result:" + user1.toString());</pre> <p><strong>3. 异步调用</strong></p> <pre> AsyncRPCProxy asyncProxy = rpcClient.createAsyncProxy(UserService.class); asyncProxy.call("getUser", new AsyncRPCCallback() { @Override public void success(Object result) { logger.info("result:" + result.toString()); } @Override public void fail(Exception e) { logger.error("result:" + e.getMessage()); } }, "188888888");</pre> <h3><strong>为什么选择Protostuff ?</strong></h3> <p><img src="https://simg.open-open.com/show/9a37024744d85565586d6d900009853b.jpg"></p> <p> </p> <p>来自:http://www.oschina.net/p/HRPC?fromerr=c1cdVMWN</p> <p> </p>