springmvc整合dubbo分布式系统的搭建demo

 

四个步骤 

1 搭建zookeeper消息中心 

2 搭建dubbo服务端代码 

3 写dubbo客户端代码 

4 dubbo提供的dubbo-admin-2.5.4.war 管理控制台 

一,搭建zookeeper


1 下载zookeeper安装文件 
    zookeeper-3.3.6.tar.gz 
   这里提供一个百度网盘的下载地址http://pan.baidu.com/s/1gexxfvP 

2  执行命令 tar zxvf zookeeper-3.4.6.tar.gz 解压, 

3 到 zookeeper-3.3.6/conf 目录下 

   把zoo_sample.cfg 复制一份为zoo.cfg,作为默认配置文件


springmvc整合dubbo分布式系统的搭建demo_第1张图片

4 配置说明 

tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。 
dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。 
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。 

我的配置: 端口2181是默认配置

 

springmvc整合dubbo分布式系统的搭建demo_第2张图片

 

5 在zookper-3.3.6/bin目录下执行 

    ./zkServer.sh start 启动zookeeper

 


6 验证zookeeper是否启动 

     使用netstat -lpn | grep 2181 
     如果有端口号, 说明已经启动

 

 

二 DubboServer 工程

1,pom.xml

Xml代码   收藏代码
  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/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0modelVersion>  
  4.   <groupId>com.zhang.dubboServergroupId>  
  5.   <artifactId>dubboServerartifactId>  
  6.   <packaging>warpackaging>  
  7.   <version>0.0.1-SNAPSHOTversion>  
  8.   <name>dubboServer Maven Webappname>  
  9.   <url>http://maven.apache.orgurl>  
  10.   <dependencies>  
  11.     <dependency>  
  12.       <groupId>junitgroupId>  
  13.       <artifactId>junitartifactId>  
  14.       <version>3.8.1version>  
  15.       <scope>testscope>  
  16.     dependency>  
  17.     <dependency>    
  18.         <groupId>javax.servletgroupId>    
  19.         <artifactId>servlet-apiartifactId>    
  20.         <version>2.4version>    
  21.       dependency>    
  22.       <dependency>    
  23.         <groupId>javax.servlet.jspgroupId>    
  24.         <artifactId>jsp-apiartifactId>    
  25.         <version>2.1version>    
  26.       dependency>  
  27.       <dependency>  
  28.         <groupId>org.springframeworkgroupId>  
  29.         <artifactId>spring-contextartifactId>  
  30.          <version>4.1.6.RELEASEversion>  
  31.      dependency>  
  32.       <dependency>    
  33.         <groupId>org.springframeworkgroupId>    
  34.         <artifactId>spring-contextartifactId>    
  35.         <version>4.1.6.RELEASEversion>    
  36.       dependency>    
  37.       <dependency>    
  38.         <groupId>org.springframeworkgroupId>    
  39.         <artifactId>spring-webmvcartifactId>    
  40.         <version>4.1.6.RELEASEversion>    
  41.       dependency>  
  42.       <dependency>    
  43.         <groupId>jstlgroupId>    
  44.         <artifactId>jstlartifactId>    
  45.         <version>1.2version>    
  46.     dependency>   
  47.     <dependency>  
  48.         <groupId>dubbogroupId>  
  49.         <artifactId>dubboartifactId>  
  50.         <version>2.5.3version>  
  51.     dependency>  
  52.     <dependency>  
  53.       <groupId>javassistgroupId>  
  54.       <artifactId>javassistartifactId>  
  55.       <version>3.15.0-GAversion>  
  56.       <classifier>GAclassifier>  
  57.     dependency>  
  58.     <dependency>  
  59.         <groupId>nettygroupId>  
  60.         <artifactId>nettyartifactId>  
  61.         <version>3.2.5.Finalversion>  
  62.     dependency>  
  63.     <dependency>  
  64.         <groupId>log4jgroupId>  
  65.         <artifactId>log4jartifactId>  
  66.         <version>1.2.17version>  
  67.     dependency>  
  68.     <dependency>  
  69.         <groupId>zkclientgroupId>  
  70.         <artifactId>zkclientartifactId>  
  71.         <version>0.4version>  
  72.     dependency>  
  73.     <dependency>  
  74.         <groupId>zookeepergroupId>  
  75.         <artifactId>zookeeperartifactId>  
  76.         <version>3.3.1version>  
  77.     dependency>  
  78.   dependencies>  
  79.   <build>  
  80.     <finalName>dubboServerfinalName>  
  81.   build>  
  82. project>  

 

2.web.xml
Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <web-app  
  4.         xmlns="http://java.sun.com/xml/ns/javaee"  
  5.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  7.         version="3.0"  
  8.         metadata-complete="false">  
  9.     <context-param>  
  10.         <param-name>contextConfigLocationparam-name>  
  11.         <param-value>/WEB-INF/spring/springmvc-config.xml;classpath*:dubbo/*.xmlparam-value>  
  12.     context-param>  
  13.     <listener>  
  14.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  15.     listener>  
  16.     <servlet>  
  17.         <servlet-name>DispatcherServletservlet-name>  
  18.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  19.         <init-param>  
  20.             <param-name>contextConfigLocationparam-name>  
  21.             <param-value>/WEB-INF/spring/springmvc-config.xmlparam-value>  
  22.         init-param>  
  23.         <load-on-startup>1load-on-startup>  
  24.         <async-supported>trueasync-supported>  
  25.     servlet>  
  26.   
  27.     <servlet-mapping>  
  28.         <servlet-name>DispatcherServletservlet-name>  
  29.         <url-pattern>/url-pattern>  
  30.     servlet-mapping>  
  31.       
  32.     <filter>  
  33.          <filter-name>CharacterEncodingFilterfilter-name>  
  34.           <filter-class>  
  35.             org.springframework.web.filter.CharacterEncodingFilter  
  36.          filter-class>  
  37.          <async-supported>trueasync-supported>  
  38.          <init-param>  
  39.                 <param-name>encodingparam-name>  
  40.                 <param-value>UTF-8param-value>  
  41.          init-param>  
  42.          <init-param>  
  43.               <param-name>forceEncodingparam-name>  
  44.               <param-value>trueparam-value>  
  45.          init-param>  
  46.     filter>  
  47.     <filter-mapping>  
  48.          <filter-name>CharacterEncodingFilterfilter-name>  
  49.          <url-pattern>/*url-pattern>  
  50.     filter-mapping>   
  51.   
  52.   
  53. web-app>  
 3,spring和dubbo配置文件
Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans:beans xmlns="http://www.springframework.org/schema/mvc"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:beans="http://www.springframework.org/schema/beans"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  9.   
  10.   
  11.       
  12.     <resources mapping="/resources/**" location="/resources/" />  
  13.   
  14.       
  15.     <annotation-driven >annotation-driven>  
  16.       
  17.     <context:component-scan base-package="com.zhang" />  
  18.   
  19.     <context:property-placeholder location="classpath:config.properties" />  
  20.       
  21.     <beans:bean id="sayHelloService" class="com.zhang.service.demoImpl"/>  
  22.       
  23.       
  24. beans:beans>  
 
Xml代码   收藏代码
  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="DubboDemo" />    
  11.     
  12.         
  13.     <dubbo:registry protocol="zookeeper" address="${able_zookeeper}"  />    
  14.       
  15.     <dubbo:monitor protocol="registry"/>  
  16.       
  17.       
  18.     <dubbo:protocol name="dubbo" port="31581" />  
  19.   
  20.       
  21.         
  22.     <dubbo:provider delay="-1" timeout="6000" retries="0"/>  
  23.     <dubbo:consumer timeout="6000" retries="0"/>  
  24. beans>  
 
Xml代码   收藏代码
  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:service interface="com.zhang.service.demo" ref="sayHelloService"  timeout="3000" protocol="dubbo"/>  
  10. beans>      
config.properties
Java代码   收藏代码
  1. #able_zookeeper  
  2. able_zookeeper=192.168.50.42:2181  
4,java代码 一个接口一个实现类
Java代码   收藏代码
  1. package com.zhang.service;  
  2.   
  3. public interface demo {  
  4.     public String sayHello();  
  5. }  
 
Java代码   收藏代码
  1. package com.zhang.service;  
  2.   
  3. public class demoImpl implements demo {  
  4.   
  5.     @Override  
  6.     public String sayHello() {  
  7.         return "hello";  
  8.     }  
  9.   
  10. }  
 
 

三 DubboClient工程

 

1,pom.xml和web.xml,config.properties配置文件和DubboServer配置一样

2,增加一个消费者配置文件applicationContext-dubbo-consumer.xml,注:这里需要把服务提供者的jar包打入到客户端,打接口就可以

Xml代码   收藏代码
  1. <strong>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:reference id="demo" interface="com.zhang.service.demo" check="false" protocol="dubbo"/>  
  10. beans> strong>  

 3,使用

Java代码   收藏代码
  1. package com.zhang.contr;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4.   
  5.   
  6. public class test {  
  7.     @Autowired  
  8.     private com.zhang.service.demo demo;  
  9.       
  10.     public String testD(){  
  11.         return demo.sayHello();  
  12.     }  
  13. }  
  14.   

 

 

四 dubbo-admin-2.5.4.war 管理控制台

    war包下载地址:http://pan.baidu.com/s/1i4xZamD

      把这个war包放到tomcat的webapps目录下 

      1 修改tomcat的端口号不要和其他服务冲突.

      2 修改 WEB-INF 下的dubbo.properties文件

 

      

 

     3 启动tomcat,访问.

 

springmvc整合dubbo分布式系统的搭建demo_第3张图片

 

     想看源码的提供一个github的访问地址

https://github.com/zqh1989/DubboDemo

 

你可能感兴趣的:(J2EE)