admin 发表于 2016-2-23 15:16

SpringMVC整合Dubbo与Zookeeper

第一步:在Linux上安装Zookeeper
       Zookeeper作为Dubbo服务的注册中心,Dubbo原先基于数据库的注册中心,没采用Zookeeper,Zookeeper一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心,Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求。我们先在linux上安装Zookeepe。
阿里云linux下安装zookeeper

第二步:配置dubbo-admin的管理页面,方便我们管理页面
淘宝SOA框架dubbo管理控制台安装和使用(一)

第三步:SpringMVC与Dubbo的整合
一:我们先开发服务注册的,就是提供服务,项目结构如图所示:

所需JAR包



applicationContext.xml配置
<?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="com.unj.dubbotest.provider.impl.DemoServiceImpl" />

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

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

      <!-- 使用zookeeper注册中心暴露服务地址 -->
      <dubbo:registry address="zookeeper://192.16.1.180:2181" check="false" subscribe="false" register="" />

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

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

</beans>
说明:
   dubbo:registry 标签一些属性的说明:
      1)register是否向此注册中心注册服务,如果设为false,将只订阅,不注册。
      2)check注册中心不存在时,是否报错。
      3)subscribe是否向此注册中心订阅服务,如果设为false,将只注册,不订阅。
      4)timeout注册中心请求超时时间(毫秒)。
      5)address可以Zookeeper集群配置,地址可以多个以逗号隔开等。
dubbo:service标签的一些属性说明:
   1)interface服务接口的路径
   2)ref引用对应的实现类的Bean的ID
   3)registry向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A
   4)register 默认true ,该协议的服务是否注册到注册中心。


第二:我们在开发服务消费者,就是调用服务

我们只需将提供者的接口打包发送给消费者即可。
applicationContext.xml配置:
<?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="hehe_consumer" />
      <!-- 使用zookeeper注册中心暴露服务地址 -->
      <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
      <dubbo:registry address="zookeeper://192.168.1.180:2181" />
      <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
      <dubbo:reference id="demoService"
                interface="com.unj.dubbotest.provider.DemoService" />
</beans>
Consumer类:
package com.alibaba.dubbo.demo.pp;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.unj.dubbotest.provider.DemoService;

public class Consumer {

      public static void main(String[] args) throws Exception {
                ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                              new String[] { "applicationContext.xml" });
                context.start();
                DemoService demoService = (DemoService) context.getBean("demoService");
                demoService.sayHello("哈哈哈");
                System.in.read();
      }

}
分别启动后:

需要项目源码的可以留下 邮箱~




我很无奈 发表于 2016-3-14 14:08

发源码liujianshe8899@163.com

admin 发表于 2016-3-14 14:17

我很无奈 发表于 2016-3-14 06:08
发源码

哈哈哈 没有~~~~~~~~~~
页: [1]
查看完整版本: SpringMVC整合Dubbo与Zookeeper