dubbo-SPI扩展(二)

本篇文章主要描述一下dubbo的扩展点中的一些基本概念和常见的一些注解

基本概念

扩展点(Extension Point)

扩展点其实就是一个Java的接口。比如dubbo中的LoadBalance接口其实就是一个扩展点

1
2
3
4
5
6
@SPI(RandomLoadBalance.NAME)
public interface LoadBalance {

@Adaptive("loadbalance")
<T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException;
}

扩展(Extension)

扩展其实扩展点的实现类。比如以扩展点LoadBalance来说,RandomLoadBalance其实就是他的一个实现类,也是一个扩展。

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
package org.apache.dubbo.rpc.cluster.loadbalance;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
* random load balance.
*
*/
public class RandomLoadBalance extends AbstractLoadBalance {

public static final String NAME = "random";

@Override
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
int length = invokers.size(); // Number of invokers
boolean sameWeight = true; // Every invoker has the same weight?
int firstWeight = getWeight(invokers.get(0), invocation);
int totalWeight = firstWeight; // The sum of weights
for (int i = 1; i < length; i++) {
int weight = getWeight(invokers.get(i), invocation);
totalWeight += weight; // Sum
if (sameWeight && weight != firstWeight) {
sameWeight = false;
}
}
if (totalWeight > 0 && !sameWeight) {
// If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
int offset = ThreadLocalRandom.current().nextInt(totalWeight);
// Return a invoker based on the random value.
for (int i = 0; i < length; i++) {
offset -= getWeight(invokers.get(i), invocation);
if (offset < 0) {
return invokers.get(i);
}
}
}
// If all invokers have the same weight value or totalWeight=0, return evenly.
return invokers.get(ThreadLocalRandom.current().nextInt(length));
}

}

扩展实例(Extension Instance)

扩展实例其实就是扩展点实现类的实例。比如new RandomLoadBalance()其实就可以得到LoadBalance扩展点的一个扩展实例

扩展自适应实例(Extension Adaptive Instance)

这个自适应实例需要好好理解一下,这个很重要。

常见注解

TODO

基本使用

TODO

dubbo-SPI扩展(一)

本篇文章描述一下dubbo的扩展性实现,主要有下面几个部分:

  • 什么叫可扩展性
  • 常见的扩展性的解决方案
  • java spi简介
  • 为什么dubbo不采用java spi,而是自己实现一个SPI机制呢
  • dubbo spi基本使用
  • dubbo扩展点的基本概念
  • dubbo SPI源码阅读

本篇文章也参考了很多业界资料,详见文件结尾

什么叫可扩展性

如同罗马不是一天建成的,任何系统都一定是从小系统不断发展成为大系统的,想要从一开始就把系统设计的足够完善是不可能的,相反的,我们应该关注当下的需求,然后再不断地对系统进行迭代。在代码层面,要求我们适当的对关注点进行抽象和隔离,在软件不断添加功能和特性时,依然能保持良好的结构和可维护性,同时允许第三方开发者对其功能进行扩展。在某些时候,软件设计者对扩展性的追求甚至超过了性能。

在谈到软件设计时,可扩展性一直被谈起,那到底什么才是可扩展性,什么样的框架才算有良好的可扩展性呢?它必须要做到以下两点:

  • 作为框架的维护者,在添加一个新功能时,只需要添加一些新代码,而不用大量的修改现有的代码,即符合开闭原则。
  • 作为框架的使用者,在添加一个新功能时,不需要去修改框架的源码,在自己的工程中添加代码即可。

Dubbo很好的做到了上面两点。这要得益于Dubbo的微内核+插件的机制。接下来的章节中我们会慢慢揭开Dubbo扩展机制的神秘面纱。

常见的扩展性的解决方案

  • Factory模式
  • IoC容器
  • OSGI容器

Dubbo作为一个框架,不希望强依赖其他的IoC容器,比如Spring,Guice。OSGI也是一个很重的实现,不适合Dubbo。最终Dubbo的实现参考了Java原生的SPI机制,但对其进行了一些扩展,以满足Dubbo的需求。

java spi简介

SPI 全称为 Service Provider Interface,是一种服务发现机制。SPI 的本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件,加载实现类。这样可以在运行时,动态为接口替换实现类。正因此特性,我们可以很容易的通过 SPI 机制为我们的程序提供拓展功能。

本节通过一个示例演示 Java SPI 的使用方法。首先,我们定义一个接口,名称为 Robot。

1
2
3
public interface Robot {
void sayHello();
}

接下来定义两个实现类,分别为 OptimusPrime 和 Bumblebee。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class OptimusPrime implements Robot {

@Override
public void sayHello() {
System.out.println("Hello, I am Optimus Prime.");
}
}

public class Bumblebee implements Robot {

@Override
public void sayHello() {
System.out.println("Hello, I am Bumblebee.");
}
}

接下来 META-INF/services 文件夹下创建一个文件,名称为 Robot 的全限定名 org.apache.spi.Robot。文件内容为实现类的全限定的类名,如下:

1
2
org.apache.spi.OptimusPrime
org.apache.spi.Bumblebee

做好所需的准备工作,接下来编写代码进行测试。

1
2
3
4
5
6
7
8
9
public class JavaSPITest {

@Test
public void sayHello() throws Exception {
ServiceLoader<Robot> serviceLoader = ServiceLoader.load(Robot.class);
System.out.println("Java SPI");
serviceLoader.forEach(Robot::sayHello);
}
}

程序将输出:

1
2
3
JAVA SPI
Hello, I am Optimus Prime.
Hello, I am Bumblebee.

从测试结果可以看出,我们的两个实现类被成功的加载,并输出了相应的内容。关于 Java SPI 的演示先到这里

为什么dubbo不采用java spi,而是自己实现一个SPI机制呢

Java SPI的使用很简单。也做到了基本的加载扩展点的功能。但Java SPI有以下的不足:

  • 需要遍历所有的实现,并实例化,然后我们在循环中才能找到我们需要的实现。
  • 配置文件中只是简单的列出了所有的扩展实现,而没有给他们命名。导致在程序中很难去准确的引用它们。
  • 扩展如果依赖其他的扩展,做不到自动注入和装配
  • 不提供类似于Spring的IOC和AOP功能
  • 扩展很难和其他的框架集成,比如扩展里面依赖了一个Spring bean,原生的Java SPI不支持

所以Java SPI应付一些简单的场景是可以的,但对于Dubbo,它的功能还是比较弱的。Dubbo对原生SPI机制进行了一些扩展。接下来,我们就更深入地了解下Dubbo的SPI机制。

Dubbo 改进了 JDK 标准的 SPI 的以下问题:

  • JDK 标准的 SPI 会一次性实例化扩展点所有实现,如果有扩展实现初始化很耗时,但如果没用上也加载,会很浪费资源。
  • 如果扩展点加载失败,连扩展点的名称都拿不到了。比如:JDK 标准的 ScriptEngine,通过 getName() 获取脚本类型的名称,但如果 RubyScriptEngine 因为所依赖的 jruby.jar 不存在,导致 RubyScriptEngine 类加载失败,这个失败原因被吃掉了,和 ruby 对应不起来,当用户执行 ruby 脚本时,会报不支持 ruby,而不是真正失败的原因。
  • 增加了对扩展点 IoC 和 AOP 的支持,一个扩展点可以直接 setter 注入其它扩展点。

其实最核心的改进是第一个和第三个问题。

dubbo spi基本使用

我们继续使用上面的例子。由于Dubbo 并未使用 Java SPI,而是重新实现了一套功能更强的 SPI 机制。Dubbo SPI 的相关逻辑被封装在了 ExtensionLoader 类中,通过 ExtensionLoader,我们可以加载指定的实现类。Dubbo SPI 所需的配置文件需放置在 META-INF/dubbo 路径下,配置内容如下。

1
2
optimusPrime = org.apache.spi.OptimusPrime
bumblebee = org.apache.spi.Bumblebee

需要在 Robot 接口上标注 @SPI 注解。下面来演示 Dubbo SPI 的用法:

1
2
3
4
5
6
7
8
9
10
11
12
public class DubboSPITest {

@Test
public void sayHello() throws Exception {
ExtensionLoader<Robot> extensionLoader =
ExtensionLoader.getExtensionLoader(Robot.class);
Robot optimusPrime = extensionLoader.getExtension("optimusPrime");
optimusPrime.sayHello();
Robot bumblebee = extensionLoader.getExtension("bumblebee");
bumblebee.sayHello();
}
}

程序将输出

1
2
Hello, I am Optimus Prime.
Hello, I am Bumblebee.

参考资料

dubbo自定义filter

dubbo的扩展性是特别的好,本篇文章通过例子来说明如何自定义dubbo的filter。为了文章完整性,贴一下官网对于filter的一些说明。

扩展说明

服务提供方和服务消费方调用过程拦截,Dubbo 本身的大多功能均基于此扩展点实现,每次远程方法执行,该拦截都会被执行,请注意对性能的影响。

约定:

  • 用户自定义 filter 默认在内置 filter 之后。
  • 特殊值 default,表示缺省扩展点插入的位置。比如:filter="xxx,default,yyy",表示 xxx` 在缺省 filter 之前,yyy 在缺省 filter 之后。
  • 特殊符号 -,表示剔除。比如:filter="-foo1",剔除添加缺省扩展点 foo1。比如:filter="-default",剔除添加所有缺省扩展点。
  • provider 和 service 同时配置的 filter 时,累加所有 filter,而不是覆盖。比如:<dubbo:provider filter="xxx,yyy"/><dubbo:service filter="aaa,bbb" />,则 xxx,yyy,aaa,bbb 均会生效。如果要覆盖,需配置:<dubbo:service filter="-xxx,-yyy,aaa,bbb" />

扩展接口

org.apache.dubbo.rpc.Filter

扩展配置

1
2
3
4
5
6
7
8
<!-- 消费方调用过程拦截 -->
<dubbo:reference filter="xxx,yyy" />
<!-- 消费方调用过程缺省拦截器,将拦截所有reference -->
<dubbo:consumer filter="xxx,yyy"/>
<!-- 提供方调用过程拦截 -->
<dubbo:service filter="xxx,yyy" />
<!-- 提供方调用过程缺省拦截器,将拦截所有service -->
<dubbo:provider filter="xxx,yyy"/>

已知扩展

  • org.apache.dubbo.rpc.filter.EchoFilter
  • org.apache.dubbo.rpc.filter.GenericFilter
  • org.apache.dubbo.rpc.filter.GenericImplFilter
  • org.apache.dubbo.rpc.filter.TokenFilter
  • org.apache.dubbo.rpc.filter.AccessLogFilter
  • org.apache.dubbo.rpc.filter.CountFilter
  • org.apache.dubbo.rpc.filter.ActiveLimitFilter
  • org.apache.dubbo.rpc.filter.ClassLoaderFilter
  • org.apache.dubbo.rpc.filter.ContextFilter
  • org.apache.dubbo.rpc.filter.ConsumerContextFilter
  • org.apache.dubbo.rpc.filter.ExceptionFilter
  • org.apache.dubbo.rpc.filter.ExecuteLimitFilter
  • org.apache.dubbo.rpc.filter.DeprecatedFilter

官网扩展示例

Maven 项目结构:

1
2
3
4
5
6
7
8
9
10
src
|-main
|-java
|-com
|-xxx
|-XxxFilter.java (实现Filter接口)
|-resources
|-META-INF
|-dubbo
|-org.apache.dubbo.rpc.Filter (纯文本文件,内容为:xxx=com.xxx.XxxFilter)

XxxFilter.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.xxx;

import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;

public class XxxFilter implements Filter {
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// before filter ...
Result result = invoker.invoke(invocation);
// after filter ...
return result;
}
}

然后配置dubbo spi:
META-INF/dubbo/org.apache.dubbo.rpc.Filter:
xxx=com.xxx.XxxFilter

实际例子

比如希望dubbo consumer在请求dubbo provider的时候,自动携带自己的jar的版本信息,那么就可以写一个JarVersionAttachFilter

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
package com.xxxx.xx.xx.rpc.dubbo.filter;

import java.util.Map;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.xxx.common.util.JarUtils;
import com.xxx.common.util.Safes;
import com.xxx.xxx.xxx.rpc.dubbo.DubboConstants;

import org.springframework.util.StringUtils;

@Activate(group = {Constants.CONSUMER}, order = -1)
public class JarVersionAttachFilter implements Filter {

private LoadingCache<Class<?>, String> versionMapping = CacheBuilder.newBuilder().maximumSize(1024).build(new CacheLoader<Class<?>, String>() {
@Override
public String load(Class<?> key) throws Exception {
return Safes.of(JarUtils.getVersion(key, null));
}
});

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
Map<String, String> attachments = invocation.getAttachments();
String version = versionMapping.getUnchecked(invoker.getInterface());
if (StringUtils.hasText(version)) {
attachments.put(DubboConstants.JAR_VERSION_NAME, version);
}
return invoker.invoke(invocation);
}
}

然后在dubbo的spi文件中写入:jarattach=com.xxxx.xx.xx.rpc.dubbo.filter.JarVersionAttachFilter就好了。这样当consumer发起请求的时候,会自动携带自己的jar的版本信息

参考资料

本地搭建dubbo的运行环境

本篇文章主要讲述如何在本地搭建dubbo的运行环境

安装zk

dubbo推荐使用zk来作为自己的注册中心,当然使用其余的实现来作为注册中心也是可以的。比如我之前就使用redis实现了一个注册中心。

Dubbo 未对 Zookeeper 服务器端做任何侵入修改,只需安装原生的 Zookeeper 服务器即可,
所有注册中心逻辑适配都在调用 Zookeeper 客户端时完成。

安装zk:

1
2
3
4
wget http://archive.apache.org/dist/zookeeper/zookeeper-3.3.3/zookeeper-3.3.3.tar.gz
tar zxvf zookeeper-3.3.3.tar.gz
cd zookeeper-3.3.3
cp conf/zoo_sample.cfg conf/zoo.cfg

配置:

1
vi conf/zoo.cfg

如果不需要集群,zoo.cfg 的内容如下 :

1
2
3
4
5
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/dubbo/zookeeper-3.3.3/data
clientPort=2181

如果需要集群,zoo.cfg 的内容如下(其中 data 目录和 server 地址需改成你真实部署机器的信息)

1
2
3
4
5
6
7
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/dubbo/zookeeper-3.3.3/data
clientPort=2181
server.1=10.20.153.10:2555:3555
server.2=10.20.153.11:2555:3555

并在 data 目录下放置 myid 文件:

1
2
mkdir data
vi myid

myid 指明自己的 id,对应上面 zoo.cfgserver. 后的数字,第一台的内容为 1,第二台的内容为 2,内容如下:

1
1

启动

1
./bin/zkServer.sh start

如果是windows环境的话,直接双击zkServer.cmd文件。不过建议修改zkServer.cmd文件,在文件的最末尾处增加一行,内容为pause,修改完以后zkServer.cmd文件
的内容为

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
@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more
REM contributor license agreements. See the NOTICE file distributed with
REM this work for additional information regarding copyright ownership.
REM The ASF licenses this file to You under the Apache License, Version 2.0
REM (the "License"); you may not use this file except in compliance with
REM the License. You may obtain a copy of the License at
REM
REM http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

pause

这样当系统环境有问题的时候就可以在终端看见具体是啥问题了。

停止:

1
./bin/zkServer.sh stop
1
2
telnet 127.0.0.1 2181
dump

或者:

1
echo dump | nc 127.0.0.1 2181

用法:

1
dubbo.registry.address=zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181

或者:

1
<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181,10.20.153.11:2181" />

比如以dubbo-demo-provider中的dubbo-demo-provider.xml文件为例子,使用本地的zk修改以后内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>

<!-- use multicast registry center to export service -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry protocol="zookeeper" address="localhost:2181"/>

<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>

<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>

<!-- declare the service interface to be exported -->
<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>

</beans>

安装dubbo

其实就是下载dubbo的源码以及对应的maven依赖:

1
2
3
4
git clone https://github.com/apache/incubator-dubbo.git
cd incubator-dubbo
运行 dubbo-demo-provider中的com.alibaba.dubbo.demo.provider.Provider
如果使用Intellij Idea 请加上-Djava.net.preferIPv4Stack=true

配置:

1
2
resource/META-INFO.spring/dubbo-demo-provider.xml
修改其中的dubbo:registery,替换成真实的注册中心地址,推荐使用zookeeper

运行demo

启动provider

本地启动zk以后,运行dubbo-demo-provider中的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
Connected to the target VM, address: '127.0.0.1:7562', transport: 'socket'
[20/08/18 11:43:20:020 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@551aa95a: startup date [Mon Aug 20 23:43:20 CST 2018]; root of context hierarchy
[20/08/18 11:43:20:020 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/dubbo-demo-provider.xml]
[20/08/18 11:43:20:020 CST] main INFO logger.LoggerFactory: using logger: org.apache.dubbo.common.logger.log4j.Log4jLoggerAdapter
[20/08/18 11:43:20:020 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:defaultCompiler, try to find an extension(bean) of type java.lang.String, dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:20:020 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:defaultCompiler, type:java.lang.String found, stop get bean., dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:20:020 CST] main INFO config.AbstractConfig: [DUBBO] The service ready on spring started. service: org.apache.dubbo.demo.DemoService, dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:21:021 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service org.apache.dubbo.demo.DemoService to local registry, dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:21:021 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service org.apache.dubbo.demo.DemoService to url dubbo://192.168.116.1:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.116.1&bind.port=20880&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=6872&qos.port=22222&side=provider&timestamp=1534779801013, dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:21:021 CST] main INFO config.AbstractConfig: [DUBBO] Register dubbo service org.apache.dubbo.demo.DemoService url dubbo://192.168.116.1:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.116.1&bind.port=20880&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=6872&qos.port=22222&side=provider&timestamp=1534779801013 to registry registry://localhost:2181/org.apache.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.2&pid=6872&qos.port=22222&registry=zookeeper&timestamp=1534779801006, dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:22:022 CST] main INFO transport.AbstractServer: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.116.1:20880, dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Load registry store file C:\Users\wenchao.ren\.dubbo\dubbo-registry-demo-provider-localhost:2181.cache, data: {org.apache.dubbo.demo.DemoService=empty://192.168.116.1:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=18588&side=provider&timestamp=1534778576083}, dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:22:022 CST] main INFO imps.CuratorFrameworkImpl: Starting
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:host.name=c
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:java.version=1.8.0_181
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:java.vendor=Oracle Corporation
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:java.home=C:\Program Files\Java\jdk1.8.0_181\jre
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:java.class.path=C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-demo\dubbo-demo-provider\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-demo\dubbo-demo-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-config\dubbo-config-spring\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-config\dubbo-config-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-monitor\dubbo-monitor-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-filter\dubbo-filter-validation\target\classes;C:\Users\wenchao.ren\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-filter\dubbo-filter-cache\target\classes;C:\Users\wenchao.ren\.m2\repository\javax\cache\cache-api\1.0.0\cache-api-1.0.0.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-bootstrap\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-common\target\classes;C:\Users\wenchao.ren\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\wenchao.ren\.m2\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;C:\Users\wenchao.ren\.m2\repository\org\javassist\javassist\3.20.0-GA\javassist-3.20.0-GA.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-beans\4.3.16.RELEASE\spring-beans-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-core\4.3.16.RELEASE\spring-core-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-web\4.3.16.RELEASE\spring-web-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-aop\4.3.16.RELEASE\spring-aop-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-context\4.3.16.RELEASE\spring-context-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-expression\4.3.16.RELEASE\spring-expression-4.3.16.RELEASE.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-rpc\dubbo-rpc-injvm\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-registry\dubbo-registry-zookeeper\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-registry\dubbo-registry-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-cluster\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-remoting\dubbo-remoting-zookeeper\target\classes;C:\Users\wenchao.ren\.m2\repository\org\apache\zookeeper\zookeeper\3.4.9\zookeeper-3.4.9.jar;C:\Users\wenchao.ren\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\wenchao.ren\.m2\repository\org\slf4j\slf4j-log4j12\1.6.1\slf4j-log4j12-1.6.1.jar;C:\Users\wenchao.ren\.m2\repository\jline\jline\0.9.94\jline-0.9.94.jar;C:\Users\wenchao.ren\.m2\repository\io\netty\netty\3.10.5.Final\netty-3.10.5.Final.jar;C:\Users\wenchao.ren\.m2\repository\com\101tec\zkclient\0.2\zkclient-0.2.jar;C:\Users\wenchao.ren\.m2\repository\org\apache\curator\curator-framework\2.12.0\curator-framework-2.12.0.jar;C:\Users\wenchao.ren\.m2\repository\org\apache\curator\curator-client\2.12.0\curator-client-2.12.0.jar;C:\Users\wenchao.ren\.m2\repository\com\google\guava\guava\16.0.1\guava-16.0.1.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-registry\dubbo-registry-multicast\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-rpc\dubbo-rpc-dubbo\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-rpc\dubbo-rpc-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-remoting\dubbo-remoting-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-container\dubbo-container-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-remoting\dubbo-remoting-netty4\target\classes;C:\Users\wenchao.ren\.m2\repository\io\netty\netty-all\4.1.25.Final\netty-all-4.1.25.Final.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-serialization\dubbo-serialization-hessian2\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-serialization\dubbo-serialization-api\target\classes;C:\Users\wenchao.ren\.m2\repository\com\alibaba\fastjson\1.2.46\fastjson-1.2.46.jar;C:\Users\wenchao.ren\.m2\repository\com\esotericsoftware\kryo\4.0.1\kryo-4.0.1.jar;C:\Users\wenchao.ren\.m2\repository\com\esotericsoftware\reflectasm\1.11.3\reflectasm-1.11.3.jar;C:\Users\wenchao.ren\.m2\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;C:\Users\wenchao.ren\.m2\repository\com\esotericsoftware\minlog\1.3.0\minlog-1.3.0.jar;C:\Users\wenchao.ren\.m2\repository\de\javakaffee\kryo-serializers\0.42\kryo-serializers-0.42.jar;C:\Users\wenchao.ren\.m2\repository\de\ruedigermoeller\fst\2.48-jdk-6\fst-2.48-jdk-6.jar;C:\Users\wenchao.ren\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.8.6\jackson-core-2.8.6.jar;C:\Users\wenchao.ren\.m2\repository\com\cedarsoftware\java-util\1.9.0\java-util-1.9.0.jar;C:\Users\wenchao.ren\.m2\repository\com\cedarsoftware\json-io\2.5.1\json-io-2.5.1.jar;C:\Users\wenchao.ren\.m2\repository\com\alibaba\hessian-lite\3.2.3\hessian-lite-3.2.3.jar;C:\Users\wenchao.ren\.m2\repository\org\objenesis\objenesis\2.6\objenesis-2.6.jar;C:\Users\wenchao.ren\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\182.3684.40\lib\idea_rt.jar;C:\Users\wenchao.ren\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\182.3684.40\lib\rt\debugger-agent.jar
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:java.library.path=C:\Program Files\Java\jdk1.8.0_181\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\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;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files\Microsoft VS Code\bin;C:\WINDOWS\System32\OpenSSH\;C:\Users\wenchao.ren\AppData\Local\Microsoft\WindowsApps;;.
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=C:\Users\wenchao.ren\AppData\Local\Temp\
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 10
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:os.version=10.0
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:user.name=wenchao.ren
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:user.home=C:\Users\wenchao.ren
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Client environment:user.dir=E:\workstations\dubbo\incubator-dubbo
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@3dd69f5a
[20/08/18 11:43:22:022 CST] main-SendThread(127.0.0.1:2181) INFO zookeeper.ClientCnxn: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[20/08/18 11:43:22:022 CST] main-SendThread(127.0.0.1:2181) INFO zookeeper.ClientCnxn: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Register: dubbo://192.168.116.1:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=6872&side=provider&timestamp=1534779801013, dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:22:022 CST] main-SendThread(127.0.0.1:2181) INFO zookeeper.ClientCnxn: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x100001d37250002, negotiated timeout = 40000
[20/08/18 11:43:22:022 CST] main-EventThread INFO state.ConnectionStateManager: State change: CONNECTED
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Subscribe: provider://192.168.116.1:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=6872&side=provider&timestamp=1534779801013, dubbo version: , current host: 192.168.116.1
[20/08/18 11:43:22:022 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url provider://192.168.116.1:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=6872&side=provider&timestamp=1534779801013, urls: [empty://192.168.116.1:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=6872&side=provider&timestamp=1534779801013], dubbo version: , current host: 192.168.116.1

同时我们查看zk的话会发现:

1
2
3
4
5
6
7
8
9
10
[zk: localhost:2181(CONNECTED) 1] ls /dubbo
[org.apache.dubbo.demo.DemoService]
[zk: localhost:2181(CONNECTED) 2] ls /dubbo/org.apache.dubbo.demo.DemoService
[configurators, providers]
[zk: localhost:2181(CONNECTED) 3]
[zk: localhost:2181(CONNECTED) 3] ls /dubbo/org.apache.dubbo.demo.DemoService/providers
[dubbo%3A%2F%2F192.168.116.1%3A20880%2Forg.apache.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.2%26generic%3Dfalse%26interface%3Dorg.apache.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D6872%26side%3Dprovider%26timestamp%3D1534779801013]
[zk: localhost:2181(CONNECTED) 4]
[zk: localhost:2181(CONNECTED) 4] ls /dubbo/org.apache.dubbo.demo.DemoService/configurators
[]

当然此时我们仅仅启动了provider,并没有启动consumer

启动consumer

同样的修改dubbo-demo-consumer模块中的dubbo-demo-consumer.xml中的注册中心,使得consumer和provider连接同一个注册中心,
修改后的内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="demo-consumer"/>

<!-- use multicast registry center to discover service -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry protocol="zookeeper" address="localhost:2181"/>

<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>

</beans>

然后运行Consumer类,输出如下:

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
Connected to the target VM, address: '127.0.0.1:8434', transport: 'socket'
[20/08/18 11:49:40:040 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@551aa95a: startup date [Mon Aug 20 23:49:40 CST 2018]; root of context hierarchy
[20/08/18 11:49:40:040 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/dubbo-demo-consumer.xml]
[20/08/18 11:49:40:040 CST] main INFO logger.LoggerFactory: using logger: org.apache.dubbo.common.logger.log4j.Log4jLoggerAdapter
[20/08/18 11:49:41:041 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:defaultCompiler, try to find an extension(bean) of type java.lang.String, dubbo version: , current host: 10.254.0.157
[20/08/18 11:49:41:041 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:defaultCompiler, type:java.lang.String found, stop get bean., dubbo version: , current host: 10.254.0.157
[20/08/18 11:49:42:042 CST] main INFO imps.CuratorFrameworkImpl: Starting
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:host.name=c
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:java.version=1.8.0_181
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:java.vendor=Oracle Corporation
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:java.home=C:\Program Files\Java\jdk1.8.0_181\jre
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:java.class.path=C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-demo\dubbo-demo-consumer\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-demo\dubbo-demo-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-config\dubbo-config-spring\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-config\dubbo-config-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-monitor\dubbo-monitor-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-filter\dubbo-filter-validation\target\classes;C:\Users\wenchao.ren\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-filter\dubbo-filter-cache\target\classes;C:\Users\wenchao.ren\.m2\repository\javax\cache\cache-api\1.0.0\cache-api-1.0.0.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-bootstrap\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-common\target\classes;C:\Users\wenchao.ren\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\wenchao.ren\.m2\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;C:\Users\wenchao.ren\.m2\repository\org\javassist\javassist\3.20.0-GA\javassist-3.20.0-GA.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-beans\4.3.16.RELEASE\spring-beans-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-core\4.3.16.RELEASE\spring-core-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-web\4.3.16.RELEASE\spring-web-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-aop\4.3.16.RELEASE\spring-aop-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-context\4.3.16.RELEASE\spring-context-4.3.16.RELEASE.jar;C:\Users\wenchao.ren\.m2\repository\org\springframework\spring-expression\4.3.16.RELEASE\spring-expression-4.3.16.RELEASE.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-rpc\dubbo-rpc-injvm\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-registry\dubbo-registry-zookeeper\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-registry\dubbo-registry-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-cluster\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-remoting\dubbo-remoting-zookeeper\target\classes;C:\Users\wenchao.ren\.m2\repository\org\apache\zookeeper\zookeeper\3.4.9\zookeeper-3.4.9.jar;C:\Users\wenchao.ren\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\wenchao.ren\.m2\repository\org\slf4j\slf4j-log4j12\1.6.1\slf4j-log4j12-1.6.1.jar;C:\Users\wenchao.ren\.m2\repository\jline\jline\0.9.94\jline-0.9.94.jar;C:\Users\wenchao.ren\.m2\repository\io\netty\netty\3.10.5.Final\netty-3.10.5.Final.jar;C:\Users\wenchao.ren\.m2\repository\com\101tec\zkclient\0.2\zkclient-0.2.jar;C:\Users\wenchao.ren\.m2\repository\org\apache\curator\curator-framework\2.12.0\curator-framework-2.12.0.jar;C:\Users\wenchao.ren\.m2\repository\org\apache\curator\curator-client\2.12.0\curator-client-2.12.0.jar;C:\Users\wenchao.ren\.m2\repository\com\google\guava\guava\16.0.1\guava-16.0.1.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-registry\dubbo-registry-multicast\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-rpc\dubbo-rpc-dubbo\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-rpc\dubbo-rpc-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-remoting\dubbo-remoting-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-container\dubbo-container-api\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-remoting\dubbo-remoting-netty4\target\classes;C:\Users\wenchao.ren\.m2\repository\io\netty\netty-all\4.1.25.Final\netty-all-4.1.25.Final.jar;E:\workstations\dubbo\incubator-dubbo\dubbo-serialization\dubbo-serialization-hessian2\target\classes;E:\workstations\dubbo\incubator-dubbo\dubbo-serialization\dubbo-serialization-api\target\classes;C:\Users\wenchao.ren\.m2\repository\com\alibaba\fastjson\1.2.46\fastjson-1.2.46.jar;C:\Users\wenchao.ren\.m2\repository\com\esotericsoftware\kryo\4.0.1\kryo-4.0.1.jar;C:\Users\wenchao.ren\.m2\repository\com\esotericsoftware\reflectasm\1.11.3\reflectasm-1.11.3.jar;C:\Users\wenchao.ren\.m2\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;C:\Users\wenchao.ren\.m2\repository\com\esotericsoftware\minlog\1.3.0\minlog-1.3.0.jar;C:\Users\wenchao.ren\.m2\repository\de\javakaffee\kryo-serializers\0.42\kryo-serializers-0.42.jar;C:\Users\wenchao.ren\.m2\repository\de\ruedigermoeller\fst\2.48-jdk-6\fst-2.48-jdk-6.jar;C:\Users\wenchao.ren\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.8.6\jackson-core-2.8.6.jar;C:\Users\wenchao.ren\.m2\repository\com\cedarsoftware\java-util\1.9.0\java-util-1.9.0.jar;C:\Users\wenchao.ren\.m2\repository\com\cedarsoftware\json-io\2.5.1\json-io-2.5.1.jar;C:\Users\wenchao.ren\.m2\repository\com\alibaba\hessian-lite\3.2.3\hessian-lite-3.2.3.jar;C:\Users\wenchao.ren\.m2\repository\org\objenesis\objenesis\2.6\objenesis-2.6.jar;C:\Users\wenchao.ren\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\182.3684.40\lib\idea_rt.jar;C:\Users\wenchao.ren\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\182.3684.40\lib\rt\debugger-agent.jar
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:java.library.path=C:\Program Files\Java\jdk1.8.0_181\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\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;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files\Microsoft VS Code\bin;C:\WINDOWS\System32\OpenSSH\;C:\Users\wenchao.ren\AppData\Local\Microsoft\WindowsApps;;.
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=C:\Users\wenchao.ren\AppData\Local\Temp\
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 10
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:os.version=10.0
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:user.name=wenchao.ren
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:user.home=C:\Users\wenchao.ren
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Client environment:user.dir=E:\workstations\dubbo\incubator-dubbo
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@59b38691
[20/08/18 11:49:42:042 CST] main-SendThread(0:0:0:0:0:0:0:1:2181) INFO zookeeper.ClientCnxn: Opening socket connection to server 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
[20/08/18 11:49:42:042 CST] main-SendThread(0:0:0:0:0:0:0:1:2181) INFO zookeeper.ClientCnxn: Socket connection established to 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181, initiating session
[20/08/18 11:49:42:042 CST] main-SendThread(0:0:0:0:0:0:0:1:2181) INFO zookeeper.ClientCnxn: Session establishment complete on server 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181, sessionid = 0x100001d37250004, negotiated timeout = 40000
[20/08/18 11:49:42:042 CST] main-EventThread INFO state.ConnectionStateManager: State change: CONNECTED
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Register: consumer://10.254.0.157/org.apache.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.2&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=2720&qos.port=33333&side=consumer&timestamp=1534780181816, dubbo version: , current host: 10.254.0.157
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Subscribe: consumer://10.254.0.157/org.apache.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.2&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=2720&qos.port=33333&side=consumer&timestamp=1534780181816, dubbo version: , current host: 10.254.0.157
[20/08/18 11:49:42:042 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://10.254.0.157/org.apache.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.2&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=2720&qos.port=33333&side=consumer&timestamp=1534780181816, urls: [dubbo://192.168.116.1:20880/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=6872&side=provider&timestamp=1534779801013, empty://10.254.0.157/org.apache.dubbo.demo.DemoService?application=demo-consumer&category=configurators&check=false&dubbo=2.0.2&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=2720&qos.port=33333&side=consumer&timestamp=1534780181816, empty://10.254.0.157/org.apache.dubbo.demo.DemoService?application=demo-consumer&category=routers&check=false&dubbo=2.0.2&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=2720&qos.port=33333&side=consumer&timestamp=1534780181816], dubbo version: , current host: 10.254.0.157
[20/08/18 11:49:43:043 CST] main INFO transport.AbstractClient: [DUBBO] Successed connect to server /192.168.116.1:20880 from NettyClient 10.254.0.157 using dubbo version , channel is NettyChannel [channel=[id: 0x5a2b2c61, L:/192.168.116.1:8464 - R:/192.168.116.1:20880]], dubbo version: , current host: 10.254.0.157
[20/08/18 11:49:43:043 CST] main INFO transport.AbstractClient: [DUBBO] Start NettyClient c/10.254.0.157 connect to the server /192.168.116.1:20880, dubbo version: , current host: 10.254.0.157
[20/08/18 11:49:43:043 CST] main INFO config.AbstractConfig: [DUBBO] Refer dubbo service org.apache.dubbo.demo.DemoService from url zookeeper://localhost:2181/org.apache.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=2720&qos.port=33333&register.ip=10.254.0.157&remote.timestamp=1534779801013&side=consumer&timestamp=1534780181816, dubbo version: , current host: 10.254.0.157
Hello world, response from provider: 192.168.116.1:20880
Hello world, response from provider: 192.168.116.1:20880
Hello world, response from provider: 192.168.116.1:20880

此时我们看zk节点:

1
2
3
4
5
6
7
8
9
10
11
[zk: localhost:2181(CONNECTED) 7] ls /dubbo/org.apache.dubbo.demo.DemoService
[consumers, configurators, routers, providers]
[zk: localhost:2181(CONNECTED) 8] ls /dubbo/org.apache.dubbo.demo.DemoService/consumers
[consumer%3A%2F%2F10.254.0.157%2Forg.apache.dubbo.demo.DemoService%3Fapplication%3Ddemo-consumer%26category%3Dconsumers%26check%3Dfalse%26dubbo%3D2.0.2%26interface%3Dorg.apache.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D2720%26qos.port%3D33333%26side%3Dconsumer%26timestamp%3D1534780181816]
[zk: localhost:2181(CONNECTED) 9] ls /dubbo/org.apache.dubbo.demo.DemoService/configurators
[]
[zk: localhost:2181(CONNECTED) 10] ls /dubbo/org.apache.dubbo.demo.DemoService/routers
[]
[zk: localhost:2181(CONNECTED) 11] ls /dubbo/org.apache.dubbo.demo.DemoService/providers
[dubbo%3A%2F%2F192.168.116.1%3A20880%2Forg.apache.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.2%26generic%3Dfalse%26interface%3Dorg.apache.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D6872%26side%3Dprovider%26timestamp%3D1534779801013]
[zk: localhost:2181(CONNECTED) 12]

可以看到多了consumerrouters节点。

同时consumer端不断输出Hello world, response from provider: 192.168.116.1:20880的日志,说明我们的环境以及搭建完成了。

参考文章

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×