用 maven 搭建 springmvc + dubbo + zookeeper 框架(生产者、消费者)

原创 2018年03月20日 15:49:16

首先要根据《maven搭建springmvc(eclipse)》新建3个maven项目:消费者项目(dubbo-consumer)、生产者项目(dubbo-provider)、公共接口项目(dubbo-interface),如图:

公共接口项目:

接口文件DemoService.java代码:

package com.shan.interf.service;

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

生产者项目:

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.shan</groupId>
  <artifactId>dubbo-provider</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>dubbo-provider Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
      <spring.version>4.1.2.RELEASE</spring.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    
    <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>dubbo</artifactId>
    	<version>2.5.3</version>
    </dependency>
    
    <dependency>
    	<groupId>org.apache.zookeeper</groupId>
    	<artifactId>zookeeper</artifactId>
    	<version>3.3.3</version>
    </dependency>
    
    <dependency>
 		<groupId>log4j</groupId>
 		<artifactId>log4j</artifactId>
 		<version>1.2.15</version>
 		<exclusions>
	  		<exclusion>
		     	<groupId>com.sun.jmx</groupId>
		     	<artifactId>jmxri</artifactId>
	  		</exclusion>
  			<exclusion>
     			<groupId>com.sun.jdmk</groupId>
     			<artifactId>jmxtools</artifactId>
  			</exclusion>
  			<exclusion>
      			<groupId>javax.jms</groupId>
      			<artifactId>jms</artifactId>
  			</exclusion>
 		</exclusions>
	</dependency> 
    
    <dependency>
	    <groupId>com.101tec</groupId>
	    <artifactId>zkclient</artifactId>
	    <version>0.10</version>
	</dependency>
    
  </dependencies>

	<build>
		<finalName>dubbo-provider</finalName>
	    <plugins>
	      <plugin>
	        <groupId>org.apache.maven.plugins</groupId>
	        <artifactId>maven-compiler-plugin</artifactId>
	        <version>3.0</version>
	        <configuration>
	          <source>1.8</source>
	          <target>1.8</target>
	        </configuration>
	      </plugin>
	    </plugins>
	</build>
</project>

dubbo配置文件dubbo-provider.xml代码:

<?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管理页面比较清晰是哪个应用暴露出来的 -->  
    <dubbo:application name="dubbo_provider"/>  
    
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
    <!-- 具体的实现bean -->
    <bean id="demoService" class="com.shan.provider.service.impl.DemoServiceImpl"/>
    
    <!-- 要暴露的服务接口 -->  
    <dubbo:service interface="com.shan.interf.service.DemoService" ref="demoService"/>  
      
</beans>

springmvc配置文件spring-servlet.xml代码:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 配置扫描的包 -->
    <context:component-scan base-package="com.shan.provider"></context:component-scan>
    
    <!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
    <mvc:annotation-driven />

    <!-- 访问静态资源 -->
    <mvc:default-servlet-handler />
    
    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 引用dubbo -->
    <import resource="dubbo-provider.xml"></import>
    
</beans>

接口实现类DemoServiceImpl.java代码:

package com.shan.provider.service.impl;

import com.shan.interf.service.DemoService;

public class DemoServiceImpl implements DemoService {
	
    public String sayHello(String name) {
        return "Hello Dubbo,Hello " + name;
    }
 
}

生产者启动类(测试用)Provider.java代码:

package com.shan.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

	public static void main(String[] args) {
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"spring-servlet.xml"});
        context.start();
        System.out.println("生产者服务已经注册成功!");
        try {
            System.in.read();//让此程序一直跑,表示一直提供服务
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
}

注:本地需要安装zookeeper才能成功启动,方法参考:《zookeeper 下载、安装、启动(windows)》

消费者项目:

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.shan</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>dubbo-consumer Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
      <spring.version>4.1.2.RELEASE</spring.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    
    <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>dubbo</artifactId>
    	<version>2.5.3</version>
    </dependency>
    
    <dependency>
    	<groupId>org.apache.zookeeper</groupId>
    	<artifactId>zookeeper</artifactId>
    	<version>3.3.3</version>
    </dependency>
    
    <dependency>
 		<groupId>log4j</groupId>
 		<artifactId>log4j</artifactId>
 		<version>1.2.15</version>
 		<exclusions>
	  		<exclusion>
		     	<groupId>com.sun.jmx</groupId>
		     	<artifactId>jmxri</artifactId>
	  		</exclusion>
  			<exclusion>
     			<groupId>com.sun.jdmk</groupId>
     			<artifactId>jmxtools</artifactId>
  			</exclusion>
  			<exclusion>
      			<groupId>javax.jms</groupId>
      			<artifactId>jms</artifactId>
  			</exclusion>
 		</exclusions>
	</dependency> 
    
    <dependency>
	    <groupId>com.101tec</groupId>
	    <artifactId>zkclient</artifactId>
	    <version>0.10</version>
	</dependency>
    
  </dependencies>

  <build>
    <finalName>dubbo-consumer</finalName>
  </build>
</project>
dubbo配置文件dubbo-consumer.xml代码:
<?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管理页面比较清晰是哪个应用暴露出来的 -->  
    <dubbo:application name="dubbo_consumer"/>
    
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="true"/>
  	
  	<!-- 生成远程服务代理,可以像使用本地bean一样使用 -->
    <dubbo:reference interface="com.shan.interf.service.DemoService" id="demoService"/>  
  
</beans>
springmvc配置文件spring-servlet.xml代码:
<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 配置扫描的包 -->
    <context:component-scan base-package="com.shan"></context:component-scan>
    
    <!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
    <mvc:annotation-driven />

    <!-- 访问静态资源 -->
    <mvc:default-servlet-handler />
    
    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 引用dubbo -->
    <import resource="dubbo-consumer.xml"></import>
    
</beans>

消费者调用生产者测试类Consumer.java,代码:

package com.shan.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.shan.interf.service.DemoService;

public class Consumer {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"spring-servlet.xml"});
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService");  
        String hello = demoService.sayHello("world"); 
        System.out.println(hello);
    }
	
}
执行结果:

生产者项目与消费者项目都需要引用公用接口项目。

生产者项目启动结果,如图:


消费者项目调用生产者结果,如图:


zookeeper

-
  • 1970年01月01日 08:00

Dubbo+Zookeeper 一个消费者对多个生产者的配置流程

承接上文补充,消费者/生产者的service层包结构必须一致。消费者service层到接口为止。生产者则从serviceImpl开始,但是上级包结构都是一样的。无论生产者还是消费者,xml中inter...
  • Lenux2017
  • Lenux2017
  • 2017-11-16 15:28:31
  • 1888

maven项目搭建dubbo的消费者和生产者

生产者:  pom.xml需要依赖的: properties> spring.version>3.2.4.RELEASEspring.version> properties> ...
  • yinbucheng
  • yinbucheng
  • 2017-08-04 22:24:52
  • 1106

DUBBO分布式服务框架与SpringMVC+Zookeeper实现服务提供和消费

DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广...
  • boonya
  • boonya
  • 2017-04-06 18:43:22
  • 2612

Maven+SpringMVC+Dubbo+zookeeper 简单的入门demo配置

参考:http://blog.csdn.net/aixiaoyang168/article/details/51362675dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用...
  • toto1297488504
  • toto1297488504
  • 2017-05-26 11:40:39
  • 2414

基于maven+dubbo+spring+zookeeper的简单项目搭建

Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。 Dubbo[]是一个分布式服务框架,...
  • sinat_32624775
  • sinat_32624775
  • 2016-05-25 17:12:42
  • 8815

手把手带你搭建 SpringMVC + Zookeeper +Dubbo(一)【Maven 创建 Web项目】

1. Maven 项目创建 在 Project Exploer 区域单机右键,选择【new】-> 选择【other】 在弹出框中输入 Maven,选择列表展示的 Maven Project 在第一个提...
  • github_34889651
  • github_34889651
  • 2016-07-29 14:20:32
  • 4390

IDEA Dubbo+ZooKeeper+SpringMVC 多模块小程序

本文主要用了Dubbo+ZooKeeper+SpringMVC做的一个小程序,阅读请确保您的dubbo和zookperr的服务已经搭建好 开始:新建一个空的MAVEN项目 点击完成,建立最外层...
  • guyang5863637
  • guyang5863637
  • 2017-03-14 13:19:54
  • 2245

maven下搭建springMVC+mybatis+mysql+zookeeper+dubbo

我这边是从没有maven开始搭建的,也顺便说下maven吧 1.maven本地仓库搭建    取官网下载maven安装包:http://www.apache.org/dyn/closer.cgi/...
  • zwrlj527
  • zwrlj527
  • 2017-09-30 16:44:47
  • 1121

dubbo+zookeeper+spring+springMVC+mybatis的使用

源码下载链接:http://files.cnblogs.com/files/la-tiao-jun-blog/dubbo%E4%BE%8B%E5%AD%90.rar 使用条件:      ecli...
  • whb123andy
  • whb123andy
  • 2017-05-18 13:07:19
  • 663
收藏助手
不良信息举报
您举报文章:用 maven 搭建 springmvc + dubbo + zookeeper 框架(生产者、消费者)
举报原因:
原因补充:

(最多只允许输入30个字)