基于Socket+Zookeeper的簡(jiǎn)單RPC框架

github:https://github.com/shawntime/shawn-native-rpc

需要自己實(shí)現(xiàn)一個(gè)簡(jiǎn)單rpc框架的闸婴,可以參考一下四濒,提供一點(diǎn)點(diǎn)小思路

RPC過(guò)程詳解

image.png
image.png

實(shí)現(xiàn)內(nèi)容

  • json序列化
  • zookeeper實(shí)現(xiàn)服務(wù)端服務(wù)注冊(cè)和客戶端的服務(wù)發(fā)現(xiàn)和負(fù)載均衡
  • 自動(dòng)掃描服務(wù)端所提供的接口,并注冊(cè)到zookeeper中
  • 自動(dòng)生成客戶端動(dòng)態(tài)代理對(duì)象
  • socket實(shí)現(xiàn)通訊

模塊說(shuō)明

  • native-core:公共包:注解把夸、異常定義灵嫌、通訊協(xié)議、序列化氛驮、zookeeper客戶端封裝以及包掃描腕柜,配置加載等工具類
  • native-customer:調(diào)用方需引入的模塊,實(shí)現(xiàn)自動(dòng)生成客戶端動(dòng)態(tài)代理對(duì)象,zk的服務(wù)發(fā)現(xiàn)和負(fù)載均衡盏缤,以及通信實(shí)現(xiàn)
  • native-provider:服務(wù)提供方需引入的模塊砰蠢,實(shí)現(xiàn)了接口客戶端請(qǐng)求并處理,zk的服務(wù)注冊(cè)
  • native-interface:對(duì)外提供服務(wù)的接口定義
  • shawn-test-provider:提供方demo
  • shawn-test-customer:調(diào)用方demo

配置

provider配置

配置naive-rpc.properties文件

scanPackage=com.shawntime.rpc.api
servicePort=11001
  • scanPackage: 包掃描路徑唉铜,自動(dòng)創(chuàng)建包路徑下接口動(dòng)態(tài)代理類
  • servicePort: 服務(wù)端監(jiān)聽(tīng)端口號(hào)

配置spring

<?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:cache="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.1.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.shawntime"/>
    <context:annotation-config/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 配置消息格式化方式 -->
    <bean id="jsonFormatter" class="com.shawntime.rpc.core.serialize.json.JsonFormatter" />
    <bean id="jsonParse" class="com.shawntime.rpc.core.serialize.json.JsonParse" />

    <!-- 配置服務(wù)類台舱,該處使用socket處理請(qǐng)求 -->
    <bean id="rpcProvider" class="com.shawntime.rpc.provider.service.socket.SocketRpcProvider">
        <property name="formatter" ref="jsonFormatter" />
        <property name="parse" ref="jsonParse" />
    </bean>

    <!-- 配置zk服務(wù)器集群信息,作服務(wù)注冊(cè) -->
    <bean id="zookeeperClient" class="com.shawntime.rpc.core.zookeeper.ZookeeperClient">
        <constructor-arg value="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183" />
    </bean>
</beans>
customer配置

配置naive-rpc.properties文件

scanPackage=com.shawntime.rpc.api
  • scanPackage: 包掃描路徑潭流,自動(dòng)創(chuàng)建包路徑下接口動(dòng)態(tài)代理類

配置spring

<?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:cache="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.1.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.shawntime"/>
    <context:annotation-config/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 配置消息格式化方式 -->
    <bean id="jsonFormatter" class="com.shawntime.rpc.core.serialize.json.JsonFormatter" />
    <bean id="jsonParse" class="com.shawntime.rpc.core.serialize.json.JsonParse" />
    
    <!-- 客戶端處理配置 -->
    <bean id="socketInvoke" class="com.shawntime.rpc.customer.invoke.socket.SocketInvoke" />
    
    <bean id="beanNameGenerator" class="org.springframework.context.annotation.AnnotationBeanNameGenerator" />

    <!-- 客戶端動(dòng)態(tài)代理注冊(cè)類 -->
    <bean id="customBeanDefinitionRegistryPostProcessor"
          class="com.shawntime.rpc.customer.config.CustomBeanDefinitionRegistryPostProcessor">
        <property name="invoke" ref="socketInvoke" />
        <property name="formatter" ref="jsonFormatter" />
        <property name="parse" ref="jsonParse" />
        <property name="beanNameGenerator" ref="beanNameGenerator" />
        <property name="serviceFindManager" ref="serviceFindManager" />
    </bean>

    <!-- 服務(wù)發(fā)現(xiàn)和負(fù)載均衡 -->
    <bean id="serviceFindManager" class="com.shawntime.rpc.customer.ServiceFindManager">
        <property name="zookeeperClient" ref="zookeeperClient" />
    </bean>

    <!-- 配置zk服務(wù)器集群信息竞惋,作服務(wù)注冊(cè) -->
    <bean id="zookeeperClient" class="com.shawntime.rpc.core.zookeeper.ZookeeperClient">
        <constructor-arg value="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183" />
    </bean>
</beans>

Demo

  • 接口定義,用RpcService注解
@RpcService
public interface IUserService {

    UserOut getUserInfo(UserIn userIn, Integer level);

    void addUserProvince(ProvinceIn in);
}
  • 提供方
package com.shawntime.test.provider;

import com.shawntime.rpc.api.IUserService;
import com.shawntime.rpc.api.contract.ProvinceIn;
import com.shawntime.rpc.api.contract.UserIn;
import com.shawntime.rpc.api.contract.UserOut;
import org.springframework.stereotype.Component;

@Component
public class UserService implements IUserService {

    public UserOut getUserInfo(UserIn userIn, Integer level) {
        UserOut userOut = new UserOut();
        if (level > 0) {
            userOut.setUserId(1);
            userOut.setAddress("安徽蕪湖");
            userOut.setAge(18);
            userOut.setUserName(userIn.getUserName());
        } else {
            userOut.setUserId(2);
            userOut.setAddress("河北保定");
            userOut.setAge(19);
            userOut.setUserName(userIn.getUserName());
        }
        return userOut;
    }

    public void addUserProvince(ProvinceIn provinceIn) {
        System.out.println(provinceIn);
    }
}

  • 調(diào)用方
@Component
public class LoginService implements ILoginService {

    @Resource
    private IUserService userService;

    public boolean login(String userName, String password, Integer level) {
        UserIn userIn = new UserIn();
        userIn.setUserName(userName);
        userIn.setPassword(password);
        UserOut userInfo = userService.getUserInfo(userIn, level);
        System.out.println("userId:" + userInfo.getUserId()
                + ", age:" + userInfo.getAge()
                + ", address:" + userInfo.getAddress());
        return userInfo != null;
    }

    public int addUserProvince(ProvinceIn in) {
        userService.addUserProvince(in);
        return 1;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末灰嫉,一起剝皮案震驚了整個(gè)濱河市拆宛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌讼撒,老刑警劉巖浑厚,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異根盒,居然都是意外死亡钳幅,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)郑象,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)贡这,“玉大人,你說(shuō)我怎么就攤上這事厂榛「墙茫” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵击奶,是天一觀的道長(zhǎng)辈双。 經(jīng)常有香客問(wèn)我,道長(zhǎng)柜砾,這世上最難降的妖魔是什么湃望? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮痰驱,結(jié)果婚禮上证芭,老公的妹妹穿的比我還像新娘。我一直安慰自己担映,他們只是感情好废士,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著蝇完,像睡著了一般官硝。 火紅的嫁衣襯著肌膚如雪矗蕊。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,482評(píng)論 1 302
  • 那天氢架,我揣著相機(jī)與錄音傻咖,去河邊找鬼。 笑死岖研,一個(gè)胖子當(dāng)著我的面吹牛卿操,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播孙援,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼硬纤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了赃磨?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤洼裤,失蹤者是張志新(化名)和其女友劉穎邻辉,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體腮鞍,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡值骇,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了移国。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吱瘩。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖迹缀,靈堂內(nèi)的尸體忽然破棺而出使碾,到底是詐尸還是另有隱情,我是刑警寧澤祝懂,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布票摇,位于F島的核電站,受9級(jí)特大地震影響砚蓬,放射性物質(zhì)發(fā)生泄漏矢门。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一灰蛙、第九天 我趴在偏房一處隱蔽的房頂上張望祟剔。 院中可真熱鬧,春花似錦摩梧、人聲如沸物延。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)教届。三九已至响鹃,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間案训,已是汗流浹背买置。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留强霎,地道東北人忿项。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像城舞,于是被迫代替她去往敵國(guó)和親轩触。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容