dubbo+zookeeper

首先是整个项目结构图:

dubbo-service:

1 package com.zto.service;
2 
3 public interface SayHelloService {
4     public String hello(String name);
5 }
SayHelloService.java
 1 package com.zto.service.impl;
 2 
 3 import com.zto.service.SayHelloService;
 4 
 5 public class SayHelloServiceImpl implements SayHelloService {
 6 
 7     public String hello(String name) {
 8         return "hello : " + name;
 9     }
10 
11 }
SayHelloServiceImpl.java
 1 package com.zto.app;
 2 
 3 import java.io.IOException;
 4 
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) throws IOException {
10         ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[] {"applicationContext-rpc.xml"});
11         context.start();
12         System.out.println("按任意键退出");
13         System.in.read();
14     }
15 }
Main.java
 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     <dubbo:application name="hello-word-service" />
11     
12     <dubbo:protocol name="dubbo" port="30900" />
13     
14     <dubbo:registry address="zookeeper://127.0.0.1:2181" />
15     
16     <dubbo:service interface="com.zto.service.SayHelloService" ref="sayHelloService" />
17     
18     <!-- 和本地bean一样实现服务 -->
19     <bean id="sayHelloService" class="com.zto.service.impl.SayHelloServiceImpl" />
20     
21 </beans>
applicationContext-rpc.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.0</modelVersion>
 4     <groupId>com.alibaba.zy</groupId>
 5     <artifactId>dubbo-service</artifactId>
 6     <packaging>jar</packaging>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <name>dubbo-service Maven Webapp</name>
 9     <dependencies>
10         <dependency>
11             <groupId>junit</groupId>
12             <artifactId>junit</artifactId>
13             <version>3.8.1</version>
14             <scope>test</scope>
15         </dependency>
16         <dependency>
17             <groupId>commons-logging</groupId>
18             <artifactId>commons-logging</artifactId>
19             <version>1.1.1</version>
20         </dependency>
21         <dependency>
22             <groupId>com.alibaba</groupId>
23             <artifactId>dubbo</artifactId>
24             <version>2.5.3</version>
25         </dependency>
26         <dependency>
27             <groupId>org.javassist</groupId>
28             <artifactId>javassist</artifactId>
29             <version>3.18.1-GA</version>
30         </dependency>
31         <dependency>
32             <groupId>log4j</groupId>
33             <artifactId>log4j</artifactId>
34             <version>1.2.15</version>
35             <exclusions>
36                 <exclusion>
37                     <groupId>com.sun.jdmk</groupId>
38                     <artifactId>jmxtools</artifactId>
39                 </exclusion>
40                 <exclusion>
41                     <groupId>com.sun.jmx</groupId>
42                     <artifactId>jmxri</artifactId>
43                 </exclusion>
44                 <exclusion>
45                     <artifactId>jms</artifactId>
46                     <groupId>javax.jms</groupId>
47                 </exclusion>
48                 <exclusion>
49                     <artifactId>mail</artifactId>
50                     <groupId>javax.mail</groupId>
51                 </exclusion>
52             </exclusions>
53         </dependency>
54         <dependency>
55             <groupId>org.springframework</groupId>
56             <artifactId>spring</artifactId>
57             <version>2.5.6.SEC03</version>
58         </dependency>
59         <dependency>
60             <groupId>org.slf4j</groupId>
61             <artifactId>slf4j-api</artifactId>
62             <version>1.7.6</version>
63         </dependency>
64         <dependency>
65             <groupId>org.slf4j</groupId>
66             <artifactId>slf4j-log4j12</artifactId>
67             <version>1.6.1</version>
68         </dependency>
69          <dependency>
70             <groupId>org.apache.zookeeper</groupId>
71             <artifactId>zookeeper</artifactId>
72             <version>3.4.6</version>
73         </dependency>
74         <dependency>
75             <groupId>com.github.sgroschupf</groupId>
76             <artifactId>zkclient</artifactId>
77             <version>0.1</version>
78         </dependency>
79         </dependencies>
80 </project>
pom.xml

dubbo-consumer:

 1 package com.zto.consumer;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 import com.zto.service.SayHelloService;
 6 
 7 public class ConsumerThd {
 8 
 9     public void sayHello() {
10         ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext-rpc*.xml"});
11         context.start();
12         SayHelloService sayHelloService = (SayHelloService) context.getBean("sayHelloService");
13         System.out.println(sayHelloService.hello("world"));
14     }
15 }
ConsumerThd.java
 1 package com.zto.test;
 2 
 3 import com.zto.consumer.ConsumerThd;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         ConsumerThd thd=new ConsumerThd();
 9         thd.sayHello();
10     }
11 
12 }
Main.java
 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     <description>消费者</description>
11     
12     <dubbo:application name="hello-world-consumer"/>
13     
14     <!-- 使用zookeeper广播注册中心暴露服务地址 -->
15     <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
16     
17     <dubbo:consumer check="false" />
18     
19     <!-- 业务 -->
20     <dubbo:reference id="sayHelloService" interface="com.zto.service.SayHelloService" timeout="15000" retries="0" />
21 </beans>
applicationContext-rpc.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.0</modelVersion>
 4     <groupId>com.zto</groupId>
 5     <artifactId>dubbo-consumer</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <name>dubbo-consumer</name>
 8     <dependencies>
 9         <dependency>
10             <groupId>junit</groupId>
11             <artifactId>junit</artifactId>
12             <version>3.8.1</version>
13             <scope>test</scope>
14         </dependency>
15         <dependency>
16             <groupId>commons-logging</groupId>
17             <artifactId>commons-logging</artifactId>
18             <version>1.1.1</version>
19         </dependency>
20             <dependency>
21                 <groupId>com.alibaba</groupId>
22                 <artifactId>dubbo</artifactId>
23                 <version>2.5.3</version>
24             </dependency>
25             <dependency>
26                 <groupId>org.javassist</groupId>
27                 <artifactId>javassist</artifactId>
28                 <version>3.18.1-GA</version>
29             </dependency>
30             <dependency>
31                 <groupId>log4j</groupId>
32                 <artifactId>log4j</artifactId>
33                 <version>1.2.15</version>
34                 <exclusions>
35                     <exclusion>
36                         <groupId>com.sun.jdmk</groupId>
37                         <artifactId>jmxtools</artifactId>
38                     </exclusion>
39                     <exclusion>
40                         <groupId>com.sun.jmx</groupId>
41                         <artifactId>jmxri</artifactId>
42                     </exclusion>
43                     <exclusion>
44                         <artifactId>jms</artifactId>
45                         <groupId>javax.jms</groupId>
46                     </exclusion>
47                     <exclusion>
48                         <artifactId>mail</artifactId>
49                         <groupId>javax.mail</groupId>
50                     </exclusion>
51                 </exclusions>
52             </dependency>
53             <dependency>
54                 <groupId>org.springframework</groupId>
55                 <artifactId>spring</artifactId>
56                 <version>2.5.6.SEC03</version>
57             </dependency>
58             <dependency>
59                 <groupId>org.slf4j</groupId>
60                 <artifactId>slf4j-api</artifactId>
61                 <version>1.7.6</version>
62             </dependency>
63             <dependency>
64                 <groupId>org.slf4j</groupId>
65                 <artifactId>slf4j-log4j12</artifactId>
66                 <version>1.6.1</version>
67             </dependency>
68             <dependency>
69                 <groupId>org.apache.zookeeper</groupId>
70                 <artifactId>zookeeper</artifactId>
71                 <version>3.4.6</version>
72             </dependency>
73             <dependency>
74                 <groupId>com.alibaba.zy</groupId>
75                 <artifactId>dubbo-service</artifactId>
76                 <version>0.0.1-SNAPSHOT</version>
77             </dependency>
78     </dependencies>
79 </project>
pom.xml

启动顺序:先启动-service 提供者  在启动消费者-consumer

Zookeeper配置

tickTime=2000
initLimit=10
syncLimit=5
dataDir=E:\zookeeper-3.4.6\data     选填自己存放路径
clientPort=2181
server.1=localhost:2287:3387

 项目地址链接: http://pan.baidu.com/s/1jIMyZsy 密码: 3h47  

 

posted @ 2016-07-06 11:31  LuckKing  阅读(334)  评论(0编辑  收藏  举报