springmvc整合dubbo分布式系统的搭建小例子

四个步骤

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分布式系统的搭建小例子_第1张图片

4 配置说明

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

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

 

springmvc整合dubbo分布式系统的搭建小例子_第2张图片

 

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

    ./zkServer.sh start 启动zookeeper

 


6 验证zookeeper是否启动

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

 

 

二 DubboServer 工程

1,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.zhang.dubboServer</groupId>
  <artifactId>dubboServer</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>dubboServer Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>  
	    <groupId>javax.servlet</groupId>  
	    <artifactId>servlet-api</artifactId>  
	    <version>2.4</version>  
	  </dependency>  
	  <dependency>  
	    <groupId>javax.servlet.jsp</groupId>  
	    <artifactId>jsp-api</artifactId>  
	    <version>2.1</version>  
	  </dependency>
	  <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
         <version>4.1.6.RELEASE</version>
     </dependency>
	  <dependency>  
	    <groupId>org.springframework</groupId>  
	    <artifactId>spring-context</artifactId>  
	    <version>4.1.6.RELEASE</version>  
	  </dependency>  
	  <dependency>  
	    <groupId>org.springframework</groupId>  
	    <artifactId>spring-webmvc</artifactId>  
	    <version>4.1.6.RELEASE</version>  
	  </dependency>
	  <dependency>  
        <groupId>jstl</groupId>  
	    <artifactId>jstl</artifactId>  
	    <version>1.2</version>  
	</dependency> 
	<dependency>
		<groupId>dubbo</groupId>
		<artifactId>dubbo</artifactId>
		<version>2.5.3</version>
	</dependency>
	<dependency>
	  <groupId>javassist</groupId>
	  <artifactId>javassist</artifactId>
	  <version>3.15.0-GA</version>
	  <classifier>GA</classifier>
	</dependency>
	<dependency>
		<groupId>netty</groupId>
		<artifactId>netty</artifactId>
		<version>3.2.5.Final</version>
	</dependency>
	<dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
	<dependency>
		<groupId>zkclient</groupId>
		<artifactId>zkclient</artifactId>
		<version>0.4</version>
	</dependency>
	<dependency>
		<groupId>zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		<version>3.3.1</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>dubboServer</finalName>
  </build>
</project>

 

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

<web-app
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0"
        metadata-complete="false">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/springmvc-config.xml;classpath*:dubbo/*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/springmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <filter>
         <filter-name>CharacterEncodingFilter</filter-name>
          <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
         </filter-class>
         <async-supported>true</async-supported>
         <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
         </init-param>
         <init-param>
              <param-name>forceEncoding</param-name>
              <param-value>true</param-value>
         </init-param>
	</filter>
	<filter-mapping>
         <filter-name>CharacterEncodingFilter</filter-name>
         <url-pattern>/*</url-pattern>
	</filter-mapping> 


</web-app>
 3,spring和dubbo配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven ></annotation-driven>
	<!--spring 自动扫描com.zhang下边的所有类  -->
	<context:component-scan base-package="com.zhang" />

	<context:property-placeholder location="classpath:config.properties" />
	<!-- 自定义配置Service -->
	<beans:bean id="sayHelloService" class="com.zhang.service.demoImpl"/>
	
	
</beans:beans>
 
<?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="DubboDemo" />  
  
    <!-- 注册中心 -->  
    <dubbo:registry protocol="zookeeper" address="${able_zookeeper}"  />  
    <!-- 是否纳入调用统计报表(可选) -->
    <dubbo:monitor protocol="registry"/>
    
	<!-- 协议 -->
	<dubbo:protocol name="dubbo" port="31581" />

	<!-- 服务者与消费者的默认配置 -->
	<!-- 延迟到Spring初始化完成后,再暴露服务,服务调用超时设置为6秒,超时不重试   -->  
	<dubbo:provider delay="-1" timeout="6000" retries="0"/>
	<dubbo:consumer timeout="6000" retries="0"/>
</beans>
 
<?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:service interface="com.zhang.service.demo" ref="sayHelloService"  timeout="3000" protocol="dubbo"/>
</beans>    
config.properties
#able_zookeeper
able_zookeeper=192.168.50.42:2181
4,java代码 一个接口一个实现类
package com.zhang.service;

public interface demo {
	public String sayHello();
}
 
package com.zhang.service;

public class demoImpl implements demo {

	@Override
	public String sayHello() {
		return "hello";
	}

}
 
 

三 DubboClient工程

 

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

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

<?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:reference id="demo" interface="com.zhang.service.demo" check="false" protocol="dubbo"/>
</beans> 

 3,使用

package com.zhang.contr;

import org.springframework.beans.factory.annotation.Autowired;


public class test {
	@Autowired
	private com.zhang.service.demo demo;
	
	public String testD(){
		return demo.sayHello();
	}
}

 

 

四 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分布式系统的搭建小例子_第3张图片

 

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

https://github.com/zqh1989/DubboDemo

 

 

你可能感兴趣的:(springmvc整合dubbo分布式系统的搭建小例子)