Java打造RPC框架(四):支持zookeeper与负载均衡

Java打造RPC框架系列第四篇

上一篇文章中 给大家讲了zookeeper作为注册中心的基本原理

http://blog.csdn.net/we_phone/article/details/78993394

这篇文章中 我讲的是RPC框架接入对单点zookeeper的支持的源码实现

详细代码可见:Github MeiZhuoRPC

看懂这篇文章需要的前提

  1. 看了前面的系列文章
  2. 熟悉java.util.concurrent包

文章出于详细讲解的目的,篇幅较长 望耐心阅读 有疑问欢迎评论

首先看使用效果

我们启动两个提供者服务

@Test
    public void multi1and2() throws InterruptedException, IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "file:src/test/java/rpcTest/MultiServer1and2Context.xml" });
        context.start();
        //启动spring后才可启动 防止容器尚未加载完毕
        RPC.start();
    }

    @Test
    public void multi2() throws InterruptedException, IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "file:src/test/java/rpcTest/MultiServer2Context.xml" });
        context.start();
        //启动spring后才可启动 防止容器尚未加载完毕
        RPC.start();
    }

下面是他们对应的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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="org.meizhuo.rpc.server.ServerConfig">
        <property name="port" value="9012"></property>
        <property name="zooKeeperHost" value="127.0.0.1:2181"></property>
        <property name="serverImplMap">
            <map>
                <!--配置对应的抽象接口及其实现-->
                <entry key="rpcTest.Service1" value="rpcTest.Service1Impl"></entry>
                <entry key="rpcTest.Service2" value="rpcTest.Service2Impl"></entry>
            </map>
        </property>
    </bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="org.meizhuo.rpc.server.ServerConfig">
        <property name="port" value="9002"></property>
        <property name="zooKeeperHost" value="127.0.0.1:2181"></property>
        <property name="serverImplMap">
            <map>
                <!--配置对应的抽象接口及其实现-->
                <entry key="rpcTest.Service2" value="rpcTest.Service2Impl"></entry>
            </map>
        </property>
    </bean>
</beans>
一个提供者只注册了service1 一个提供者service1 2都注册了

接下来调用消费者

ExecutorService executorService= Executors.newFixedThreadPool(8);
        for (int i = 0; i <1000 ; i++) {
            int finalI = i+1;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    Service1 service1= (Service1) RPC.call(Service1.class);
                    Service2 service2= (Service2) RPC.call(Service2.class);
                    System.out.println("第"+ finalI +"次发出请求");
                    service1.count();
                    service2.count();
                }
            });
        }

<beans xmlns="http://ww
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值