Maven配置dubbo环境简单例子

环境准备:

  1.zookeeper:zookeeper-3.4.6版本

  2.maven:apache-maven-3.3.9版本

  3.dubbo监控工具:dubbo-admin-2.5.4-SNAPSHOT 一个war包,用户查看注册中心的提供者和消费者

 

项目结构:

 dubbo-provider-test

  |--src/main/java 程序目录

    |--com.wengen.dubbo_provider_test.service      提供者接口目录

      |--DomeService.java

    |--com.wengen.dubbo_provider_test.service.impl  提供者接口实现目录

      |--DomeServiceImpl.java

   |--src/test/java 测试目录

    |--com.wengen.dubbo_provider_test

      |--DomeServiceTest.java  提供者启动类

      |--DomeServiceConsumerTest.java 消费者启动类

 

  |--src/main/resources

    |--spring.xml spring 配置文件

    |--config

      |--provider.xml 提供者配置文件

      |--consumer.xml 消费者配置文件

  |--pom.xml maven的pom文件

 

源码:

pom.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/xsd/maven-4.0.0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4 
  5   <groupId>com.wengen</groupId>
  6   <artifactId>dubbo-provider-test</artifactId>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <packaging>jar</packaging>
  9 
 10   <name>dubbo-provider-test</name>
 11   <url>http://maven.apache.org</url>
 12     
 13   <properties>
 14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 15         <druid.version>1.0.18</druid.version>
 16         <jaxen.version>1.1.1</jaxen.version>
 17         <lombok.version>1.14.4</lombok.version>
 18         <slf4j.version>1.7.5</slf4j.version>
 19         <dom4j.version>1.6.1</dom4j.version>
 20         <spring.version>4.2.5.RELEASE</spring.version>
 21         <commons-httpclient.version>3.0.1</commons-httpclient.version>
 22         <dubbo.version>2.5.2</dubbo.version>
 23         <zkclient.version>0.1</zkclient.version>
 24         <project.junit.version>4.8.2</project.junit.version>
 25         <spring.amqp.version>1.2.0.RELEASE</spring.amqp.version>
 26         <spring.version>4.2.5.RELEASE</spring.version>
 27         <spring-core.version>4.2.5.RELEASE</spring-core.version>
 28         <aspectj.version>1.7.0</aspectj.version>
 29         <aopalliance.version>1.0</aopalliance.version>
 30         <asm.version>3.1</asm.version>
 31         <mybatis.version>3.2.8</mybatis.version>
 32     </properties>
 33 
 34 
 35   <dependencies>
 36   
 37   
 38     <dependency>
 39       <groupId>junit</groupId>
 40       <artifactId>junit</artifactId>
 41       <version>3.8.1</version>
 42       <scope>test</scope>
 43     </dependency>
 44     
 45      <dependency>
 46             <groupId>com.github.sgroschupf</groupId>
 47             <artifactId>zkclient</artifactId>
 48             <version>${zkclient.version}</version>
 49         </dependency>
 50     
 51      <dependency>
 52             <groupId>com.alibaba</groupId>
 53             <artifactId>dubbo</artifactId>
 54             <version>${dubbo.version}</version>
 55             <exclusions>
 56                 <exclusion>
 57                     <groupId>org.springframework</groupId>
 58                     <artifactId>spring</artifactId>
 59                 </exclusion>
 60             </exclusions>
 61         </dependency>
 62     
 63     <dependency>
 64             <groupId>org.springframework</groupId>
 65             <artifactId>spring-context</artifactId>
 66             <version>${spring.version}</version>
 67         </dependency>
 68         <dependency>
 69             <groupId>org.springframework</groupId>
 70             <artifactId>spring-aop</artifactId>
 71             <version>${spring.version}</version>
 72         </dependency>
 73         <dependency>
 74             <groupId>org.springframework</groupId>
 75             <artifactId>spring-tx</artifactId>
 76             <version>${spring.version}</version>
 77         </dependency>
 78         <dependency>
 79             <groupId>org.springframework</groupId>
 80             <artifactId>spring-context-support</artifactId>
 81             <version>${spring.version}</version>
 82         </dependency>
 83         <dependency>
 84             <groupId>org.springframework</groupId>
 85             <artifactId>spring-jdbc</artifactId>
 86             <version>${spring.version}</version>
 87         </dependency>
 88         <dependency>
 89             <groupId>org.springframework</groupId>
 90             <artifactId>spring-web</artifactId>
 91             <version>${spring.version}</version>
 92         </dependency>
 93         <dependency>
 94             <groupId>org.springframework</groupId>
 95             <artifactId>spring-webmvc</artifactId>
 96             <version>${spring.version}</version>
 97         </dependency>
 98         <dependency>
 99             <groupId>org.springframework</groupId>
100             <artifactId>spring-aspects</artifactId>
101             <version>${spring.version}</version>
102         </dependency>
103         <dependency>
104             <groupId>org.springframework</groupId>
105             <artifactId>spring-beans</artifactId>
106             <version>${spring.version}</version>
107         </dependency>
108         <dependency>
109             <groupId>org.springframework</groupId>
110             <artifactId>spring-core</artifactId>
111             <version>${spring.version}</version>
112         </dependency>
113         <dependency>
114             <groupId>org.springframework</groupId>
115             <artifactId>spring-jms</artifactId>
116             <version>${spring.version}</version>
117         </dependency>
118         <dependency>
119             <groupId>org.springframework</groupId>
120             <artifactId>spring-expression</artifactId>
121             <version>${spring.version}</version>
122         </dependency>
123         <dependency>
124             <groupId>org.springframework</groupId>
125             <artifactId>spring-instrument</artifactId>
126             <version>${spring.version}</version>
127         </dependency>
128         <dependency>
129             <groupId>org.springframework</groupId>
130             <artifactId>spring-orm</artifactId>
131             <version>${spring.version}</version>
132         </dependency>
133         <dependency>
134             <groupId>org.springframework</groupId>
135             <artifactId>spring-oxm</artifactId>
136             <version>${spring.version}</version>
137         </dependency>
138         <dependency>
139             <groupId>org.springframework</groupId>
140             <artifactId>spring-test</artifactId>
141             <version>${spring.version}</version>
142             <scope>test</scope>
143         </dependency>
144         <dependency>
145             <groupId>org.springframework.amqp</groupId>
146             <artifactId>spring-rabbit</artifactId>
147             <version>${spring.amqp.version}</version>
148         </dependency>
149     
150   </dependencies>
151 </project>
View Code

 

spring.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:aop="http://www.springframework.org/schema/aop"
 4        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
 6        xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 9         http://www.springframework.org/schema/tx
10         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
11         http://www.springframework.org/schema/aop
12         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
13         http://www.springframework.org/schema/context
14         http://www.springframework.org/schema/context/spring-context-3.1.xsd
15         http://www.springframework.org/schema/mvc 
16         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
17         http://www.springframework.org/schema/task 
18         http://www.springframework.org/schema/task/spring-task-3.1.xsd">
19 
20     <!-- 自动扫描bean,把作了注解的类转换为bean -->
21     <context:component-scan base-package="com.wengen.dubbo_provider_test.service">
22         <context:include-filter type="annotation"
23                                 expression="com.alibaba.dubbo.config.annotation.Service" />
24     </context:component-scan>
25 
26     <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
27     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
28     <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
29     <!-- 强制使用CGLIB代理 -->
30     <aop:aspectj-autoproxy proxy-target-class="true" />
31     <task:annotation-driven />
32     <context:annotation-config />
33     <import resource="classpath:/config/provider.xml" />
34 </beans>
View Code

 

提供者配置文件:provider.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"
 4     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans        
 6     http://www.springframework.org/schema/beans/spring-beans.xsd       
 7      http://code.alibabatech.com/schema/dubbo        
 8      http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 9      
10       <!-- 提供方应用信息,用于计算依赖关系 -->
11      <dubbo:application name="dubbo-provider-test" />
12      
13      <!-- 使用multicast广播注册中心暴露服务地址 -->
14      <dubbo:registry protocol="zookeeper"  address="127.0.0.1:2181" />
15      
16       <!-- 用dubbo协议在20880端口暴露服务 -->
17         <dubbo:protocol name="dubbo" port="20881" />
18         
19         <!-- 声明需要暴露的服务接口 -->
20         <dubbo:service interface="com.wengen.dubbo_provider_test.service.DomeService" ref="domeservice"/>
21         
22         <!-- 和本地bean一样实现服务 -->
23         <bean id="domeservice" class="com.wengen.dubbo_provider_test.service.impl.DomeServiceImpl"/>
24 </beans>
25  
View Code

 

消费者配置文件:consumer.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"
 4     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans        
 6     http://www.springframework.org/schema/beans/spring-beans.xsd        
 7     http://code.alibabatech.com/schema/dubbo        
 8     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 9     
10     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
11     <dubbo:application name="dubbo-consumer-test"  />
12     
13     <!-- 使用multicast广播注册中心暴露服务地址 -->
14      <dubbo:registry protocol="zookeeper"  address="127.0.0.1:2181" />
15      
16       <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
17     <dubbo:reference id="demoService" interface="com.wengen.dubbo_provider_test.service.DomeService" />
18     
19     </beans>
20  
View Code

 

提供者接口:DomeService.java

1 package com.wengen.dubbo_provider_test.service;
2 
3 public interface DomeService {
4     String sayHello(String name);
5 }
View Code

 

提供者接口实现:DomeServiceImpl.java

 1 package com.wengen.dubbo_provider_test.service.impl;
 2 
 3 import com.wengen.dubbo_provider_test.service.DomeService;
 4 
 5 public class DomeServiceImpl implements DomeService {
 6 
 7     @Override
 8     public String sayHello(String name) {
 9     
10         return "Helle" + name;
11     }
12 
13 
14 }
View Code

 

提供者启动类:DomeServiceTest.java

 1 package com.wengen.dubbo_provider_test;
 2 
 3 import java.io.IOException;
 4 
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class DomeServiceTest {
 8 
 9     public static void main(String[] args)throws IOException {
10         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});
11         context.start();
12             //任意键退出
13             System.in.read();
14     }
15 
16 }
View Code

 

消费者启动类:DomeServiceConsumerTest.java

 1 package com.wengen.dubbo_provider_test;
 2 
 3 import java.io.IOException;
 4 
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.wengen.dubbo_provider_test.service.DomeService;
 8 
 9 public class DomeServiceConsumerTest  {
10     public static void main(String []args) throws IOException{
11         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String []{"classpath:/config/consumer.xml"});
12         context.start();
13         DomeService dome = (DomeService) context.getBean("demoService");
14         String hello = dome.sayHello(" ADSADS");
15         System.out.println(hello);
16     }
17 }
View Code

 

 

dubbo监控工具安装请参考:http://www.cnblogs.com/coreyjk/p/6560085.html

posted @ 2017-03-16 15:50  钓的不是鱼  阅读(1133)  评论(0编辑  收藏  举报