分布式服务框架Dubbo入门实例

Dubbo的官网中是这样介绍Dubbo的。DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

本文是对Dubbo的框架的一次学习步骤记录。

所需环境:Centos7 x64,Win7 x64,eclipse,maven,zookeeper,Dubbo

1.zookeeper安装与配置

先到 zookeeper官网下载一个zookeeper安装文件下来,我这里下载3.4.8稳定版。
在Linux系统中运行:
wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz
解压:
#tar zxvf zookeeper-3.4.8.tar.gz
配置:
切换到解压目录,进入conf目录,复制默认的zoo_sample.cfg文件,改名为zoo.cfg
#cp zoo_sample.cfg zoo.cfg
[plain]  view plain  copy
  1. [root@localhost conf]# ll  
  2. 总用量 16  
  3. -rw-rw-r-- 1 jk145 jk145  535 2月   6 11:46 configuration.xsl  
  4. -rw-rw-r-- 1 jk145 jk145 2161 2月   6 11:46 log4j.properties  
  5. -rw-r--r-- 1 root  root  1056 7月   3 21:21 zoo.cfg  
  6. -rw-rw-r-- 1 jk145 jk145  922 2月   6 11:46 zoo_sample.cfg  
  7. [root@localhost conf]#   

之后切换到zookeeper的bin 目录,运行zookeeper就好。
在bin目录下,执行./zkServer.sh start

[plain]  view plain  copy
  1. [root@localhost bin]# pwd  
  2. /usr/local/zookeeper/zookeeper-3.4.8/bin  
  3. [root@localhost bin]# ./zkServer.sh start  
  4. ZooKeeper JMX enabled by default  
  5. Using config: /usr/local/zookeeper/zookeeper-3.4.8/bin/../conf/zoo.cfg  
  6. Starting zookeeper ... STARTED  
  7. [root@localhost bin]#   


2.Dubbo服务端搭建

新建立一个manven工程,可以是jar也可以是war工程。
在pom中加入依赖
[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.haiyang</groupId>  
  5.     <artifactId>dubbo</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>war</packaging>  
  8.     <properties>  
  9.         <spring.version>4.1.3.RELEASE</spring.version>  
  10.         <dubbo.version>2.5.3</dubbo.version>  
  11.         <zookeeper.version>3.4.8</zookeeper.version>  
  12.     </properties>  
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>com.alibaba</groupId>  
  16.             <artifactId>dubbo</artifactId>  
  17.             <version>${dubbo.version}</version>  
  18.             <exclusions>  
  19.                 <exclusion>  
  20.                     <groupId>org.springframework</groupId>  
  21.                     <artifactId>spring</artifactId>  
  22.                 </exclusion>  
  23.             </exclusions>  
  24.         </dependency>  
  25.         <dependency>  
  26.             <groupId>org.apache.zookeeper</groupId>  
  27.             <artifactId>zookeeper</artifactId>  
  28.             <version>${zookeeper.version}</version>  
  29.         </dependency>  
  30.         <!-- Spring -->  
  31.         <dependency>  
  32.             <groupId>org.springframework</groupId>  
  33.             <artifactId>spring-beans</artifactId>  
  34.             <version>${spring.version}</version>  
  35.         </dependency>  
  36.         <dependency>  
  37.             <groupId>org.springframework</groupId>  
  38.             <artifactId>spring-webmvc</artifactId>  
  39.             <version>${spring.version}</version>  
  40.         </dependency>  
  41.         <dependency>  
  42.             <groupId>org.springframework</groupId>  
  43.             <artifactId>spring-aspects</artifactId>  
  44.             <version>${spring.version}</version>  
  45.         </dependency>  
  46.         <dependency>  
  47.             <groupId>com.github.sgroschupf</groupId>  
  48.             <artifactId>zkclient</artifactId>  
  49.             <version>0.1</version>  
  50.         </dependency>  
  51.     </dependencies>  
  52.     <build>  
  53.         <plugins>  
  54.             <!-- java编译插件 -->  
  55.             <plugin>  
  56.                 <groupId>org.apache.maven.plugins</groupId>  
  57.                 <artifactId>maven-compiler-plugin</artifactId>  
  58.                 <version>3.2</version>  
  59.                 <configuration>  
  60.                     <source>1.7</source>  
  61.                     <target>1.7</target>  
  62.                     <encoding>UTF-8</encoding>  
  63.                 </configuration>  
  64.             </plugin>  
  65.         </plugins>  
  66.     </build>  
  67. </project>  

3.Dubbo服务端服务代码

服务接口类:
[java]  view plain  copy
  1. package com.haiyang.service;  
  2.   
  3. public interface HelloService {  
  4.     String sayHello(String str);  
  5. }  

服务实现类:
[java]  view plain  copy
  1. package com.haiyang.service.impl;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. import com.haiyang.service.HelloService;  
  6.   
  7. @Service("helloService")  
  8. public class HelloServiceImpl implements HelloService {  
  9.     public String sayHello(String str) {  
  10.         return "Hello " + str;  
  11.     }  
  12.   
  13. }  

4.Dubbo服务端Spring文件配置

Spring-context.xml配置:
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"  
  3.     xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    
  6.            http://www.springframework.org/schema/aop     
  7.            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd    
  8.            http://www.springframework.org/schema/tx    
  9.            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd    
  10.            http://www.springframework.org/schema/context    
  11.            http://www.springframework.org/schema/context/spring-context-3.2.xsd"  
  12.     default-autowire="byName" default-lazy-init="false">  
  13.   
  14.     <!-- 采用注释的方式配置bean -->  
  15.     <context:annotation-config />  
  16.   
  17.     <!-- 配置要扫描的包 -->  
  18.     <context:component-scan base-package="com.haiyang" />  
  19.   
  20.     <!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->  
  21.     <aop:aspectj-autoproxy proxy-target-class="true" />     
  22.     <import resource="dubbo-provider.xml" />  
  23. </beans>  

dubbo-provider.xml配置:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.             http://www.springframework.org/schema/beans/spring-beans.xsd    
  6.             http://code.alibabatech.com/schema/dubbo    
  7.             http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  8.   
  9.     <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->  
  10.     <dubbo:application name="test_provider" />  
  11.   
  12.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  13.     <dubbo:registry protocol="zookeeper"  
  14.         address="zookeeper://192.168.138.129:2181" check="false" subscribe="false"  
  15.         register=""></dubbo:registry>  
  16.   
  17.     <!-- 用dubbo协议在20880端口暴露服务 -->  
  18.     <dubbo:protocol name="dubbo" port="20880" />  
  19.   
  20.     <!-- 用户服务接口 -->  
  21.     <dubbo:service interface="com.haiyang.service.HelloService"  
  22.         ref="helloService" />  
  23.   
  24. </beans>    

5.Dubbo服务端测试类

[java]  view plain  copy
  1. package com.haiyang;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class DubboProvider {  
  6.   
  7.     public static void main(String[] args) {  
  8.         try {  
  9.             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");  
  10.             context.start();  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         synchronized (DubboProvider.class) {  
  15.             while (true) {  
  16.                 try {  
  17.                     DubboProvider.class.wait();  
  18.                 } catch (InterruptedException e) {  
  19.                     e.printStackTrace();  
  20.                 }  
  21.             }  
  22.         }  
  23.     }  
  24. }  

5.Dubbo客户端搭建

和上面的Dubbo服务端一样,加入相同的jar文件,另外,再加入上一个工程,将它作为一个依赖,同时记得将上面的Dubbo服务工程用maven  install下
pom清单如下:
[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.haiyang</groupId>  
  5.     <artifactId>dubbo-use</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>war</packaging>  
  8.     <properties>  
  9.         <spring.version>4.1.3.RELEASE</spring.version>  
  10.         <dubbo.version>2.5.3</dubbo.version>  
  11.         <zookeeper.version>3.4.8</zookeeper.version>  
  12.     </properties>  
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>com.alibaba</groupId>  
  16.             <artifactId>dubbo</artifactId>  
  17.             <version>${dubbo.version}</version>  
  18.             <exclusions>  
  19.                 <exclusion>  
  20.                     <groupId>org.springframework</groupId>  
  21.                     <artifactId>spring</artifactId>  
  22.                 </exclusion>  
  23.             </exclusions>  
  24.         </dependency>  
  25.         <dependency>  
  26.             <groupId>org.apache.zookeeper</groupId>  
  27.             <artifactId>zookeeper</artifactId>  
  28.             <version>${zookeeper.version}</version>  
  29.         </dependency>  
  30.         <!-- Spring -->  
  31.         <dependency>  
  32.             <groupId>org.springframework</groupId>  
  33.             <artifactId>spring-beans</artifactId>  
  34.             <version>${spring.version}</version>  
  35.         </dependency>  
  36.         <dependency>  
  37.             <groupId>org.springframework</groupId>  
  38.             <artifactId>spring-webmvc</artifactId>  
  39.             <version>${spring.version}</version>  
  40.         </dependency>  
  41.         <dependency>  
  42.             <groupId>org.springframework</groupId>  
  43.             <artifactId>spring-aspects</artifactId>  
  44.             <version>${spring.version}</version>  
  45.         </dependency>  
  46.         <dependency>  
  47.             <groupId>com.github.sgroschupf</groupId>  
  48.             <artifactId>zkclient</artifactId>  
  49.             <version>0.1</version>  
  50.         </dependency>  
  51.         <!-- 服务类 -->  
  52.         <dependency>  
  53.             <groupId>com.haiyang</groupId>  
  54.             <artifactId>dubbo</artifactId>  
  55.             <version>0.0.1-SNAPSHOT</version>  
  56.         </dependency>  
  57.     </dependencies>  
  58.     <build>  
  59.         <plugins>  
  60.             <!-- java编译插件 -->  
  61.             <plugin>  
  62.                 <groupId>org.apache.maven.plugins</groupId>  
  63.                 <artifactId>maven-compiler-plugin</artifactId>  
  64.                 <version>3.2</version>  
  65.                 <configuration>  
  66.                     <source>1.7</source>  
  67.                     <target>1.7</target>  
  68.                     <encoding>UTF-8</encoding>  
  69.                 </configuration>  
  70.             </plugin>  
  71.         </plugins>  
  72.     </build>  
  73. </project>  

6.Dubbo客户端Spring配置

spring-context.xml配置如下:
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"  
  3.     xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    
  6.            http://www.springframework.org/schema/aop     
  7.            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd    
  8.            http://www.springframework.org/schema/tx    
  9.            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd    
  10.            http://www.springframework.org/schema/context    
  11.            http://www.springframework.org/schema/context/spring-context-3.2.xsd"  
  12.     default-autowire="byName" default-lazy-init="false">  
  13.   
  14.     <!-- 采用注释的方式配置bean -->  
  15.     <context:annotation-config />  
  16.   
  17.     <!-- 配置要扫描的包 -->  
  18.     <context:component-scan base-package="com.haiyang" />  
  19.       
  20.     <!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->  
  21.     <aop:aspectj-autoproxy proxy-target-class="true" />     
  22.       
  23.     <import resource="dubbo-consumer.xml" />  
  24.   
  25. </beans>  

dubbo-consumer.xml配置如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd    
  6.         http://code.alibabatech.com/schema/dubbo    
  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  8.   
  9.     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
  10.     <dubbo:application name="dubbo-use" />  
  11.   
  12.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  13.     <!-- 注册中心地址 -->  
  14.     <dubbo:registry protocol="zookeeper" address="192.168.138.129:2181" />  
  15.   
  16.     <!-- 用户服务接口,和服务类名对应 -->  
  17.     <dubbo:reference interface="com.haiyang.service.HelloService"  
  18.         id="helloService" />  
  19.   
  20.   
  21. </beans>    

7.Dubbo客户端测试类

[java]  view plain  copy
  1. package com.haiyang.test;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. import com.haiyang.service.HelloService;  
  6.   
  7. public class HaiTest {  
  8.   
  9.     public static void main(String[] args) {  
  10.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  11.                 new String[] { "spring/dubbo-consumer.xml" });  
  12.         context.start();  
  13.         HelloService demoService = (HelloService) context.getBean("helloService");  
  14.         System.out.println(demoService.sayHello("world"));  
  15.         synchronized (DubboTest.class) {  
  16.             while (true) {  
  17.                 try {  
  18.                     DubboTest.class.wait();  
  19.                 } catch (InterruptedException e) {  
  20.                     e.printStackTrace();  
  21.                 }  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26. }  

8.Dubbo整体测试

为了观察方便,先在zookeeper的服务器上查看日志文件,对应目录为:
{path}/zookeeper/bin/zookeeper.out
用tail -f命令监听此日志文件
[ping@javaEE bin]$ tail -f zookeeper.out 
同时,为了观察方便,区别下,将之前服务端HelloServiceImpl这个类的return "Hello:" + str;改为return "Hello Dubbo:" + str;

运行Dubbo服务端测试类(DubboProvider.java)



运行Dubbo客户端测试类(HaiTest.java)

从这里可以看出,成功调用了服务端的方法
同时,zookeeper监听的日志文件也记录到了日志信息。


9.Dubbo管理控制台安装

同时,为了Dubbo也自带了管理控制台,在这里,也顺便安装下。
在百度上搜一下dubbo-admin-2.5.4.war ,将它下下来,再传到Linux中去.
在crt中输入rz,选择dubbo-admin-2.5.4.war文件,上传,并将它解压到linux的tomcat的webapps目录下。
运行unzip -d解压,将它解压,并将名称改为ROOT
unzip dubbo-admin-2.5.4.war -d ROOT
再进入解压后的ROOT目录下,修改配置文件

vim ROOT/WEB-INF/dubbo.properties

dubbo.registry.address=zookeeper://192.168.138.129

dubbo.admin.root.password=root

dubbo.admin.guest.password=guest

保存:x
最后再启动此Tomcat就可以了!
最后在浏览器里连接下


输入刚刚配置的密码和用户名:root    root
登录 

能成功进入了,并且也能检测到目前连上的服务。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值