本文给出一个整合Maven Spring Dubbo Zookeeper的示例,并且一步步给出完成步骤,并对其中可能遇到的问题进行解决。
一、Maven模块化
创建一个名为dubbo-demo的Maven工程,该工程包含三个模块
- dubbo-demo-interface (定义对外开放接口)
- dubbo-demo-provider (接口实现)
- dubbo-demo-consumer (接口调用)
dubbo-demo工程中的pom.xml文件如下:
代码语言:javascript复制
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>dubbo-demo-provider</module>
<module>dubbo-demo-consumer</module>
<module>dubbo-demo-interface</module>
</modules>
</project>
接下来,我们就一步一步来完成interface、provider以及consumer模块的内容。
二、模块interface
2.1 定义接口
创建一个接口(GreetingService),包含一个hello接口,如:
代码语言:javascript复制
package com.xxx.dubbo.demo.service;
/**
* @author wangmengjun
*
*/
public interface GreetingService {
String hello(String name);
}
简单的接口就定义好了。
有了接口,就可以在Provider中完成接口的实现,并暴露接口服务为Dubbo服务给其它模块使用(如consumer)。
三、模块Provider
3.1 添加依赖包
- 添加interface依赖包
<dependency>
<groupId>com.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 添加spring依赖包
<!--引入Spring依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
- 添加dubbo依赖包
<!-- Dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.4-snapshot</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.6.Final</version>
</dependency>
- 添加zk-client依赖包
<!-- ZK-client -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
dubbo-demo-provider模块详细的pom.xml文件如下:
代码语言:javascript复制
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-demo-provider</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.framework.version>4.3.6.RELEASE</spring.framework.version>
</properties>
<dependencies>
<dependency>
<groupId>com.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--引入Spring依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.4-snapshot</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.6.Final</version>
</dependency>
<!-- ZK-client -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
3.2 实现接口
编写GreetingService的实现类GreetingServiceImpl, 并实现hello方法
代码语言:javascript复制
package com.xxx.dubbo.demo.service.impl;
import java.util.Date;
import com.xxx.dubbo.demo.service.GreetingService;
/**
* @author wangmengjun
*
*/
public class GreetingServiceImpl implements GreetingService {
public String hello(String name) {
System.out.println("Hello Service is calling : " new Date());
String greetMessage = "Hello, " name;
return greetMessage;
}
}
3.3 配置XML文件
- spring-dubbo-provider.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
">
<description>Dubbo Demo Service</description>
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-demo-provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"
client="zkclient" />
<!-- dubbo缓存 -->
<dubbo:protocol id="dubbo" name="dubbo" port="20880"
threadpool="cached" threads="100" />
<!-- Greeting服务 -->
<bean id="greetingService" class="com.xxx.dubbo.demo.service.impl.GreetingServiceImpl" />
<dubbo:service protocol="dubbo"
interface="com.xxx.dubbo.demo.service.GreetingService" ref="greetingService" />
</beans>
- applicationContext.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:p="http://www.springframework.org/schema/p"
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
">
<!--服务层配置 -->
<import resource="classpath:spring-dubbo-provider.xml" />
</beans>
3.4 编写启动类
编写一个启动类Main
代码语言:javascript复制
package com.xxx.dubbo.demo.main;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
context.start();
System.in.read();
}
}
其中,
System.in.read(); 的作用是控制台输入任何字符退出~ 因为本文示例provider不是web工程,这个操作只是保持provider的服务一直开着~
接下来,我们继续来完成模块Consumer的代码和配置。
四、模块Consumer
4.1 添加依赖包
- 添加interface依赖包
<dependency>
<groupId>com.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 添加spring依赖包
<!--引入Spring依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
- 添加dubbo依赖包
<!-- Dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.4-snapshot</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.6.Final</version>
</dependency>
- 添加zk-client依赖包
<!-- ZK-client -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
dubbo-demo-consumer模块详细的pom.xml文件如下:
代码语言:javascript复制
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-demo-consumer</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.framework.version>4.3.6.RELEASE</spring.framework.version>
</properties>
<dependencies>
<dependency>
<groupId>com.xxx.dubbo.demo</groupId>
<artifactId>dubbo-demo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--引入Spring依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.4-snapshot</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.6.Final</version>
</dependency>
<!-- ZK-client -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
4.2 配置XML文件
- spring-dubbo-consume.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
">
<description>Dubbo Demo Service</description>
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="DemoProvider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="172.0.0.1:2181"
client="zkclient" />
<!-- dubbo缓存 -->
<dubbo:protocol id="dubbo" name="dubbo" port="20880"
threadpool="cached" threads="100" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="greetingService"
interface="com.xxx.dubbo.demo.service.GreetingService" />
</beans>
- applicationContext.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:p="http://www.springframework.org/schema/p"
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
">
<!--服务层配置 -->
<import resource="classpath:spring-dubbo-consumer.xml" />
</beans>
4.3 编写测试类
在dubbo-demo-consumer模块下,创建一个调用interface接口的方法。
代码语言:javascript复制
package com.xxx.dubbo.demo.main;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xxx.dubbo.demo.service.GreetingService;
public class Main {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
GreetingService greetingService = (GreetingService) context.getBean("greetingService");
String greetMessage = greetingService.hello("Eric");
System.out.println("Consumer ==> " greetMessage);
context.destroy();
}
}
五、Zookeeper安装和配置
5.1 安装
可以从zookeeper官网下载zookeeper安装包~ 本文使用的版本是3.4.6, 将下载的安装包解压缩即可。
5.2 配置
进入Zookeeper安装目录下的conf目录。
复制zoo_sample.conf文件,并将复制的配置文件取名为zoo.conf。其内容如下:
代码语言:javascript复制
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
简单使用的话,可以不做修改。
至此,interface,provider以及consumer模块已经准备好,zookeeper也已经准备好~ 接下来就可以做相关的测试了~
六、测试 & 结果
6.1 启动Zookeeper
进入Zookeeper安装路径下的bin目录。
window系统中,启动zkServer.cmd,Linux中,启动zkServer.sh~
启动zookeeper服务,
6.2 启动提供者
运行dubbo-demo-provider模块中的Main类。
- 运行之后,出现如下问题:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
- 根据描述 ,解决方法很简单,配置一个log4j.properties文件在resouce下即可
log4j.properties的内容简单如下:
代码语言:javascript复制
# Rules reminder:
# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=INFO,stdout
## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH:mm:ss,SSS}] %l - %m%n
重新运行Main类,可以看出来,provider已经成功启动。
代码语言:javascript复制
[INFO ][2017-05-18 21:06:13,577] org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:582) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46309a10: startup date [Thu May 18 21:06:13 CST 2017]; root of context hierarchy
[INFO ][2017-05-18 21:06:13,626] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [applicationContext.xml]
[INFO ][2017-05-18 21:06:13,717] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [spring-dubbo-provider.xml]
[INFO ][2017-05-18 21:06:13,779] - using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter
[INFO ][2017-05-18 21:06:14,097] com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:107) - [DUBBO] The service ready on spring started. service: com.xxx.dubbo.demo.service.GreetingService, dubbo version: 2.5.4-SNAPSHOT, current host: 127.0.0.1
[INFO ][2017-05-18 21:06:14,213] com.alibaba.dubbo.config.ServiceConfig.exportLocal(ServiceConfig.java:513) - [DUBBO] Export dubbo service com.xxx.dubbo.demo.service.GreetingService to local registry, dubbo version: 2.5.4-SNAPSHOT, current host: 127.0.0.1
[INFO ][2017-05-18 21:06:14,213] com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:473) - [DUBBO] Export dubbo service com.xxx.dubbo.demo.service.GreetingService to url dubbo://192.168.102.51:20880/com.xxx.dubbo.demo.service.GreetingService?anyhost=true&application=dubbo-demo-provider&dubbo=2.5.4-SNAPSHOT&generic=false&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=18448&side=provider&threadpool=cached&threads=100×tamp=1495112774127, dubbo version: 2.5.4-SNAPSHOT, current host: 127.0.0.1
[INFO ][2017-05-18 21:06:14,214] com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:484) - [DUBBO] Register dubbo service com.xxx.dubbo.demo.service.GreetingService url dubbo://192.168.102.51:20880/com.xxx.dubbo.demo.service.GreetingService?anyhost=true&application=dubbo-demo-provider&dubbo=2.5.4-SNAPSHOT&generic=false&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=18448&side=provider&threadpool=cached&threads=100×tamp=1495112774127 to registry registry://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=dubbo-demo-provider&client=zkclient&dubbo=2.5.4-SNAPSHOT&pid=18448®istry=zookeeper×tamp=1495112774106, dubbo version: 2.5.4-SNAPSHOT, current host: 127.0.0.1
[INFO ][2017-05-18 21:06:14,397] com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:69) - [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.102.51:20880, dubbo version: 2.5.4-SNAPSHOT, current host: 127.0.0.1
[INFO ][2017-05-18 21:06:14,434] org.I0Itec.zkclient.ZkEventThread.run(ZkEventThread.java:64) - Starting ZkClient event thread.
[INFO ][2017-05-18 21:06:14,443] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:zookeeper.version=3.3.3-1073969, built on 02/23/2011 22:27 GMT
[INFO ][2017-05-18 21:06:14,443] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:host.name=wangmengjun-PC
[INFO ][2017-05-18 21:06:14,443] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.version=1.7.0_79
[INFO ][2017-05-18 21:06:14,443] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.vendor=Oracle Corporation
[INFO ][2017-05-18 21:06:14,444] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.home=C:Program FilesJavajre7
[INFO ][2017-05-18 21:06:14,444] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.class.path=F:JavaDeveloperworkspacesSpringMVCDubboExampledubbo-demodubbo-demo-providertargetclasses;F:JavaDeveloperworkspacesSpringMVCDubboExampledubbo-demodubbo-demo-interfacetargetclasses;D:java_toolsReponsitoriesMavenorgspringframeworkspring-core4.3.6.RELEASEspring-core-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavencommons-loggingcommons-logging1.2commons-logging-1.2.jar;D:java_toolsReponsitoriesMavenorgspringframeworkspring-context4.3.6.RELEASEspring-context-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavenorgspringframeworkspring-aop4.3.6.RELEASEspring-aop-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavenorgspringframeworkspring-beans4.3.6.RELEASEspring-beans-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavenorgspringframeworkspring-expression4.3.6.RELEASEspring-expression-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavencomalibabadubbo2.5.4-snapshotdubbo-2.5.4-snapshot.jar;D:java_toolsReponsitoriesMavenorgjavassistjavassist3.18.1-GAjavassist-3.18.1-GA.jar;D:java_toolsReponsitoriesMavenionettynetty3.10.6.Finalnetty-3.10.6.Final.jar;D:java_toolsReponsitoriesMavencomgithubsgroschupfzkclient .1zkclient-0.1.jar;D:java_toolsReponsitoriesMavenorgapachezookeeperzookeeper3.3.3zookeeper-3.3.3.jar;D:java_toolsReponsitoriesMavenjlinejline .9.94jline-0.9.94.jar;D:java_toolsReponsitoriesMavenjunitjunit3.8.1junit-3.8.1.jar;D:java_toolsReponsitoriesMavenlog4jlog4j1.2.14log4j-1.2.14.jar
[INFO ][2017-05-18 21:06:14,444] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.library.path=C:Program FilesJavajre7bin;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)Common Fileslenovoeasyplussdkbin;.;C:Program FilesJavajdk1.7.0_79bin;D:developapache-jmeter-2.13bin;C:Program FilesTortoiseSVNbin;D:developDatabasesMysqlmysql-5.6.20bin;D:developSonarsonar-runner-2.4bin;D:developSonarsonarqube-5.1.1bin;D:developpythonpython-2.7.10python.exe;C:Program FilesTortoiseGitbin;D:developZeroCZeroCIce-3.6.2bin;D:developapache-maven-3.3.3bin;D:developSonarsonar-scanner-2.8bin;F:JavaDeveloperSQLmysql-5.7.17-winx64bin;F:JavaDeveloperNodejsnodejs7;C:UserswangmengjunAppDataLocalProgramsPythonPython36Scripts;C:UserswangmengjunAppDataLocalProgramsPythonPython36;C:UserswangmengjunAppDataLocalProgramsPythonPython35Scripts;C:UserswangmengjunAppDataLocalProgramsPythonPython35;C:Program Files (x86)OpenV**bin;C:UserswangmengjunAppDataLocalMicrosoftWindowsApps;C:UserswangmengjunAppDataRoamingnpm;.
[INFO ][2017-05-18 21:06:14,444] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.io.tmpdir=C:UsersWANGME~1AppDataLocalTemp
[INFO ][2017-05-18 21:06:14,444] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.compiler=<NA>
[INFO ][2017-05-18 21:06:14,444] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:os.name=Windows 8.1
[INFO ][2017-05-18 21:06:14,445] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:os.arch=amd64
[INFO ][2017-05-18 21:06:14,445] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:os.version=6.3
[INFO ][2017-05-18 21:06:14,445] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:user.name=wangmengjun
[INFO ][2017-05-18 21:06:14,445] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:user.home=C:Userswangmengjun
[INFO ][2017-05-18 21:06:14,445] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:user.dir=F:JavaDeveloperworkspacesSpringMVCDubboExampledubbo-demodubbo-demo-provider
[INFO ][2017-05-18 21:06:14,446] org.apache.zookeeper.ZooKeeper.<init>(ZooKeeper.java:373) - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.I0Itec.zkclient.ZkClient@7251c1ad
[INFO ][2017-05-18 21:06:14,454] org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1041) - Opening socket connection to server /127.0.0.1:2181
[INFO ][2017-05-18 21:06:14,456] org.apache.zookeeper.ClientCnxn$SendThread.primeConnection(ClientCnxn.java:949) - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO ][2017-05-18 21:06:14,608] org.apache.zookeeper.ClientCnxn$SendThread.readConnectResult(ClientCnxn.java:738) - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x15c1ba12c860000, negotiated timeout = 30000
[INFO ][2017-05-18 21:06:14,610] org.I0Itec.zkclient.ZkClient.processStateChanged(ZkClient.java:449) - zookeeper state changed (SyncConnected)
[INFO ][2017-05-18 21:06:14,611] com.alibaba.dubbo.registry.support.AbstractRegistry.register(AbstractRegistry.java:302) - [DUBBO] Register: dubbo://192.168.102.51:20880/com.xxx.dubbo.demo.service.GreetingService?anyhost=true&application=dubbo-demo-provider&dubbo=2.5.4-SNAPSHOT&generic=false&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=18448&side=provider&threadpool=cached&threads=100×tamp=1495112774127, dubbo version: 2.5.4-SNAPSHOT, current host: 127.0.0.1
[INFO ][2017-05-18 21:06:14,699] com.alibaba.dubbo.registry.support.AbstractRegistry.subscribe(AbstractRegistry.java:325) - [DUBBO] Subscribe: provider://192.168.102.51:20880/com.xxx.dubbo.demo.service.GreetingService?anyhost=true&application=dubbo-demo-provider&category=configurators&check=false&dubbo=2.5.4-SNAPSHOT&generic=false&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=18448&side=provider&threadpool=cached&threads=100×tamp=1495112774127, dubbo version: 2.5.4-SNAPSHOT, current host: 127.0.0.1
[INFO ][2017-05-18 21:06:14,758] com.alibaba.dubbo.registry.support.AbstractRegistry.notify(AbstractRegistry.java:422) - [DUBBO] Notify urls for subscribe url provider://192.168.102.51:20880/com.xxx.dubbo.demo.service.GreetingService?anyhost=true&application=dubbo-demo-provider&category=configurators&check=false&dubbo=2.5.4-SNAPSHOT&generic=false&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=18448&side=provider&threadpool=cached&threads=100×tamp=1495112774127, urls: [empty://192.168.102.51:20880/com.xxx.dubbo.demo.service.GreetingService?anyhost=true&application=dubbo-demo-provider&category=configurators&check=false&dubbo=2.5.4-SNAPSHOT&generic=false&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=18448&side=provider&threadpool=cached&threads=100×tamp=1495112774127], dubbo version: 2.5.4-SNAPSHOT, current host: 127.0.0.1
6.3 消费者调用
接下来,最后一个步骤就是在dubbo-demo-consumer中,调用Dubbo服务即可。
运行中的Main类,结果如下:
代码语言:javascript复制
[INFO ][2017-05-18 21:08:45,916] org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:582) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6ff58334: startup date [Thu May 18 21:08:45 CST 2017]; root of context hierarchy
[INFO ][2017-05-18 21:08:45,966] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [applicationContext.xml]
[INFO ][2017-05-18 21:08:46,059] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [spring-dubbo-consumer.xml]
[INFO ][2017-05-18 21:08:46,118] - using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter
[INFO ][2017-05-18 21:08:46,533] com.alibaba.dubbo.registry.support.AbstractRegistry.loadProperties(AbstractRegistry.java:232) - [DUBBO] Load registry store file C:Userswangmengjun.dubbodubbo-registry-127.0.0.1.cache, data: {com.xxx.dubbo.demo.service.GreetingService=empty://192.168.102.51:20880/com.xxx.dubbo.demo.service.GreetingService?anyhost=true&application=dubbo-demo-provider&category=configurators&check=false&dubbo=2.5.4-SNAPSHOT&generic=false&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=18448&side=provider&threadpool=cached&threads=100×tamp=1495112774127}, dubbo version: 2.5.4-SNAPSHOT, current host: 127.0.0.1
[INFO ][2017-05-18 21:08:46,545] org.I0Itec.zkclient.ZkEventThread.run(ZkEventThread.java:64) - Starting ZkClient event thread.
[INFO ][2017-05-18 21:08:46,554] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:zookeeper.version=3.3.3-1073969, built on 02/23/2011 22:27 GMT
[INFO ][2017-05-18 21:08:46,554] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:host.name=wangmengjun-PC
[INFO ][2017-05-18 21:08:46,554] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.version=1.7.0_79
[INFO ][2017-05-18 21:08:46,554] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.vendor=Oracle Corporation
[INFO ][2017-05-18 21:08:46,554] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.home=C:Program FilesJavajre7
[INFO ][2017-05-18 21:08:46,555] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.class.path=F:JavaDeveloperworkspacesSpringMVCDubboExampledubbo-demodubbo-demo-consumertargetclasses;F:JavaDeveloperworkspacesSpringMVCDubboExampledubbo-demodubbo-demo-interfacetargetclasses;D:java_toolsReponsitoriesMavenorgspringframeworkspring-core4.3.6.RELEASEspring-core-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavencommons-loggingcommons-logging1.2commons-logging-1.2.jar;D:java_toolsReponsitoriesMavenorgspringframeworkspring-context4.3.6.RELEASEspring-context-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavenorgspringframeworkspring-aop4.3.6.RELEASEspring-aop-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavenorgspringframeworkspring-beans4.3.6.RELEASEspring-beans-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavenorgspringframeworkspring-expression4.3.6.RELEASEspring-expression-4.3.6.RELEASE.jar;D:java_toolsReponsitoriesMavencomalibabadubbo2.5.4-snapshotdubbo-2.5.4-snapshot.jar;D:java_toolsReponsitoriesMavenorgjavassistjavassist3.18.1-GAjavassist-3.18.1-GA.jar;D:java_toolsReponsitoriesMavenionettynetty3.10.6.Finalnetty-3.10.6.Final.jar;D:java_toolsReponsitoriesMavencomgithubsgroschupfzkclient .1zkclient-0.1.jar;D:java_toolsReponsitoriesMavenorgapachezookeeperzookeeper3.3.3zookeeper-3.3.3.jar;D:java_toolsReponsitoriesMavenjlinejline .9.94jline-0.9.94.jar;D:java_toolsReponsitoriesMavenjunitjunit3.8.1junit-3.8.1.jar;D:java_toolsReponsitoriesMavenlog4jlog4j1.2.14log4j-1.2.14.jar
[INFO ][2017-05-18 21:08:46,555] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.library.path=C:Program FilesJavajre7bin;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)Common Fileslenovoeasyplussdkbin;.;C:Program FilesJavajdk1.7.0_79bin;D:developapache-jmeter-2.13bin;C:Program FilesTortoiseSVNbin;D:developDatabasesMysqlmysql-5.6.20bin;D:developSonarsonar-runner-2.4bin;D:developSonarsonarqube-5.1.1bin;D:developpythonpython-2.7.10python.exe;C:Program FilesTortoiseGitbin;D:developZeroCZeroCIce-3.6.2bin;D:developapache-maven-3.3.3bin;D:developSonarsonar-scanner-2.8bin;F:JavaDeveloperSQLmysql-5.7.17-winx64bin;F:JavaDeveloperNodejsnodejs7;C:UserswangmengjunAppDataLocalProgramsPythonPython36Scripts;C:UserswangmengjunAppDataLocalProgramsPythonPython36;C:UserswangmengjunAppDataLocalProgramsPythonPython35Scripts;C:UserswangmengjunAppDataLocalProgramsPythonPython35;C:Program Files (x86)OpenV**bin;C:UserswangmengjunAppDataLocalMicrosoftWindowsApps;C:UserswangmengjunAppDataRoamingnpm;.
[INFO ][2017-05-18 21:08:46,555] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.io.tmpdir=C:UsersWANGME~1AppDataLocalTemp
[INFO ][2017-05-18 21:08:46,555] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:java.compiler=<NA>
[INFO ][2017-05-18 21:08:46,556] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:os.name=Windows 8.1
[INFO ][2017-05-18 21:08:46,556] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:os.arch=amd64
[INFO ][2017-05-18 21:08:46,556] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:os.version=6.3
[INFO ][2017-05-18 21:08:46,556] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:user.name=wangmengjun
[INFO ][2017-05-18 21:08:46,556] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:user.home=C:Userswangmengjun
[INFO ][2017-05-18 21:08:46,557] org.apache.zookeeper.Environment.logEnv(Environment.java:97) - Client environment:user.dir=F:JavaDeveloperworkspacesSpringMVCDubboExampledubbo-demodubbo-demo-consumer
[INFO ][2017-05-18 21:08:46,558] org.apache.zookeeper.ZooKeeper.<init>(ZooKeeper.java:373) - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.I0Itec.zkclient.ZkClient@4c52f14d
[INFO ][2017-05-18 21:08:46,576] org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1041) - Opening socket connection to server /127.0.0.1:2181
[INFO ][2017-05-18 21:08:46,578] org.apache.zookeeper.ClientCnxn$SendThread.primeConnection(ClientCnxn.java:949) - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO ][2017-05-18 21:08:46,605] org.apache.zookeeper.ClientCnxn$SendThread.readConnectResult(ClientCnxn.java:738) - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x15c1ba12c860001, negotiated timeout = 30000
[INFO ][2017-05-18 21:08:46,607] org.I0Itec.zkclient.ZkClient.processStateChanged(ZkClient.java:449) - zookeeper state changed (SyncConnected)
[INFO ][2017-05-18 21:08:46,628] com.alibaba.dubbo.registry.support.AbstractRegistry.register(AbstractRegistry.java:302) - [DUBBO] Register: consumer://192.168.102.51/com.xxx.dubbo.demo.service.GreetingService?application=DemoProvider&category=consumers&check=false&dubbo=2.5.4-SNAPSHOT&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=21572&side=consumer×tamp=1495112926463, dubbo version: 2.5.4-SNAPSHOT, current host: 192.168.102.51
[INFO ][2017-05-18 21:08:46,702] com.alibaba.dubbo.registry.support.AbstractRegistry.subscribe(AbstractRegistry.java:325) - [DUBBO] Subscribe: consumer://192.168.102.51/com.xxx.dubbo.demo.service.GreetingService?application=DemoProvider&category=providers,configurators,routers&dubbo=2.5.4-SNAPSHOT&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=21572&side=consumer×tamp=1495112926463, dubbo version: 2.5.4-SNAPSHOT, current host: 192.168.102.51
[INFO ][2017-05-18 21:08:46,853] com.alibaba.dubbo.registry.support.AbstractRegistry.notify(AbstractRegistry.java:422) - [DUBBO] Notify urls for subscribe url consumer://192.168.102.51/com.xxx.dubbo.demo.service.GreetingService?application=DemoProvider&category=providers,configurators,routers&dubbo=2.5.4-SNAPSHOT&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=21572&side=consumer×tamp=1495112926463, urls: [dubbo://192.168.102.51:20880/com.xxx.dubbo.demo.service.GreetingService?anyhost=true&application=dubbo-demo-provider&dubbo=2.5.4-SNAPSHOT&generic=false&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=18448&side=provider&threadpool=cached&threads=100×tamp=1495112774127, empty://192.168.102.51/com.xxx.dubbo.demo.service.GreetingService?application=DemoProvider&category=configurators&dubbo=2.5.4-SNAPSHOT&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=21572&side=consumer×tamp=1495112926463, empty://192.168.102.51/com.xxx.dubbo.demo.service.GreetingService?application=DemoProvider&category=routers&dubbo=2.5.4-SNAPSHOT&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=21572&side=consumer×tamp=1495112926463], dubbo version: 2.5.4-SNAPSHOT, current host: 192.168.102.51
[INFO ][2017-05-18 21:08:46,974] com.alibaba.dubbo.remoting.transport.AbstractClient.connect(AbstractClient.java:287) - [DUBBO] Successed connect to server /192.168.102.51:20880 from NettyClient 192.168.102.51 using dubbo version 2.5.4-SNAPSHOT, channel is NettyChannel [channel=[id: 0x5830ab5f, /192.168.102.51:59578 => /192.168.102.51:20880]], dubbo version: 2.5.4-SNAPSHOT, current host: 192.168.102.51
[INFO ][2017-05-18 21:08:46,975] com.alibaba.dubbo.remoting.transport.AbstractClient.<init>(AbstractClient.java:105) - [DUBBO] Start NettyClient wangmengjun-PC/192.168.102.51 connect to the server /192.168.102.51:20880, dubbo version: 2.5.4-SNAPSHOT, current host: 192.168.102.51
[INFO ][2017-05-18 21:08:47,030] com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:423) - [DUBBO] Refer dubbo service com.xxx.dubbo.demo.service.GreetingService from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=DemoProvider&check=false&dubbo=2.5.4-SNAPSHOT&generic=false&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=21572&side=consumer×tamp=1495112926463, dubbo version: 2.5.4-SNAPSHOT, current host: 192.168.102.51
Consumer ==> Hello, Eric
[INFO ][2017-05-18 21:08:47,140] org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:987) - Closing org.springframework.context.support.ClassPathXmlApplicationContext@6ff58334: startup date [Thu May 18 21:08:45 CST 2017]; root of context hierarchy
[INFO ][2017-05-18 21:08:47,140] com.alibaba.dubbo.registry.support.AbstractRegistry.unsubscribe(AbstractRegistry.java:343) - [DUBBO] Unsubscribe: consumer://192.168.102.51/com.xxx.dubbo.demo.service.GreetingService?application=DemoProvider&category=providers,configurators,routers&dubbo=2.5.4-SNAPSHOT&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=21572&side=consumer×tamp=1495112926463, dubbo version: 2.5.4-SNAPSHOT, current host: 192.168.102.51
[INFO ][2017-05-18 21:08:47,141] com.alibaba.dubbo.remoting.transport.netty.NettyChannel.close(NettyChannel.java:135) - [DUBBO] Close netty channel [id: 0x5830ab5f, /192.168.102.51:59578 => /192.168.102.51:20880], dubbo version: 2.5.4-SNAPSHOT, current host: 192.168.102.51
[INFO ][2017-05-18 21:08:47,143] com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.disconnected(DubboProtocol.java:130) - [DUBBO] disconected from /192.168.102.51:20880,url:dubbo://192.168.102.51:20880/com.xxx.dubbo.demo.service.GreetingService?anyhost=true&application=DemoProvider&check=false&codec=dubbo&dubbo=2.5.4-SNAPSHOT&generic=false&heartbeat=60000&interface=com.xxx.dubbo.demo.service.GreetingService&methods=hello&pid=21572&side=consumer×tamp=1495112926463, dubbo version: 2.5.4-SNAPSHOT, current host: 192.168.102.51
从上述日志中可以看出,hello接口已经成功调用,并输出了Consumer ==> Hello, Eric
当然,我们可以切换到provider的输出日志,包含hello方法被调用执行的时间信息。
代码语言:javascript复制
Hello Service is calling : Thu May 18 21:08:47 CST 2017
至此,一个简单的Maven Spring Dubbb Zookeeper整合示例就完成了。后续,可以将provider变成一个Web工程,如springMVC实现等,其中可以和MySQL或者NoSQL(如MongoDB等)集成,进一步完成其它功能。
七、问题解决参考
7.1 No appenders could be found for logger
代码语言:javascript复制
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
根据描述 ,解决方法很简单,配置一个log4j.properties文件在resouce下即可。
log4j.properties的内容简单如下:
代码语言:javascript复制
# Rules reminder:
# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=INFO,stdout
## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH:mm:ss,SSS}] %l - %m%n
八、整个工程结构