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

首先要根据《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);
    }
	
}
执行结果:

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

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


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


阅读更多

没有更多推荐了,返回首页