spring dubbo整合详细

来源:互联网 发布:手机网络配置文件 编辑:程序博客网 时间:2024/04/29 23:14

资料下载:

http://download.csdn.net/detail/xiao__miao/9791753

DubboZookeeperSpringMVC整合使用

 第一步:在Linux上安装Zookeeper

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

    1)下载Zookeeper-3.4.6.tar.gz  地址http://www.apache.org/dist/zookeeper/

    2) 我们放到Linux下的一个文件夹,然后解压: 

      #tar zxvf zookeeper-3.4.6.tar.gz

  (3)然后在对应的zookeeper-3.4.6/conf 下有一个文件zoo_sample.cfg的这个文件里面配置了监听客户端连接的端口等一些信息,Zookeeper 在启动时会找zoo.cfg这个文件作为默认配置文件,所以我们复制一个名称为zoo.cfg的文件,如图所示:


配置如下:


(4)现在进入自己配置的dataDir目录,我这里说/tmp/zookeeper/data/

在此目录下创建一个myid,写下自己的主机的zk唯一标识------就是server.  后面这个

我这里说server.1,所以我的标识就是1

vi myid 然后写下1,保存。每台虚拟机都做对应的事情。

还有小伙伴们别忘记把防火墙需要打开的端口打开!!!!

再此,我是配置了三台虚拟机,如果是单机的同学可以自己网上寻找下配置zk的


 (5)启动Zookeeper 的服务,如图所示:


我这里是已经启动了服务,所以他这里会提示我已经有服务在了,端口号是2702,想看看服务可以输入命令

ps -ef|grep 2702ps -aux|grep 2702

 到这边Zookeeper的安装和配置完成.

 第二步:配置dubbo-admin的管理页面,方便我们管理页面

(1)下载dubbo-admin-2.5.4.warjdk要求是1.7,在Linuxtomcat部署,先把dubbo-admin-2.5.4放在tomcatwebapps/ROOT下,然后进行解压:

        #unzip -d dubbo-admin-2.4.1.war /home/wangmiao/tomcat-7.0.75/webapps/ROOT/

    (2)然后到webapps/ROOT/WEB-INF下,有一个dubbo.properties文件,里面指向Zookeeper ,使用的是Zookeeper 的注册中心,如图所示:

        

 

   (3)然后启动tomcat服务,打开地址页面会让你输入用户名和密码,用户名和密码都是root,并访问服务,显示登陆页面,说明dubbo-admin部署成功,如图所示:

 

第三步:SpringMVCDubbo的整合,这边使用的Maven的管理项目

 第一:我们先开发服务注册的,就是提供服务,项目结构如图所示:


因为这个就是一个工具包,所以不需要web服务,直接next。具体步骤就不详说了。

生成的pom及项目架构如图:

 

注意,因为待会要使用mvn install把这个生成的工具项目放入本地仓库,所以加个packaging指定为jar。为了其他项目能用到此包

1commonDbo项目加入了一个服务接口

public interface DemoService {   public String hello(String name);}


2现在要建立一个提供服务的maven web项目exportDbopom.xml加入DubboZookeeperjar包、引用commonDbojar包,代码如下:

 <dependency>      <groupId>com.wm.common</groupId>      <artifactId>commonDbo</artifactId>      <version>1.0-SNAPSHOT</version>    </dependency>    <dependency>      <groupId>org.javassist</groupId>      <artifactId>javassist</artifactId>      <version>3.18.2-GA</version>    </dependency>    <dependency>      <groupId>com.alibaba</groupId>      <artifactId>dubbo</artifactId>      <version>2.5.3</version>      <exclusions>        <exclusion>          <artifactId>spring</artifactId>          <groupId>org.springframework</groupId>        </exclusion>        <exclusion>          <artifactId>javassist</artifactId>          <groupId>org.javassist</groupId>        </exclusion>        <exclusion>          <artifactId>netty</artifactId>          <groupId>io.netty</groupId>        </exclusion>      </exclusions>    </dependency>    <dependency>      <groupId>org.apache.zookeeper</groupId>      <artifactId>zookeeper</artifactId>      <version>3.4.6</version>    </dependency>    <dependency>      <groupId>com.github.sgroschupf</groupId>      <artifactId>zkclient</artifactId>      <version>0.1</version>      <exclusions>        <exclusion>          <artifactId>netty</artifactId>          <groupId>io.netty</groupId>        </exclusion>      </exclusions>    </dependency>


(3)exportDbo实现具体的服务,代码如下:

@Service("demoService")public class DemoServiceImpl implements DemoService {    public String sayHello(String s) {        String str = "youxiude ni"+s;        return str;    }}

(4)我们服务以及实现好了,这时要暴露服务,代码如下:

<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="hello-world-app"  />    <!-- 使用multicast广播注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://10.211.55.6:2181" check="false" />    <!-- 声明需要暴露的服务接口 -->    <dubbo:service interface="com.wm.common.service.DemoService" ref="demoService" />    <!-- 和本地bean一样实现服务 -->    <bean id="demoService" class="com.wm.service.impl.DemoServiceImpl" /></beans>

说明:

   dubbo:registry 标签一些属性的说明:

      1register是否向此注册中心注册服务,如果设为false,将只订阅,不注册

      2check注册中心不存在时,是否报错。

      3subscribe是否向此注册中心订阅服务,如果设为false,将只注册,不订阅

      4timeout注册中心请求超时时间(毫秒)

      5address可以Zookeeper集群配置,地址可以多个以逗号隔开等。

  dubbo:service标签的一些属性说明:

     1interface服务接口的路径

     2ref引用对应的实现类的BeanID

     3registry向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A

     4register 默认true ,该协议的服务是否注册到注册中心。


 (5)启动项目,然后我们在Dubbo管理页面上显示,已经暴露的服务,但显示还没有消费者,因为我们还没实现消费者服务,如图所示:


第二:我们在开发服务消费者,就是调用服务,我们在新建一个新的消费者项目结构如图所示:


  (1)importDbo的pom.xml引入Dubbo和Zookeeper的jar包、commonDbojar包,因为引入commonDbojar包,我们在项目中调用像在本地调用一样。代码如下:

<dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>    <dependency>      <groupId>com.wm.common</groupId>      <artifactId>commonDbo</artifactId>      <version>1.0-SNAPSHOT</version>    </dependency>    <dependency>      <groupId>org.javassist</groupId>      <artifactId>javassist</artifactId>      <version>3.18.2-GA</version>    </dependency>    <dependency>      <groupId>com.alibaba</groupId>      <artifactId>dubbo</artifactId>      <version>2.5.3</version>      <exclusions>        <exclusion>          <artifactId>spring</artifactId>          <groupId>org.springframework</groupId>        </exclusion>        <exclusion>          <artifactId>javassist</artifactId>          <groupId>org.javassist</groupId>        </exclusion>        <exclusion>          <artifactId>netty</artifactId>          <groupId>io.netty</groupId>        </exclusion>      </exclusions>    </dependency>    <dependency>      <groupId>org.apache.zookeeper</groupId>      <artifactId>zookeeper</artifactId>      <version>3.4.6</version>    </dependency>    <dependency>      <groupId>com.github.sgroschupf</groupId>      <artifactId>zkclient</artifactId>      <version>0.1</version>      <exclusions>        <exclusion>          <artifactId>netty</artifactId>          <groupId>io.netty</groupId>        </exclusion>      </exclusions>    </dependency>


(2)importDbo项目的具体实现,代码如下: 

@Controllerpublic class IndexController {    @Autowired    private DemoService demoService;    @RequestMapping("/hello")    public String index(Model model){        String name = demoService.sayHello("zz");        System.out.println("name>>"+name);        return "/index";    }}


 (3)我们要引用的地址,代码如下:

<?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="consumer-of-helloworld-app"  />    <!-- 使用multicast广播注册中心暴露发现服务地址 -->    <dubbo:registry address="zookeeper://10.211.55.6:2181" check="false" />    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->    <dubbo:reference id="demoService" interface="com.wm.common.service.DemoService" /></beans>


说明:

   dubbo:reference 的一些属性的说明:

      1interface调用的服务接口

      2check 启动时检查提供者是否存在,true报错,false忽略

      3registry 从指定注册中心注册获取服务列表,在多个注册中心时使用,值为<dubbo:registry>id属性,多个注册中心ID用逗号分隔

      4loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用

   

(4)项目启动,Dubbo管理页面,能看到消费者,如图所示:



(5)然后访问消费者项目,Controller层能像调用本地一样调用服务的具体实现,如图所示:








总结:dubbo还有更多的功能,作者根据网上的案例,再结合自己的实践,记录下这些操作步骤,希望对之后的人学习提供一些帮助。

这是这篇博文我所上传的项目和相关的一些工具都放在里面了,有需要的可以去下载一下

http://download.csdn.net/detail/xiao__miao/9791753

http://download.csdn.net/detail/xiao__miao/9791753
0 0
原创粉丝点击