Dubbo(三)使用示例

创建项目

在 dubbo-learn 父工程下,创建interface模块、provider模块、consumer模块。其中interface模块存放所有的接口、provider模块提供服务、consumer消费服务。本示例使用XML配置。创建完成后的项目结构如下:

在父工程的 pom.xml 文件中添加需要的依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<properties>
<spring.version>4.3.9.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- 日志 start-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<!-- 日志 end-->

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</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>

provider模块和consumer模块的resources目录里加入log4j.properties配置文件:

1
2
3
4
5
6
7
8
9
10
11
log4j.rootCategory=info, stdout , E
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.appender.E=org.apache.log4j.RollingFileAppender
log4j.appender.E.File=${TOMCAT_HONE}/logs/duboo-learn.log
log4j.appender.E.MaxFileSize=10MB
log4j.appender.E.MaxBackupIndex=100
log4j.appender.E.ImmediateFlush=true
log4j.appender.E.layout=org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

interface模块

在interface模块中创建需要的接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.lzumetal.dubbolearn.demo.service;

/**
* Created by liaosi on 2017/9/24.
*/
public interface UserService {

/**
* 登录
* @param username
* @param password
* @return 登录是否成功
*/
public boolean login(String username, String password);
}

interface 模块即完毕。

provider 模块

在provider模块中要引入interface模块的依赖,consumer模块同样也要引入:

1
2
3
4
5
<dependency>
<groupId>com.lzumetal</groupId>
<artifactId>demo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

创建UserService接口的实现类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.lzumetal.dubbolearn.demo.service.impl;

import com.lzumetal.dubbolearn.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


/**
* Created by liaosi on 2017/9/24.
*/
@Service("userService") //自动注入到ioc容器中,并且bean的id是userService
public class UserServiceImpl implements UserService {

private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);


@Override
public boolean login(String username, String password) {
log.error("用户登录:[username:{}, password:{}]", username, password);
if (username != null && password != null && username.equals(password)) {
log.error("用户校验通过。[username:{}]", username);
return true;
}
log.error("用户校验失败![username:{}]", username);
return false;
}

}

创建provider模块的dubbo配置文件dubbo-provider.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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="dubbodemo-provider"/>

<!--连接到那个注册中心-->
<dubbo:registry id="zk-register" protocol="zookeeper" address="zookeeper://192.168.128.1:2181"/>

<!--用dubbo协议在20880端口暴露服务-->
<dubbo:protocol id="dobbo-protocol" name="dubbo" port="20880"/>

<!--声明需要暴露的服务接口-->
<!-- userService 是已经加入spring容器的bean(接口的具体实现类) -->
<dubbo:service interface="com.lzumetal.dubbolearn.demo.service.UserService" ref="userService" timeout="3000"
version="1.0"/>
<!-- 定义了version="1.0",则消费者也必须要消费配置消费version="1.0"的接口 -->

</beans>

创建provider模块的spring配置文件Provider-AplicationContext.xml,需要配置注解的包扫描,并且引入dubbo-provider.xm配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
<?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"
xsi:schemaLocation="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">

<context:component-scan base-package="com.lzumetal.dubbolearn.demo.service.impl"/>

<import resource="Dubbo-Provider.xml"/>

</beans>

至此,provider模块的配置基本完成,再写一个启动类将provider启动起来提供服务。

创建一个类SimpleLauncher.java,用于加载Spring:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.lzumetal.dubbolearn.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class SimpleLauncher {

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("Provider-AplicationContext.xml");
applicationContext.start();
System.in.read(); //按任意键退出
}
}

到这里provider模块配置结束。启动zookeeper,然后运行Launcher类中的main()方法启动provider的服务。观察控制的打印信息,看是否有报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"D:\Program Files\Java\jdk1.8.0_144\bin\java" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\lib\idea_rt.jar=11161:D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;D:\IDEA Workspace\dubbo-learn\demo-provider\target\classes;D:\IDEA Workspace\dubbo-learn\demo-interface\target\classes;D:\Software\Maven\RepMaven\org\springframework\spring-beans\4.3.9.RELEASE\spring-beans-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-context\4.3.9.RELEASE\spring-context-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-aop\4.3.9.RELEASE\spring-aop-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-expression\4.3.9.RELEASE\spring-expression-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-context-support\4.3.9.RELEASE\spring-context-support-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-core\4.3.9.RELEASE\spring-core-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\Software\Maven\RepMaven\org\slf4j\slf4j-log4j12\1.7.12\slf4j-log4j12-1.7.12.jar;D:\Software\Maven\RepMaven\org\slf4j\slf4j-api\1.7.12\slf4j-api-1.7.12.jar;D:\Software\Maven\RepMaven\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\Software\Maven\RepMaven\com\google\code\gson\gson\2.8.1\gson-2.8.1.jar;D:\Software\Maven\RepMaven\com\alibaba\dubbo\2.5.5\dubbo-2.5.5.jar;D:\Software\Maven\RepMaven\org\springframework\spring-web\4.3.10.RELEASE\spring-web-4.3.10.RELEASE.jar;D:\Software\Maven\RepMaven\org\javassist\javassist\3.20.0-GA\javassist-3.20.0-GA.jar;D:\Software\Maven\RepMaven\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;D:\Software\Maven\RepMaven\org\apache\zookeeper\zookeeper\3.4.10\zookeeper-3.4.10.jar;D:\Software\Maven\RepMaven\jline\jline\0.9.94\jline-0.9.94.jar;D:\Software\Maven\RepMaven\io\netty\netty\3.10.5.Final\netty-3.10.5.Final.jar;D:\Software\Maven\RepMaven\com\101tec\zkclient\0.10\zkclient-0.10.jar" com.lzumetal.dubbolearn.demo.SimpleLauncher
17:24:10,372 INFO main support.ClassPathXmlApplicationContext:583 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@368102c8: startup date [Sat Jul 28 17:24:10 CST 2018]; root of context hierarchy
17:24:10,455 INFO main xml.XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [Provider-AplicationContext.xml]
17:24:10,704 INFO main xml.XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [Dubbo-Provider.xml]
17:24:10,763 INFO main logger.LoggerFactory:? - using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter
17:24:11,528 INFO main config.AbstractConfig:107 - [DUBBO] The service ready on spring started. service: com.lzumetal.dubbolearn.demo.service.UserService, dubbo version: 2.5.5, current host: 127.0.0.1
17:24:11,745 INFO main config.AbstractConfig:574 - [DUBBO] Export dubbo service com.lzumetal.dubbolearn.demo.service.UserService to local registry, dubbo version: 2.5.5, current host: 127.0.0.1
17:24:11,746 INFO main config.AbstractConfig:535 - [DUBBO] Export dubbo service com.lzumetal.dubbolearn.demo.service.UserService to url dubbo://192.168.163.1:20880/com.lzumetal.dubbolearn.demo.service.UserService?anyhost=true&application=dubbodemo-provider&dubbo=2.5.5&generic=false&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=10496&revision=1.0&side=provider&timeout=3000&timestamp=1532769851570&version=1.0, dubbo version: 2.5.5, current host: 127.0.0.1
17:24:11,746 INFO main config.AbstractConfig:546 - [DUBBO] Register dubbo service com.lzumetal.dubbolearn.demo.service.UserService url dubbo://192.168.163.1:20880/com.lzumetal.dubbolearn.demo.service.UserService?anyhost=true&application=dubbodemo-provider&dubbo=2.5.5&generic=false&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=10496&revision=1.0&side=provider&timeout=3000&timestamp=1532769851570&version=1.0 to registry registry://192.168.128.1:2181/com.alibaba.dubbo.registry.RegistryService?application=dubbodemo-provider&dubbo=2.5.5&pid=10496&registry=zookeeper&timestamp=1532769851543, dubbo version: 2.5.5, current host: 127.0.0.1
17:24:12,269 INFO main transport.AbstractServer:64 - [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.163.1:20880, dubbo version: 2.5.5, current host: 127.0.0.1
17:24:12,291 INFO main zookeeper.ZookeeperRegistry:223 - [DUBBO] Load registry store file C:\Users\liaosi\.dubbo\dubbo-registry-192.168.128.1.cache, data: {com.lzumetal.dubbolearn.demo.service.UserService:1.0=empty://192.168.163.1/com.lzumetal.dubbolearn.demo.service.UserService?application=dubbodemo-consumer&category=configurators&check=false&dubbo=2.5.5&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=10732&revision=1.0&side=consumer&timeout=60000&timestamp=1532769684512&version=1.0 empty://192.168.163.1/com.lzumetal.dubbolearn.demo.service.UserService?application=dubbodemo-consumer&category=routers&check=false&dubbo=2.5.5&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=10732&revision=1.0&side=consumer&timeout=60000&timestamp=1532769684512&version=1.0 dubbo://192.168.163.1:20880/com.lzumetal.dubbolearn.demo.service.UserService?anyhost=true&application=dubbodemo-provider&dubbo=2.5.5&generic=false&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=12028&revision=1.0&side=provider&timeout=3000&timestamp=1532769675556&version=1.0, com.lzumetal.dubbolearn.demo.service.UserService=empty://192.168.163.1/com.lzumetal.dubbolearn.demo.service.UserService?application=dubbodemo-consumer&category=configurators&dubbo=2.5.5&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=9076&side=consumer&timestamp=1532764431975 empty://192.168.163.1/com.lzumetal.dubbolearn.demo.service.UserService?application=dubbodemo-consumer&category=routers&dubbo=2.5.5&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=9076&side=consumer&timestamp=1532764431975 dubbo://192.168.163.1:20880/com.lzumetal.dubbolearn.demo.service.UserService?anyhost=true&application=dubbodemo-provider&dubbo=2.5.5&generic=false&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=12144&side=provider&timeout=3000&timestamp=1532763053853}, dubbo version: 2.5.5, current host: 127.0.0.1
17:24:12,310 INFO ZkClient-EventThread-16-192.168.128.1:2181 zkclient.ZkEventThread:65 - Starting ZkClient event thread.
17:24:12,319 INFO main zookeeper.ZooKeeper:100 - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
17:24:12,319 INFO main zookeeper.ZooKeeper:100 - Client environment:host.name=liaosi-PC
17:24:12,319 INFO main zookeeper.ZooKeeper:100 - Client environment:java.version=1.8.0_144
17:24:12,319 INFO main zookeeper.ZooKeeper:100 - Client environment:java.vendor=Oracle Corporation
17:24:12,320 INFO main zookeeper.ZooKeeper:100 - Client environment:java.home=D:\Program Files\Java\jdk1.8.0_144\jre
17:24:12,320 INFO main zookeeper.ZooKeeper:100 - Client environment:java.class.path=D:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;D:\IDEA Workspace\dubbo-learn\demo-provider\target\classes;D:\IDEA Workspace\dubbo-learn\demo-interface\target\classes;D:\Software\Maven\RepMaven\org\springframework\spring-beans\4.3.9.RELEASE\spring-beans-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-context\4.3.9.RELEASE\spring-context-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-aop\4.3.9.RELEASE\spring-aop-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-expression\4.3.9.RELEASE\spring-expression-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-context-support\4.3.9.RELEASE\spring-context-support-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\org\springframework\spring-core\4.3.9.RELEASE\spring-core-4.3.9.RELEASE.jar;D:\Software\Maven\RepMaven\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\Software\Maven\RepMaven\org\slf4j\slf4j-log4j12\1.7.12\slf4j-log4j12-1.7.12.jar;D:\Software\Maven\RepMaven\org\slf4j\slf4j-api\1.7.12\slf4j-api-1.7.12.jar;D:\Software\Maven\RepMaven\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\Software\Maven\RepMaven\com\google\code\gson\gson\2.8.1\gson-2.8.1.jar;D:\Software\Maven\RepMaven\com\alibaba\dubbo\2.5.5\dubbo-2.5.5.jar;D:\Software\Maven\RepMaven\org\springframework\spring-web\4.3.10.RELEASE\spring-web-4.3.10.RELEASE.jar;D:\Software\Maven\RepMaven\org\javassist\javassist\3.20.0-GA\javassist-3.20.0-GA.jar;D:\Software\Maven\RepMaven\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;D:\Software\Maven\RepMaven\org\apache\zookeeper\zookeeper\3.4.10\zookeeper-3.4.10.jar;D:\Software\Maven\RepMaven\jline\jline\0.9.94\jline-0.9.94.jar;D:\Software\Maven\RepMaven\io\netty\netty\3.10.5.Final\netty-3.10.5.Final.jar;D:\Software\Maven\RepMaven\com\101tec\zkclient\0.10\zkclient-0.10.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\lib\idea_rt.jar
17:24:12,321 INFO main zookeeper.ZooKeeper:100 - Client environment:java.library.path=D:\Program Files\Java\jdk1.8.0_144\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\oracle\liaosi\product\11.2.0\client_1\bin;D:\oracle\liaosi\product\11.2.0\dbhome_1\bin;D:\Software\hadoop-2.8.2\bin;D:\Program Files\Git\bin;D:\Program Files\Java\jdk1.8.0_144\bin;D:\Software\Maven\apache-maven-3.3.9\bin;c:\gtk\bin;C:\Program Files\2345Soft\HaoZip;C:\Program Files (x86)\Google\Chrome\Application;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\Program Files\Subversion\bin;D:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Windows7Master;C:\Program Files\nodejs\;D:\Software\hugo_0.40.1_Windows-64bit\;D:\Program Files\Python\Python36\Scripts\;D:\Program Files\Python\Python36\;C:\Users\liaosi\AppData\Roaming\npm;.
17:24:12,322 INFO main zookeeper.ZooKeeper:100 - Client environment:java.io.tmpdir=C:\Users\liaosi\AppData\Local\Temp\
17:24:12,322 INFO main zookeeper.ZooKeeper:100 - Client environment:java.compiler=<NA>
17:24:12,322 INFO main zookeeper.ZooKeeper:100 - Client environment:os.name=Windows 7
17:24:12,322 INFO main zookeeper.ZooKeeper:100 - Client environment:os.arch=amd64
17:24:12,322 INFO main zookeeper.ZooKeeper:100 - Client environment:os.version=6.1
17:24:12,322 INFO main zookeeper.ZooKeeper:100 - Client environment:user.name=liaosi
17:24:12,323 INFO main zookeeper.ZooKeeper:100 - Client environment:user.home=C:\Users\liaosi
17:24:12,323 INFO main zookeeper.ZooKeeper:100 - Client environment:user.dir=D:\IDEA Workspace\dubbo-learn
17:24:12,324 INFO main zookeeper.ZooKeeper:438 - Initiating client connection, connectString=192.168.128.1:2181 sessionTimeout=30000 watcher=org.I0Itec.zkclient.ZkClient@3b0fe47a
17:24:12,337 INFO main zkclient.ZkClient:936 - Waiting for keeper state SyncConnected
17:24:12,341 INFO main-SendThread(192.168.128.1:2181) zookeeper.ClientCnxn:1032 - Opening socket connection to server 192.168.128.1/192.168.128.1:2181. Will not attempt to authenticate using SASL (unknown error)
17:24:12,342 INFO main-SendThread(192.168.128.1:2181) zookeeper.ClientCnxn:876 - Socket connection established to 192.168.128.1/192.168.128.1:2181, initiating session
17:24:12,358 INFO main-SendThread(192.168.128.1:2181) zookeeper.ClientCnxn:1299 - Session establishment complete on server 192.168.128.1/192.168.128.1:2181, sessionid = 0x10000017ad50024, negotiated timeout = 30000
17:24:12,361 INFO main-EventThread zkclient.ZkClient:713 - zookeeper state changed (SyncConnected)
17:24:12,363 INFO main zookeeper.ZookeeperRegistry:293 - [DUBBO] Register: dubbo://192.168.163.1:20880/com.lzumetal.dubbolearn.demo.service.UserService?anyhost=true&application=dubbodemo-provider&dubbo=2.5.5&generic=false&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=10496&revision=1.0&side=provider&timeout=3000&timestamp=1532769851570&version=1.0, dubbo version: 2.5.5, current host: 127.0.0.1
17:24:12,406 INFO main zookeeper.ZookeeperRegistry:316 - [DUBBO] Subscribe: provider://192.168.163.1:20880/com.lzumetal.dubbolearn.demo.service.UserService?anyhost=true&application=dubbodemo-provider&category=configurators&check=false&dubbo=2.5.5&generic=false&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=10496&revision=1.0&side=provider&timeout=3000&timestamp=1532769851570&version=1.0, dubbo version: 2.5.5, current host: 127.0.0.1
17:24:12,462 INFO main zookeeper.ZookeeperRegistry:404 - [DUBBO] Notify urls for subscribe url provider://192.168.163.1:20880/com.lzumetal.dubbolearn.demo.service.UserService?anyhost=true&application=dubbodemo-provider&category=configurators&check=false&dubbo=2.5.5&generic=false&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=10496&revision=1.0&side=provider&timeout=3000&timestamp=1532769851570&version=1.0, urls: [empty://192.168.163.1:20880/com.lzumetal.dubbolearn.demo.service.UserService?anyhost=true&application=dubbodemo-provider&category=configurators&check=false&dubbo=2.5.5&generic=false&interface=com.lzumetal.dubbolearn.demo.service.UserService&methods=login&pid=10496&revision=1.0&side=provider&timeout=3000&timestamp=1532769851570&version=1.0], dubbo version: 2.5.5, current host: 127.0.0.1

启动成功后,在zookeeper上可以看到/dubbo目录下就有我们暴露出去的所有接口信息。

consumer模块

在consumer模块中配置dubbo的配置文件Dubbo-Consumer.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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="dubbodemo-consumer"/>

<!--连接到哪个注册中心(这里是连接到本机2181端口的zookeeper)-->
<dubbo:registry address="zookeeper://192.168.128.1:2181"/>

<!--生成远程代理服务,调用接口的方法-->
<dubbo:reference id="userService" interface="com.lzumetal.dubbolearn.demo.service.UserService" timeout="60000"
version="1.0" check="false"/>


</beans>

配置consumer模块中的spring配置文件Consumer-ApplicationContext.xml

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<import resource="Dubbo-Consumer.xml"/>

</beans>

创建服务消费的类UserServiceConsumer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.lzumetal.dubbolearn.demo.consumer;

import com.lzumetal.dubbolearn.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Created by liaosi on 2017/9/24.
*/
public class UserServiceConsumer {

private static final Logger log = LoggerFactory.getLogger(UserServiceConsumer.class);

public static void main(String args[]) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Consumer-ApplicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
log.info("执行结果:" + userService.login("hello", "hello"));
}

}

到这里 consumer 模块配置基本结束。我们运行UserServiceConsumermain()方法调用 provider服务(前提是provider服务要开启),成功之后会打印出是否调用成功。

如果如上图这样就表示成功调用了。
可以看到 DUBBO 框架是在 Spring 的基础上加上一个简单的配置文件即可把一个服务暴露出去。
DUBBO 配置文件基本有 application、registry、protocol 3个公共参数分别告诉了 DUBBO 以及 zookeeper 注册中心:我是谁?我向谁注册?怎么调用我的服务? 通过这 3 个配置,其他消费者就可以找到对应服务。


本文示例代码已上传至个人Github,链接:https://github.com/liaosilzu2007/dubbo-learn.git

------ 本文完 ------