作者: 一字馬胡
轉(zhuǎn)載標(biāo)志 【2017-11-03】
更新日志
日期 | 更新內(nèi)容 | 備注 |
---|---|---|
2017-11-03 | 添加轉(zhuǎn)載標(biāo)志 | 持續(xù)更新 |
項(xiàng)目地址:https://github.com/pandening/Nsrpc
ok-rpc
ok-rpc是一個(gè)輕量級(jí)、學(xué)習(xí)型的rpc框架鹿响,使用了流行的NIO框架Netty作為底層數(shù)據(jù)傳輸介質(zhì),使用Spring容器框架管理bean谋国,使用Zookeeper來做服務(wù)注冊(cè)和服務(wù)發(fā)現(xiàn)。未來還會(huì)添加更多有趣的技術(shù)到這里面來盼忌,所以ok-rpc的將一直不斷的更新赘方,或多或少的添加新的內(nèi)容铛楣,使其逐漸完善,并走向功能豐富明肮、性能卓越的方向菱农。
性能和功能點(diǎn)是目前ok-rpc框架首要考慮的關(guān)鍵點(diǎn),目前的ok-rpc性能并不可觀柿估,資源占用非常大循未,日后需要盡快不斷的優(yōu)化完善。
在設(shè)計(jì)上秫舌,ok-rpc希望一臺(tái)機(jī)器部署一個(gè)ok-rpc的服務(wù)端或者客戶端的妖,而一個(gè)服務(wù)端希望可以使用一個(gè)[ip:pot]對(duì)外提供服務(wù),一個(gè)ok-rpc客戶端則可以訪問任意可達(dá)的服務(wù)足陨,未來會(huì)做權(quán)限控制嫂粟,關(guān)于checklist和todolist將在下文中提到。
ok-rpc的服務(wù)端啟動(dòng)之初墨缘,就會(huì)加載所有注冊(cè)的服務(wù)星虹,服務(wù)想要對(duì)外發(fā)布就需要通過ok-rpc的服務(wù)端進(jìn)行對(duì)外暴露零抬,而ok-rpc使用了Spring強(qiáng)大得bean管理能力接管了暴露服務(wù)的任務(wù),同樣客戶端在啟動(dòng)之初同樣會(huì)使用Spring的強(qiáng)大力量來初始化service的引用宽涌,ok-rpc的服務(wù)端與客戶端之間通過Netty進(jìn)行數(shù)據(jù)傳輸(request和response)平夜,目前僅僅支持基于Tpc的傳輸,而且協(xié)議極為簡(jiǎn)單卸亮,未來將重新設(shè)計(jì)通信協(xié)議忽妒,包括codec協(xié)商、loadBalance等等內(nèi)容嫡良。ok-rpc使用了各種技術(shù)來管理遠(yuǎn)端服務(wù)和客戶端調(diào)用锰扶,使得服務(wù)端的服務(wù)暴露異常簡(jiǎn)單献酗,客戶端的調(diào)用就好像是在本地調(diào)用一樣寝受。
ok-rpc的名字源于okHttp和okIo,對(duì)于ok開頭的框架都是非常厲害的罕偎,介于希望ok-rpc也能逐漸走向牛逼之路很澄,所以取名字為“ok-rpc”,至于為什么中間添加了“-”颜及,也就意味著離OkHttp甩苛、OkIo等框架還要很遠(yuǎn)的路要走。
ok-rpc的使用
使用ok-rpc異常簡(jiǎn)單俏站,下面是一個(gè)簡(jiǎn)單的示例讯蒲。
- 服務(wù)端暴露服務(wù)
你需要編寫一個(gè)接口,比如下面的這個(gè):
package io.hujian.rpc.test.client;
/**
* Created by HuJian on 2017/10/5.
*
*/
public interface EchoService {
String echo(String msg);
}
對(duì)于服務(wù)端來說肄扎,你還需要編寫實(shí)現(xiàn)類:
package io.hujian.rpc.test.server;
import io.hujian.npc.manager.RpcService;
import io.hujian.rpc.test.client.EchoService;
/**
* Created by HuJian on 2017/10/5.
*
* impl
*/
@RpcService(EchoService.class)
public class EchoServiceImpl implements EchoService{
@Override
public String echo(String msg) {
return "Echo:" + msg;
}
}
需要注意的一點(diǎn)是墨林,你需要在你的接口的實(shí)現(xiàn)類上加上@RpcService注解,讓ok-rpc知道這是一個(gè)ok-rpc接口的實(shí)現(xiàn)類犯祠。至于
具體的接口旭等,你需要添加在注解的參數(shù)中。
最后一步衡载,當(dāng)然是使用Spring的xml文件來主持bean了:
<bean id="nodeList" class="io.hujian.npc.pubisher.RpcNodeGroup">
<property name="nodeGroupName" value="test-node-list-group"/>
<property name="nodeList" value="127.0.0.1:18999:1"/>
</bean>
<bean id="echoService" class="io.hujian.npc.pubisher.RpcServicePublishBean">
<property name="nodeGroup" ref="nodeList"/>
<property name="interfaceName" value="EchoService"/>
<property name="url" value="io.hujian.rpc.test.client.EchoService"/>
<property name="version" value="1.0.0"/>
</bean>
不要以為nodeList取名叫List就可以使用多個(gè)地址搔耕,目前來說完全只支持一個(gè)地址,上文已經(jīng)說了ok-rpc的設(shè)計(jì)考慮痰娱。經(jīng)過這幾步弃榨,現(xiàn)在
你可以加載這個(gè)xml文件來啟動(dòng)你的rpc-server了,啟動(dòng)rpc-server也使用了Spring梨睁,下面是一個(gè)示例文件:
<?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:component-scan base-package="io.hujian.*"/>
<!-- load config file -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:zkConfig.properties</value>
</list>
</property>
</bean>
<!-- The Rpc Discover Service -->
<bean id="serviceDiscovery" class="io.hujian.npc.discover.ServiceDiscover"/>
<bean id="rpcClient" class="io.hujian.npc.manager.RpcClient">
<constructor-arg name="discover" ref="serviceDiscovery"/>
</bean>
<bean id="serviceRegistry" class="io.hujian.npc.register.SampleServiceRegister"/>
<bean id="rpcServer" class="io.hujian.npc.manager.RpcServer"/>
</beans>
使用Spring加載上述文件即可啟動(dòng)ok-rpcserver惭墓,比如這個(gè)文件叫ApplicationContext.xml,則可以使用下面的代碼
來啟動(dòng)ok-rpc服務(wù)器:
new ClassPathXmlApplicationContext("ApplicationContext.xml");
- 客戶端注冊(cè)服務(wù)bean
首先,你需要有一份服務(wù)端提供的接口類文件而姐,比如腊凶,你想要訪問上面注冊(cè)的EchoService,那么你就需要有一個(gè)這樣的接口類。然后
你通過注冊(cè)下面的bean來加載引用:
<bean id="echoService" class="io.hujian.npc.consumer.RpcServiceReferenceBean"
init-method="init">
<property name="clazz" value="io.hujian.rpc.test.client.EchoService"/>
</bean>
然后钧萍,你就可以使用這個(gè)bean了:
ApplicationContext context = new ClassPathXmlApplicationContext("client-services.xml");
EchoService echoService = (EchoService) context.getBean("echoService");
System.out.println("Response:" + echoService.echo("hello, ok-rpc!"));
測(cè)試
Request 數(shù)量 | Response數(shù)量 | 花費(fèi)時(shí)間 | qps | 客戶端使用線程數(shù)量 |
---|---|---|---|---|
160000 | 160000 | 22473ms | 7119 | 16 |
320000 | 320000 | 37836ms | 8457 | 16 |
800000 | 800000 | 102226ms | 7825 | 16 |
服務(wù)端運(yùn)行時(shí)資源:
客戶端運(yùn)行時(shí)資源:
測(cè)試機(jī)器:MacBook Pro褐缠, 2.2GHz Intel Core i7 16GB 1600MHz DDR3
License
Copyright 2017 HuJian
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.