目测很多公司,特别是互联网公司,更特别的是电商公司,很多都在使用dubbo作为服务框架,在网上找了例子,看看dubbo的简单使用。
看起来调用的方式和rmi和web service是类似的,具体深入点的区别还不是很了解,另外一点,网上很多教程都是基于maven的,看样子maven的使用真的是越来越广泛了,不使用maven,很多例子都难以下手,至于maven就不赘述了。
这个例子就是基于zookeeper + maven + dubbo + spring的例子
首先看一下运行结果:
启动zookeeper:
接着启动dubbo的服务端:
最后启动dubbo的客户端,调用服务端的服务:
客户端日志:
接收客户端请求后,返回响应时服务端的日志:
整个的调用过程就是如此~!
下面看一下例子的搭建过程:
首先是zookeeper的搭建
这个我也是刚接触,可以百度相关教程,总体来说,简单的配置和启动不难。
下载zookeeper,解压到某路径下,如图:
文件夹下的conf子文件夹是配置文件所在路径,
主要是将zoo_sample.cfg复制一份修改为zoo.cfg,然后修改其中的配置:
然后直接在子文件夹bin中双击启动
启动完毕
服务端搭建
服务端截图:
代码如下:
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/maven-v4_0_0.xsd">
5
6 <modelVersion>4.0.0</modelVersion>
7
8 <groupId>com.mor.maven</groupId>
9 <artifactId>dubboserver</artifactId>
10 <version>0.0.1</version>
11 <packaging>jar</packaging>
12
13 <name>dubboserver</name>
14 <url>http://maven.apache.org</url>
15
16 <properties>
17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18 <spring.version>3.2.13.RELEASE</spring.version>
19 <slf4j.version>1.6.6</slf4j.version>
20 </properties>
21
22 <dependencies>
23 <dependency>
24 <groupId>junit</groupId>
25 <artifactId>junit</artifactId>
26 <version>3.8.1</version>
27 <scope>test</scope>
28 </dependency>
29 <!-- Spring -->
30 <dependency>
31 <groupId>org.springframework</groupId>
32 <artifactId>spring-aop</artifactId>
33 <version>${spring.version}</version>
34 </dependency>
35 <dependency>
36 <groupId>org.springframework</groupId>
37 <artifactId>spring-core</artifactId>
38 <version>${spring.version}</version>
39 </dependency>
40 <dependency>
41 <groupId>org.springframework</groupId>
42 <artifactId>spring-beans</artifactId>
43 <version>${spring.version}</version>
44 </dependency>
45 <dependency>
46 <groupId>org.springframework</groupId>
47 <artifactId>spring-context</artifactId>
48 <version>${spring.version}</version>
49 </dependency>
50 <dependency>
51 <groupId>org.springframework</groupId>
52 <artifactId>spring-expression</artifactId>
53 <version>${spring.version}</version>
54 </dependency>
55 <!-- spring end -->
56 <!-- log -->
57 <dependency>
58 <groupId>log4j</groupId>
59 <artifactId>log4j</artifactId>
60 <version>1.2.16</version>
61 </dependency>
62 <dependency>
63 <groupId>org.slf4j</groupId>
64 <artifactId>slf4j-api</artifactId>
65 <version>${slf4j.version}</version>
66 </dependency>
67 <dependency>
68 <groupId>org.slf4j</groupId>
69 <artifactId>slf4j-log4j12</artifactId>
70 <version>${slf4j.version}</version>
71 </dependency>
72 <!-- dubbo -->
73 <dependency>
74 <groupId>com.alibaba</groupId>
75 <artifactId>dubbo</artifactId>
76 <version>2.5.3</version>
77 </dependency>
78 <!-- zkclient -->
79 <dependency>
80 <groupId>com.github.sgroschupf</groupId>
81 <artifactId>zkclient</artifactId>
82 <version>0.1</version>
83 </dependency>
84 <!-- zookeeper -->
85 <dependency>
86 <groupId>org.apache.zookeeper</groupId>
87 <artifactId>zookeeper</artifactId>
88 <version>3.4.5</version>
89 </dependency>
90 </dependencies>
91
92 <build>
93 <finalName>dubbo-demo</finalName>
94 <plugins>
95 <plugin>
96 <groupId>org.apache.maven.plugins</groupId>
97 <artifactId>maven-compiler-plugin</artifactId>
98 <version>2.1</version>
99 <configuration>
100 <source>1.5</source>
101 <target>1.5</target>
102 <encoding>UTF-8</encoding>
103 <failOnError>false</failOnError>
104 </configuration>
105 </plugin>
106 </plugins>
107 </build>
108 </project>
applicationProvider.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:application name="hello-world-app" />
<!-- 本机 伪集群 测试 -->
<dubbo:registry protocol="zookeeper" address="192.168.10.171:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.mor.server.dubbo.service.DemoServer" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.mor.server.dubbo.service.DemoServerImpl" />
</beans>
log4j.properties
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n
log4j.rootLogger=INFO,Stdout
log4j.logger.org.apache.wicket=INFO
log4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFO
log4j.logger.org.apache.wicket.version=INFO
log4j.logger.org.apache.wicket.RequestCycle=INFO
ServerMain.java
package com.mor.main;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServerMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationProvider.xml" });
context.start();
System.out.println("按任意键退出");
System.in.read();
}
}
DemoServer.java
package com.mor.server.dubbo.service;
public interface DemoServer {
String sayHello(String str);
}
DemoServerImpl.java
package com.mor.server.dubbo.service;
import java.util.Date;
public class DemoServerImpl implements DemoServer {
public String sayHello(String str) {
System.out.println("服务端-2-:接收到客户端请求为:[" + str + "]");
String respStr = "你好 ,当前时间为:" + new Date();
System.out.println("服务端-3-:返回客户端信息:[" + respStr + "]");
return respStr;
}
}
客户端搭建
工程截图:
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/maven-v4_0_0.xsd">
5
6 <modelVersion>4.0.0</modelVersion>
7 <groupId>ClientMaven</groupId>
8 <artifactId>Client</artifactId>
9 <version>0.0.1-SNAPSHOT</version>
10 <name>dubboClient</name>
11 <url>http://maven.apache.org</url>
12
13 <properties>
14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15 <spring.version>3.2.13.RELEASE</spring.version>
16 <slf4j.version>1.6.6</slf4j.version>
17 </properties>
18
19 <dependencies>
20 <dependency>
21 <groupId>junit</groupId>
22 <artifactId>junit</artifactId>
23 <version>3.8.1</version>
24 <scope>test</scope>
25 </dependency>
26 <!-- Spring -->
27 <dependency>
28 <groupId>org.springframework</groupId>
29 <artifactId>spring-aop</artifactId>
30 <version>${spring.version}</version>
31 </dependency>
32 <dependency>
33 <groupId>org.springframework</groupId>
34 <artifactId>spring-core</artifactId>
35 <version>${spring.version}</version>
36 </dependency>
37 <dependency>
38 <groupId>org.springframework</groupId>
39 <artifactId>spring-beans</artifactId>
40 <version>${spring.version}</version>
41 </dependency>
42 <dependency>
43 <groupId>org.springframework</groupId>
44 <artifactId>spring-context</artifactId>
45 <version>${spring.version}</version>
46 </dependency>
47 <dependency>
48 <groupId>org.springframework</groupId>
49 <artifactId>spring-expression</artifactId>
50 <version>${spring.version}</version>
51 </dependency>
52 <!-- spring end -->
53 <!-- log -->
54 <dependency>
55 <groupId>log4j</groupId>
56 <artifactId>log4j</artifactId>
57 <version>1.2.16</version>
58 </dependency>
59 <dependency>
60 <groupId>org.slf4j</groupId>
61 <artifactId>slf4j-api</artifactId>
62 <version>${slf4j.version}</version>
63 </dependency>
64 <dependency>
65 <groupId>org.slf4j</groupId>
66 <artifactId>slf4j-log4j12</artifactId>
67 <version>${slf4j.version}</version>
68 </dependency>
69 <!-- dubbo -->
70 <dependency>
71 <groupId>com.alibaba</groupId>
72 <artifactId>dubbo</artifactId>
73 <version>2.5.3</version>
74 </dependency>
75 <!-- zkclient -->
76 <dependency>
77 <groupId>com.github.sgroschupf</groupId>
78 <artifactId>zkclient</artifactId>
79 <version>0.1</version>
80 </dependency>
81 <!-- zookeeper -->
82 <dependency>
83 <groupId>org.apache.zookeeper</groupId>
84 <artifactId>zookeeper</artifactId>
85 <version>3.4.5</version>
86 </dependency>
87 </dependencies>
88
89 <build>
90 <finalName>dubbo-demo</finalName>
91 <plugins>
92 <plugin>
93 <groupId>org.apache.maven.plugins</groupId>
94 <artifactId>maven-compiler-plugin</artifactId>
95 <version>2.1</version>
96 <configuration>
97 <source>1.5</source>
98 <target>1.5</target>
99 <encoding>UTF-8</encoding>
100 <failOnError>false</failOnError>
101 </configuration>
102 </plugin>
103 </plugins>
104 </build>
105 </project>
106
该pom.xml和服务端的依赖基本一致
ClientMain.java
package com.mor.client.dubbo.main;
import com.mor.client.dubbo.action.ChatAction;
public class ClientMain {
public static void main(String[] args) {
ChatAction act = new ChatAction();
act.SayHello();
}
}
ChatAction.java
package com.mor.client.dubbo.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mor.server.dubbo.service.DemoServer;
public class ChatAction {
public void SayHello() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" });
context.start();
DemoServer demoServer = (DemoServer) context.getBean("demoService");
String reqStr = "我是客户端参数:[当前时间是多少?]";
System.out.println("客户端-1-:开始发起请求,请求参数为:" + reqStr);
System.out.println("客户端-4-:接收到服务端信息为["
+ demoServer.sayHello(reqStr)
+ "]");
}
}
applicationConsumer.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:application name="consumer-of-helloworld-app" />
<!-- 使用本地伪集群注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.10.171:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.mor.server.dubbo.service.DemoServer" />
</beans>
由于客户端需要调用服务端,所以可以采取工程依赖和jar包依赖两种
工程依赖:
在myeclipse里面,右击Client-->Build path-->Configure build path
然后选择Projects,选择服务端
或者把服务端打成jar包,然后客户端直接引用。
至此,全部过程就搭建完毕,运行的时候依次启动:
1.zookeeper
2.服务端
3.客户端
即可得到最开始结果。
几个注意的问题:
如果applicationProvider.xml的dubbo标签打红叉,可以自行百度myeclipse的xsd的引用
可参见:http://vickya.iteye.com/blog/1113277
注意xml配置文件中的ip和端口
加油学习~!