Dubbo与Zookeeper、SpringMVC整合和使用(个人入门demo)

 

开发工具html

MyEclipse 10.7git

JDKgithub

1.7web

容器spring

Tomcat 8(运行dubbo)apache

zookeeper版本windows

zookeeper-3.4.6tomcat

dubboapp

dubbo-admin-2.5.3webapp

dubbo-admin-2.5.3下载地址:http://pan.baidu.com/s/1bozCMzP

zookeeper下载地址:http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.6/

Dubbo+Zookeeper 的下载安装配置启动

1.dubbo-admin-2.5.3下载完成后。放在webapps文件夹下面。先把默认的tomcat的ROOT项目备份后移除。将dubbo-admin-2.5.3.war改为ROOT.war 备用

2.zookeeper下载后解压安装便可。windows安装完成后以下图

 

进入到conf里面,会看到zoo_sample.cfg文件。复制一个修改成zoo.cfg,修改相关配置内容

# The number of milliseconds of each tick

tickTime=2000

# The number of ticks that the initial

# synchronization phase can take

initLimit=10

# The number of ticks that can pass between

# sending a request and getting an acknowledgement

syncLimit=5

# the directory where the snapshot is stored.

# do not use /tmp for storage, /tmp here is just

# example sakes.

dataDir=/tmp/zookeeper

# the port at which the clients will connect

#这块是设置zookeeper的端口。默认2181

clientPort=2181

# the maximum number of client connections.

# increase this if you need to handle more clients

#maxClientCnxns=60

#

# Be sure to read the maintenance section of the

# administrator guide before turning on autopurge.

#

# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance

#

# The number of snapshots to retain in dataDir

#autopurge.snapRetainCount=3

# Purge task interval in hours

# Set to "0" to disable auto purge feature

#autopurge.purgeInterval=1

到bin文件夹下面。启动zookeeper,windows 点击zkServer.cmd便可

 

 

 

3.能够启动tomcat了。这样直接访问 就能看到dubbo的页面。要否则dubbo启动会报错。第一次入门的话建议就用默认的端口配置便可。默认用户 root 密码 root

 

 

 

 

 

 

 

 

 

登陆成功之后看到以下页面

 

以上就是dubbo+zookeeper的配置和启动。下面开始Java代码的编写

  • 整合Dubbo+Zookeeper+SpringMVC

1.建立myDubbo项目为主项目,结构图以下 myC P S后续才建立,刚开始是没有的。

 

POM.XML配置以下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com</groupId>

  <artifactId>mydubbo</artifactId>

  注意这里。打包为POM,分布式系统的第一步

  <packaging>pom</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>mydubbo</name>

  <url>http://maven.apache.org</url>

   <properties> 

        <spring.version>3.2.4.RELEASE</spring.version> 

    </properties>

  <dependencies>

            <dependency> 

            <groupId>com.alibaba</groupId> 

            <artifactId>dubbo</artifactId> 

            <version>2.5.3</version> 

            <exclusions> 

                <exclusion> 

                    <groupId>org.springframework</groupId> 

                    <artifactId>spring</artifactId> 

                </exclusion> 

            </exclusions> 

        </dependency> 

        <!--dubbo注册中心--> 

        <dependency> 

            <groupId>org.apache.zookeeper</groupId> 

            <artifactId>zookeeper</artifactId> 

            <version>3.4.6</version> 

        </dependency> 

        <!--zookeeper客户端--> 

        <dependency> 

            <groupId>com.github.sgroschupf</groupId> 

            <artifactId>zkclient</artifactId> 

            <version>0.1</version> 

        </dependency> 

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-core</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-context</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

  </dependencies>

  <build>

    <finalName>mydubbo</finalName>

  </build>

  建立子项目后自动会加载。刚开始是没有的

  <modules>

         <module>myService</module>

         <module>myProvider</module>

         <module>myConsumer</module>

  </modules>

</project>

2.建立服务提供商子项目,MyEclipse操做以下图示意 对mydubbo项目鼠标右键new或者maven插件直接建立maven module项目,填写子项目名称。有的若是不选种Create a simple project 自定义建立会报错。选择则不会。根据实际状况来肯定是否选择

 


3.建立3个子项目分别为 myService(服务商(接口),模块提供方) myProvider(供应者,给dubbo zookeeper注册暴露接口) myConsumer

  • myService 项目相关配置
  1. pom配置
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.   <parent>
  5.     <artifactId>mydubbo</artifactId>
  6.     <groupId>com</groupId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.   </parent>
  9.   <name>myService</name>
  10. 10.   <artifactId>myService</artifactId>
  11. 11.   <packaging>jar</packaging>

</project>

 

  1. 新建一个接口类

13. package com.xiaoshuai;

  1. 14.  

15. public interface HelloService {

  1. 16.     public String speakHello(String name);

}

 

  1. 以上工做完成后,进行maven install
  • myProvider项目相关配置
  1. pom配置
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.   <parent>
  5.     <artifactId>mydubbo</artifactId>
  6.     <groupId>com</groupId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.   </parent>
  9.   <artifactId>myProvider</artifactId>

10. 引用接口服务项目 子项目maven install 为jar

  1. 11.     <dependencies>
  2. 12.              <dependency>
  3. 13.                       <groupId> myService </groupId>
  4. 14.                       <artifactId>myService</artifactId>
  5. 15.                       <version>0.0.1-SNAPSHOT</version>
  6. 16.              </dependency>
  7. 17.     </dependencies>

</project>

18. 实现接口服务者

19. package com.xiaoshuai.impl;

  1. 20.  
  2. 21.  

22. import com.xiaoshuai.HelloService;

23. public class HelloServiceImpl implements HelloService{

  1. 24.  
  2. 25.     public String speakHello(String name) {
  3. 26.              return "你好:"+name+"欢迎查阅小帅丶博客";
  4. 27.     }
  5. 28.    

29. }

  1. 将实现类接口类暴露给dubbo+zookeeper 在myProvider建立provider.xml 内容以下

31. <?xml version="1.0" encoding="UTF-8"?>

32. <beans xmlns="http://www.springframework.org/schema/beans" 

  1. 33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  2. 34.        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
  3. 35.        xsi:schemaLocation="http://www.springframework.org/schema/beans       
  4. 36.        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
  5. 37.   
  6. 38.     <!-- 提供方应用信息,用于计算依赖关系 --> 
  7. 39.     <dubbo:application name="hello-provider"  /> 
  8. 40.   
  9. 41.     <!-- 使用multicast广播注册中心暴露服务地址 --> 
  10. 42.     <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 
  11. 43.   
  12. 44.     <!-- 用dubbo协议在20880端口暴露服务 --> 
  13. 45.     <dubbo:protocol name="dubbo" port="20880" /> 
  14. 46.   
  15. 47.     <!-- 声明须要暴露的服务接口 --> 
  16. 48.     <dubbo:service interface="com.xiaoshuai.HelloService" ref="helloService" /> 
  17. 49.  
  18. 50.     <!-- 和本地bean同样实现服务 --> 
  19. 51.     <bean id="helloService" class="com.xiaoshuai.impl.HelloServiceImpl" /> 
  20. 52.   

53. </beans> 

  1. 须要建立一个Java类写个方法 去 加载provider.xml 注册到dubbo + zookeeper

55. package com.xiaoshuai.impl;

56. import org.springframework.context.support.ClassPathXmlApplicationContext;

  1. 57.  

58. public class ProviderServer {

  1. 59.     public static void main(String[] args){
  2. 60.              try {
  3. 61.                       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
  4. 62.                       context.start();
  5. 63.                       System.in.read();
  6. 64.              } catch (Exception e) {
  7. 65.                       e.printStackTrace();
  8. 66.              }
  9. 67.     }

68. }

  1. 能够运行。启动该提供者服务,编写消费者。
  2. 能够看到dubbo 提供者服务已经注册

 

 

  • myConsumer 项目相关配置
  1. pom配置
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.   <parent>
  5.     <artifactId>mydubbo</artifactId>
  6.     <groupId>com</groupId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.   </parent>
  9.   <artifactId>myConsumer</artifactId>
  10. 10.     <dependencies>
  11. 11.              <dependency>
  12. 12.                       <groupId>com</groupId>
  13. 13.                       <artifactId>myService</artifactId>
  14. 14.                       <version>0.0.1-SNAPSHOT</version>
  15. 15.              </dependency>
  16. 16.     </dependencies>

</project>

  1. 消费者进行订阅provider的服务,也就是须要去zookeeper读取加载服务,那就须要建立一个consumer.xml去加载zookeeper

18. <?xml version="1.0" encoding="UTF-8"?>

19. <beans xmlns="http://www.springframework.org/schema/beans"

  1. 20.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  2. 21.     xsi:schemaLocation="http://www.springframework.org/schema/beans       
  3. 22.        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  4. 23.     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方同样 -->
  5. 24.     <dubbo:application name="hello-consumer" />
  6. 25.     <!-- 使用multicast广播注册中心暴露发现服务地址 -->
  7. 26.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  8. 27.     <!-- 生成远程服务代理,能够和本地bean同样使用demoService -->
  9. 28.     <dubbo:reference id="helloService" interface="com.xiaoshuai.HelloService" />

29. </beans>

  1. 建立一个消费者方法,进行调用ConsumerClient

31. package com.xiaoshuai;

  1. 32.  

33. import org.springframework.context.support.ClassPathXmlApplicationContext;

  1. 34.  

35. public class ConsumerClient {

  1. 36.     public static void main(String[] args) {
  2. 37.              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
  3. 38.              HelloService helloService = (HelloService) context.getBean("helloService");
  4. 39.              String result = helloService.speakHello("xiaoshuai");
  5. 40.              System.out.println(result);
  6. 41.     }

42. }

  1. 采起debug的方式运行该方法输出如下内容
  2. 接下来看dubbo中心有什么内容。
  3. 看看消费者里面显示些什么内容
  4. 详细消费者信息如图所示
    • 以上一个简单的分布式的Demo就已经完结
相关文章
相关标签/搜索