spring整合zookeeper与dubbo的入门案例(一)

首先简单理解一下dubbo与zookeeper在项目框架中的角色,关于dubbo与zookeeper的理论介绍有很多,大家可以百度一下。

dubbo从角色的角度可以简单的理解为两种角色,一种消费服务方(provider),一种消费方(consumer)。

zookeeper在框架中的作用就是服务注册中心,将provider的服务暴露给消费方。以这种方式来实现分布式服务的目的,provider可以是多个,但是对于consumer来说,只知道一个服务提供的地址,那就是zookeeper。(以上纯属个人理解,欢迎大神指正)

本文实现简单的案例,来研究这个过程,后续我们将在这个例子上进行丰富。


进行实际开发前,我们需要先进行环境的搭建,可以参考我的博客

zookeeper搭建

dubbo-admin搭建

开发工具我使用的是idea,首先创建project:rpc-demo,然后创建三个module:api/provider/consumer

 api:接口定义工程,供provider与consumer共同引用。

provider:接口实现工程,真正的业务处理工程。

consumer:消费方,调用provider实现的接口。


api中我们定义一个接口:

package cn.com.demo.service;

import java.util.List;

/**
 * @Author : zw
 * @Date : 16:17 2017/5/12
 * @Remark :
 **/
public interface DemoService {

    String sayHello(String name);

    public List getUsers();
}

在provider中实现这个接口:

package cn.com.demo.service.impl;

import cn.com.demo.entity.User;
import cn.com.demo.service.DemoService;


import java.util.ArrayList;
import java.util.List;


/**
 * @Author : zw
 * @Date : 14:16 2017/5/15
 * @Remark :
 **/


public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        return "Hello " + name;
    }

    public List getUsers() {
        List list = new ArrayList();
        User u1 = new User();
        u1.setName("hejingyuan");

        u1.setAge(20);
        u1.setSex("f");

        User u2 = new User();
        u2.setName("xvshu");
        u2.setAge(21);
        u2.setSex("m");


        list.add(u1);
        list.add(u2);

        return list;
    }
}

并在provider中进行配置,向zookeeper服务注册中心暴露接口:

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- 具体的实现bean -->
    <bean id="demoService" class="cn.com.demo.service.impl.DemoServiceImpl" />

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="xs_provider" />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->

    <!-- 使用zookeeper注册中心暴露服务地址 即zookeeper的所在服务器ip地址和端口号 -->
    <dubbo:registry address="zookeeper://192.168.1.145:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="cn.com.demo.service.DemoService"
                   ref="demoService" />

</beans>

下面提供一个provider的启动案例,大家也可以将applicationContext.xml配置到web.xml中,然后把war包扔到Tomcat中启动。

package cn.com.demo.startup;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author : zw
 * @Date : 15:15 2017/5/15
 * @Remark :
 **/
public class Provider {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        context.start();
        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
    }
}

在consumer中我们可以调用zookeeper暴露出来的接口服务:

首先我们先获取到刚才provider提供的接口


<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="hjy_consumer" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <dubbo:registry address="zookeeper://192.168.1.145:2181" />

    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
    <dubbo:reference id="demoService"
                     interface="cn.com.demo.service.DemoService" />

</beans>

后续我们就可以像使用spring普通的bean一样使用demoService


package cn.com.demo;

import cn.com.demo.service.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @Author : zw
 * @Date : 15:19 2017/5/15
 * @Remark :
 **/
public class startup {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        context.start();

        DemoService demoService = (DemoService) context.getBean("demoService");
        String hello = demoService.sayHello("zw");
        System.out.println(hello);

        List list = demoService.getUsers();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
        }
        System.in.read();
    }
} 

我们运行provider与consumer可以,然后登陆dubbo管理中心就可以看到我们的接口服务状况





下面附上我的源码链接:

源码




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值