码迷,mamicode.com
首页 > 其他好文 > 详细

分布式框架Dubbo配置和实例

时间:2017-10-20 20:02:46      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:工作   依赖关系   计算   lease   对话   完成   jdk   pac   官网   

 

 

 

 

准备工作:

1、ZooKeeper:需要去Apache Zookeeper官网下载Zookeeper.tar.gz包,Dubbo是依赖于Zookeeper的

2、Maven:需要去Apache Maven官网下载Maven-xxx.bin.zip包,主要是为了打包war挂在Tomcat下

3、Tomcat:当做运行的服务器

4、Dubbo:http://dubbo.io/ 需要下载dubbo.zip到本地

5、Ecplise,IDE供写代码使用(不要忘记安装和配置JDK)

废话不说,直接开始

一、创建项目提供者

打开Ecplise----File-----New------Project,选择Maven Project,然后Next

技术分享

技术分享

这里只是为了演示,Group Id和Artifact Id名称你可以随意起

然后等待项目创建完成

创建完成之后修改项目下的pom.xml文件

 1 <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/xsd/maven-4.0.0.xsd">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>com.luo</groupId>
 4   <artifactId>provider</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   <properties>
 7       <spring.version>3.2.8.RELEASE</spring.version>
 8   </properties>
 9   
10   <dependencies>
11       <dependency>
12           <groupId>com.alibaba</groupId>
13           <artifactId>dubbo</artifactId>
14           <version>2.5.3</version>
15           <exclusions>
16               <exclusion>
17                   <groupId>org.springframework</groupId>
18                   <artifactId>spring</artifactId>
19               </exclusion>
20           </exclusions>
21       </dependency>
22       <dependency>  
23           <groupId>com.github.sgroschupf</groupId>  
24           <artifactId>zkclient</artifactId>  
25           <version>0.1</version>  
26       </dependency>
27       <!-- spring相关 -->  
28       <dependency>  
29           <groupId>org.springframework</groupId>  
30           <artifactId>spring-core</artifactId>  
31           <version>${spring.version}</version>  
32       </dependency>  
33       <dependency>  
34           <groupId>org.springframework</groupId>  
35           <artifactId>spring-beans</artifactId>  
36           <version>${spring.version}</version>  
37       </dependency>  
38       <dependency>  
39           <groupId>org.springframework</groupId>  
40           <artifactId>spring-context</artifactId>  
41           <version>${spring.version}</version>  
42       </dependency>  
43       <dependency>  
44           <groupId>org.springframework</groupId>  
45           <artifactId>spring-jdbc</artifactId>  
46           <version>${spring.version}</version>  
47       </dependency>  
48       <dependency>  
49           <groupId>org.springframework</groupId>  
50           <artifactId>spring-web</artifactId>  
51           <version>${spring.version}</version>  
52       </dependency>  
53       <dependency>  
54           <groupId>org.springframework</groupId>  
55           <artifactId>spring-webmvc</artifactId>  
56           <version>${spring.version}</version>  
57       </dependency>  
58       <dependency>  
59           <groupId>org.springframework</groupId>  
60           <artifactId>spring-aop</artifactId>  
61           <version>${spring.version}</version>  
62       </dependency>  
63       <dependency>  
64           <groupId>org.springframework</groupId>  
65           <artifactId>spring-tx</artifactId>  
66           <version>${spring.version}</version>  
67       </dependency>  
68       <dependency>  
69           <groupId>org.springframework</groupId>  
70           <artifactId>spring-orm</artifactId>  
71           <version>${spring.version}</version>  
72       </dependency>  
73       <dependency>  
74           <groupId>org.springframework</groupId>  
75           <artifactId>spring-context-support</artifactId>  
76           <version>${spring.version}</version>  
77       </dependency>  
78       <dependency>  
79           <groupId>org.springframework</groupId>  
80           <artifactId>spring-test</artifactId>  
81           <version>${spring.version}</version>  
82       </dependency>  
83       <dependency>  
84           <groupId>org.springframework</groupId>  
85           <artifactId>spring-jms</artifactId>  
86           <version>${spring.version}</version>  
87       </dependency>  
88   </dependencies>
89 </project>

然后在项目中添加文件

技术分享

ITestService.java和TestService.java

1 package com.song.Iservice;
2 
3 public interface ITestService {
4     public String getName();
5 }
 1 package com.song.service;
 2 import com.song.Iservice.*;
 3 public class TestService implements ITestService  {
 4 
 5     public String getName() {
 6         // TODO Auto-generated method stub
 7         return "song";
 8     }
 9 
10 }
 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     <!--具体实现的bean-->
10     <bean id="testService" class="com.luo.service.impl.TestServiceImpl" />
11     <!--提供方应用信息,用于计算依赖关系-->
12     <dubbo:application name="provider" />
13     <!--使用Zookeeper注册中心暴露服务地址-->
14     <dubbo:registry address="zookeeper://127.0.0.1:2181" />
15     <!--用dubbo协议在29014端口暴露服务-->
16     <dubbo:protocol name="dubbo" port="29014" />
17     <!--声明需要暴露的服务接口-->
18     <dubbo:service interface="com.luo.service.TestService" ref="testService" />
19 </beans>

上面是application.xml

下面是Test.java

 1 package com.song.test;
 2 
 3 import java.io.IOException;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Test {
 7     public static void main(String[] args) {
 8         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"application.xml"});
 9         context.start();
10         System.out.println("Provider register Success");
11         try {
12             System.in.read();//让此程序一直跑,表示一直提供服务
13         }
14         catch(IOException e) {
15             e.printStackTrace();
16         }
17     }
18 }

上面提供者的程序已经写完了

接下来是配置了

1、Zookeeper配置:打开Zookeeper所在文件的bin目录运行zkServer.cmd(别忘了配置conf里面的cfg文件并且重命名为zoo.cfg)

2、打开dubbo所在文件dubbo-admin目录,在此目录中打开cmd 然后输入

mvn package -Dmaven.skip.test=true

如果成功会生成一个target文件夹,里面有个war文件,把这个文件拷贝到Tomcat下的WebApps目录下

3、运行Tomcat会生成一个和war同名称的文件夹,打开这个文件夹,下面的WEB-INF子文件夹里面有个dubbo.properties的文件

技术分享

技术分享

可以看到下面的内容,这就是网页登录的账号和密码

1 dubbo.registry.address=zookeeper://127.0.0.1:2181
2 dubbo.admin.root.password=root
3 dubbo.admin.guest.password=guest

在Tomcat中输入访问的文件夹名称就会弹出登录对话框,输入完用户和密码后就会进入到Dubbo-admin的主界面

技术分享

 

 

 3、启动Ecplise的Test.java---Run as Java Application,可以看到logcat输出“Provider register Success”

 输入:com.song.service可以得到如下界面

技术分享

下面我们来写Consumer的代码

 和前面一样建一个Maven项目,命名为consumer

1、修改pom.xml

 1 <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/xsd/maven-4.0.0.xsd">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>com.song</groupId>
 4   <artifactId>consumer</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   <properties>  
 7       <spring.version>3.2.8.RELEASE</spring.version>  
 8   </properties>  
 9 
10   <dependencies>  
11       <!-- 添加provider的jar包 -->  
12       <dependency>  
13           <groupId>com.song</groupId>  
14           <artifactId>provider</artifactId>  
15           <version>0.0.1-SNAPSHOT</version>  
16       </dependency>  
17       <!-- 添加dubbo依赖 -->  
18       <dependency>  
19           <groupId>com.alibaba</groupId>  
20           <artifactId>dubbo</artifactId>  
21           <version>2.5.3</version>  
22           <exclusions>  
23               <exclusion>  
24                   <groupId>org.springframework</groupId>  
25                   <artifactId>spring</artifactId>  
26               </exclusion>  
27           </exclusions>  
28       </dependency>  
29       <!-- 添加zk客户端依赖 -->  
30       <dependency>  
31           <groupId>com.github.sgroschupf</groupId>  
32           <artifactId>zkclient</artifactId>  
33           <version>0.1</version>  
34       </dependency>  
35       <!-- spring相关 -->  
36       <dependency>  
37           <groupId>org.springframework</groupId>  
38           <artifactId>spring-core</artifactId>  
39           <version>${spring.version}</version>  
40       </dependency>  
41       <dependency>  
42           <groupId>org.springframework</groupId>  
43           <artifactId>spring-beans</artifactId>  
44           <version>${spring.version}</version>  
45       </dependency>  
46       <dependency>  
47           <groupId>org.springframework</groupId>  
48           <artifactId>spring-context</artifactId>  
49           <version>${spring.version}</version>  
50       </dependency>  
51       <dependency>  
52           <groupId>org.springframework</groupId>  
53           <artifactId>spring-jdbc</artifactId>  
54           <version>${spring.version}</version>  
55       </dependency>  
56       <dependency>  
57           <groupId>org.springframework</groupId>  
58           <artifactId>spring-web</artifactId>  
59           <version>${spring.version}</version>  
60       </dependency>  
61       <dependency>  
62           <groupId>org.springframework</groupId>  
63           <artifactId>spring-webmvc</artifactId>  
64           <version>${spring.version}</version>  
65       </dependency>  
66       <dependency>  
67           <groupId>org.springframework</groupId>  
68           <artifactId>spring-aop</artifactId>  
69           <version>${spring.version}</version>  
70       </dependency>  
71       <dependency>  
72           <groupId>org.springframework</groupId>  
73           <artifactId>spring-tx</artifactId>  
74           <version>${spring.version}</version>  
75       </dependency>  
76       <dependency>  
77           <groupId>org.springframework</groupId>  
78           <artifactId>spring-orm</artifactId>  
79           <version>${spring.version}</version>  
80       </dependency>  
81       <dependency>  
82           <groupId>org.springframework</groupId>  
83           <artifactId>spring-context-support</artifactId>  
84           <version>${spring.version}</version>  
85       </dependency>  
86       <dependency>  
87           <groupId>org.springframework</groupId>  
88           <artifactId>spring-test</artifactId>  
89           <version>${spring.version}</version>  
90       </dependency>  
91       <dependency>  
92           <groupId>org.springframework</groupId>  
93           <artifactId>spring-jms</artifactId>  
94           <version>${spring.version}</version>  
95       </dependency>  
96   </dependencies>  
97 </project>

其实就是添加了前一个项目的依赖

1 <!-- 添加provider的jar包 -->  
2       <dependency>  
3           <groupId>com.song</groupId>  
4           <artifactId>provider</artifactId>  
5           <version>0.0.1-SNAPSHOT</version>  
6       </dependency>  

技术分享

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

consumerTest.java

 1 package consumer;
 2 
 3 import java.io.IOException;
 4 
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.luo.service.TestService;
 8 
 9 public class consumerTest {
10     public static void main(String[] args) {
11         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
12                 new String[] { "application.xml" });
13         context.start();
14         TestService testService = (TestService) context.getBean("testService");
15         System.out.println(testService.getName());
16         try {
17             System.in.read();
18         } catch (IOException e) {       
19             e.printStackTrace();
20         }  
21 
22     }
23 }

然后启动项目,会在消费者那里看到有一个IP

 

分布式框架Dubbo配置和实例

标签:工作   依赖关系   计算   lease   对话   完成   jdk   pac   官网   

原文地址:http://www.cnblogs.com/wangyulong/p/7700730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!