[最新] SpringBoot2.X整合Dubbo2.7带注册中心版本

前言

关于dubbo整合springboot的案例太多了,但绝大部分都过时了。

总体来归类有两种整合方式:

  1. 利用阿里巴巴提供的dubbo-spring-boot-starter
  2. 利用Apache提供的 dubbo-spring-boot-starter

因为dubbo已经开源给Apache了,并且阿里这个项目已经不再更新。所以建议大家还是以Apache这个为准,千万别搞错了。pom.xmlgroupId一定要是org.apache.dubbo,不是com.alibaba.spring.boot

本demo使用Apache官方提供的 dubbo-spring-boot-starter 中提供的注解:@DubboService、@DubboReference 整合简单案例 因为官方的demo是基于无注册中心的 我自己通过找资料补齐了带有注册中心案例的demo,方便大家参考。 大家可以选择直接克隆代码编译运行 Github:https://github.com/yunnasheng/springboot-dubbo-zk.git

或者动手搭建一个demo(推荐,自己手敲后记忆更深刻)

本项目配置如下

  • 可用的Linux下zk集群环境,没环境的可参照文章搭建:zookeeper集群配置实战
  • JDK 1.8
  • Apache dubbo 2.7.8
  • ZooKeeper 3.6.2
  • SpringBoot 2.3.0.RELEASE

开始

项目结构如下

  • 父模块:springboot-dubbo-zk
  • 接口定义模块:springboot-dubbo-zk-api
  • 服务提供者:springboot-dubbo-zk-provider
  • 服务消费者: springboot-dubbo-zk-consumer

配置

api 模块
  1. 在api模块定义一个service接口
public interface GreetingService {
    String sayHi(String name);
}
provider 模块
  1. 在provider模块配置pom.xml
       <!--   Spring Boot      -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>
        <!--   Apache Dubbo Starter    -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!--  Dubbo   -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.8</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--    对zk的简单操作    -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <!--   分布式锁、分布式栅栏等工具     -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>
  1. 编写启动类
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class ProviderApp {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ProviderApp.class)
                .web(WebApplicationType.SERVLET)
                .run(args);
    }
}
  1. 实现API接口
import com.lb.springboot.service.GreetingService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

/**
 * 功能描述:GreetingServiceImpl <br/>
 *
 * @author lb
 * @date: 2020-12-30 17:14<br/>
 * @since JDK 1.8
 */
@DubboService(version = "1.0")
public class GreetingServiceImpl implements GreetingService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final Random costTimeRandom = new Random();

    @Override
    public String sayHi(String name) {
        await();
        return String.format("Hi, %s",name);
    }

    private void await() {
        try {
            long timeInMillisToWait = costTimeRandom.nextInt(500);
            Thread.sleep(timeInMillisToWait);
            logger.info("execution time : " + timeInMillisToWait + " ms.");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

@DubboService(version = "1.0")是Apache提供的注解,这个version是其中的一项配置,可以用于我们的接口版本设置,服务端提供多个版本,可以实现客户端切换版本,在业务上不去影响原有版本。该注解支持多项配置,有兴趣的可以下载源码读一下,这里就不多介绍了。

  1. 配置application.properties
spring.application.name=provider-app-name

# 扫描包路径
dubbo.scan.base-packages=com.lb.springboot.service.impl

# dubbo应用配置,可在监控中心看到
dubbo.application.id=provider-ws-id
dubbo.application.name=provider-ws-name

# 协议指定为dubbo,也支持rest
dubbo.protocol.name=dubbo
# 协议端口可以自定义
dubbo.protocol.port=10190 
# 注册中心地址
dubbo.registry.id=zookeeper
# 单节点的zookeeper可以只配置zookeeper://10.211.55.5:2182
# 多个服务节点可以加?backup= 以逗号分隔即可
dubbo.registry.address=zookeeper://10.211.55.5:2182?backup=10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185
  1. 启动provider

直接右键运行ProviderApp的main方法即可。 控制台打印如下:

/Library/Java/JavaVirtualrepository/io/netty/netty/3.7.0.Final/netty-3.7.0.Final.jar:/Users/lb/work/mvn-repository/com/google/guava/guava/16.0.1/guava-16.0.1.jar:/Users/lb/work/mvn-repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/Users/lb/work/mvn-repository/org/apache/curator/curator-recipes/2.12.0/curator-recipes-2.12.0.jar com.lb.springboot.ProviderApp
log4j:WARN No appenders could be found for logger (org.apache.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2020-12-31 14:45:34.740  INFO 69009 --- [           main] d.s.b.c.e.WelcomeLogoApplicationListener : 

 :: Dubbo Spring Boot (v2.7.8) : https://github.com/apache/dubbo-spring-boot-project
 :: Dubbo (v2.7.8) : https://github.com/apache/dubbo
 :: Discuss group : dev@dubbo.apache.org

2020-12-31 14:45:34.744  INFO 69009 --- [           main] e.OverrideDubboConfigApplicationListener : Dubbo Config was overridden by externalized configuration {dubbo.application.id=provider-ws-id, dubbo.application.name=provider-ws-name, dubbo.application.qos-enable=false, dubbo.config.multiple=true, dubbo.protocol.name=dubbo, dubbo.protocol.port=10190, dubbo.registry.address=zookeeper://10.211.55.5:2182?backup=10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185, dubbo.registry.id=zookeeper, dubbo.scan.base-packages=com.lb.springboot.service.impl}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.1)

2020-12-31 14:45:34.807  INFO 69009 --- [           main] com.lb.springboot.ProviderApp            : Starting ProviderApp using Java 1.8.0_211 on lbdeMacBook-Pro.local with PID 69009 (/Users/lb/work/github-workspace/springboot-dubbo-zk/springboot-dubbo-zk-provider/target/classes started by lb in /Users/lb/work/github-workspace/springboot-dubbo-zk)
2020-12-31 14:45:34.807  INFO 69009 --- [           main] com.lb.springboot.ProviderApp            : No active profile set, falling back to default profiles: default
2020-12-31 14:45:35.286  INFO 69009 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [referenceAnnotationBeanPostProcessor] has been registered.
2020-12-31 14:45:35.286  INFO 69009 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigAliasPostProcessor]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [dubboConfigAliasPostProcessor] has been registered.
2020-12-31 14:45:35.287  INFO 69009 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.context.DubboLifecycleComponentApplicationListener]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [dubboLifecycleComponentApplicationListener] has been registered.
2020-12-31 14:45:35.288  INFO 69009 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [dubboBootstrapApplicationListener] has been registered.
2020-12-31 14:45:35.288  INFO 69009 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.beans.factory.config.DubboConfigDefaultPropertyValueBeanPostProcessor]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [dubboConfigDefaultPropertyValueBeanPostProcessor] has been registered.
2020-12-31 14:45:35.351  INFO 69009 --- [           main] .b.f.a.ConfigurationBeanBindingRegistrar : The configuration bean definition [name : provider-ws-id, content : Root bean: class [org.apache.dubbo.config.ApplicationConfig]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] has been registered.
2020-12-31 14:45:35.351  INFO 69009 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [com.alibaba.spring.beans.factory.annotation.ConfigurationBeanBindingPostProcessor]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [configurationBeanBindingPostProcessor] has been registered.
2020-12-31 14:45:35.351  INFO 69009 --- [           main] .b.f.a.ConfigurationBeanBindingRegistrar : The configuration bean definition [name : zookeeper, content : Root bean: class [org.apache.dubbo.config.RegistryConfig]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] has been registered.
2020-12-31 14:45:35.351  INFO 69009 --- [           main] .b.f.a.ConfigurationBeanBindingRegistrar : The configuration bean definition [name : org.apache.dubbo.config.ProtocolConfig#0, content : Root bean: class [org.apache.dubbo.config.ProtocolConfig]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] has been registered.
2020-12-31 14:45:35.432  INFO 69009 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [dubboBootstrapApplicationListener] has been registered.
2020-12-31 14:45:35.451  INFO 69009 --- [           main] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition 'org.apache.dubbo.spring.boot.autoconfigure.DubboRelaxedBinding2AutoConfiguration' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2020-12-31 14:45:35.451  INFO 69009 --- [           main] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition 'org.apache.dubbo.spring.boot.autoconfigure.DubboAutoConfiguration' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2020-12-31 14:45:35.766  INFO 69009 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-12-31 14:45:35.773  INFO 69009 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-31 14:45:35.774  INFO 69009 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2020-12-31 14:45:35.828  INFO 69009 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-31 14:45:35.828  INFO 69009 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 984 ms
2020-12-31 14:45:36.117  INFO 69009 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-31 14:45:36.416  INFO 69009 --- [           main] .a.ConfigurationBeanBindingPostProcessor : The configuration bean [<dubbo:application name="provider-ws-name" qosEnable="false" hostname="lbdeMacBook-Pro.local" />] have been binding by the configuration properties [{id=provider-ws-id, name=provider-ws-name, qos-enable=false}]
2020-12-31 14:45:36.422  INFO 69009 --- [           main] .a.ConfigurationBeanBindingPostProcessor : The configuration bean [<dubbo:registry address="zookeeper://10.211.55.5:2182?backup=10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185" protocol="zookeeper" port="2182" />] have been binding by the configuration properties [{id=zookeeper, address=zookeeper://10.211.55.5:2182?backup=10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185}]
2020-12-31 14:45:36.427  INFO 69009 --- [           main] .a.ConfigurationBeanBindingPostProcessor : The configuration bean [<dubbo:protocol name="dubbo" port="10190" />] have been binding by the configuration properties [{name=dubbo, port=10190}]ework/spring-core/5.3.2/spring-core-5.3.2.jar:/Users/lb/work/mvn-repository/org/springframework/spring-jcl/5.3.2/spring-jcl-5.3.2.jar:/U2.12.0.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar
2020-12-31 14:45:36.689  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.library.path=/Users/lb/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
2020-12-31 14:45:36.689  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.io.tmpdir=/var/folders/60/5x2rgklx0gz75pm7y9nywgbc0000gn/T/
2020-12-31 14:45:36.689  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.compiler=<NA>
2020-12-31 14:45:36.689  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.name=Mac OS X
2020-12-31 14:45:36.689  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.arch=x86_64
2020-12-31 14:45:36.689  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.version=10.15.7
2020-12-31 14:45:36.690  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.name=lb
2020-12-31 14:45:36.690  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.home=/Users/lb
2020-12-31 14:45:36.690  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.dir=/Users/lb/work/github-workspace/springboot-dubbo-zk
2020-12-31 14:45:36.690  INFO 69009 --- [           main] org.apache.zookeeper.ZooKeeper           : Initiating client connection, connectString=10.211.55.5:2182,10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@7dff6d05
2020-12-31 14:45:36.698  INFO 69009 --- [.211.55.5:2182)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server 10.211.55.5/10.211.55.5:2182. Will not attempt to authenticate using SASL (unknown error)
2020-12-31 14:45:36.703  INFO 69009 --- [.211.55.5:2182)] org.apache.zookeeper.ClientCnxn          : Socket connection established to 10.211.55.5/10.211.55.5:2182, initiating session
2020-12-31 14:45:36.710  INFO 69009 --- [.211.55.5:2182)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server 10.211.55.5/10.211.55.5:2182, sessionid = 0x1000069d498001c, negotiated timeout = 40000
2020-12-31 14:45:36.715  INFO 69009 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager     : State change: CONNECTED
2020-12-31 14:45:37.221  INFO 69009 --- [           main] com.lb.springboot.ProviderApp            : Started ProviderApp in 2.705 seconds (JVM running for 3.147)

启动成功后,我们可以里使用dubbo提供的telnet 命令

连接dubbo提供者

 telnet localhost 10190
Trying ::1...
Connected to localhost.
Escape character is '^]'.

dubbo>

输入help命令查看dubbo支持哪些命令

dubbo>help
Please input "help [command]" show detail.
 status [-l]                      - Show status.
 shutdown [-t <milliseconds>]     - Shutdown Dubbo Application.
 pwd                              - Print working default service.
 trace [service] [method] [times] - Trace the service.
 exit                             - Exit the telnet.
 help [command]                   - Show help.
 invoke [service.]method(args)    - Invoke the service method.
 count [service] [method] [times] - Count the service.
 clear [lines]                    - Clear screen.
 ls [-l] [service]                - List services and methods.
 log level                        - Change log level or show log
 select [index]                   - Select the index of the method you want to invoke.
 ps [-l] [port]                   - Print server ports and connections.
 cd [service]                     - Change default service.
dubbo>

可以输入ls -l查看发布了哪些dubbo服务

dubbo>ls -l
PROVIDER:
com.lb.springboot.service.GreetingService:1.0 ->  published: Y

查看指定接口有哪些方法

dubbo>ls com.lb.springboot.service.GreetingService
com.lb.springboot.service.GreetingService:1.0 (as provider):
	sayHi

还支持使用invoke命令执行dubbo接口方法,语法为 invoke<接口全限定名>.<方法名>

dubbo>invoke com.lb.springboot.service.GreetingService.sayHi("zhangSan")
Use default service com.lb.springboot.service.GreetingService.
result: "Hi, zhangSan"
elapsed: 91 ms.

更多的命令操作就不在这里演示了,大家可以看一下官方文档。

接下来我们配置消费端

consumer 模块
  1. 配置pom.xml 与provider的一样
  2. 配置application.propertie
server.port=8002
spring.application.name=consumer-service

# dubbo应用配置,可在监控中心看到
dubbo.application.id=consumer-ws-id
dubbo.application.name=consumer-ws-name

# 协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=10190

## Dubbo 注册中心
dubbo.registry.address=zookeeper://10.211.55.5:2182?backup=10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185
dubbo.registry.timeout=30000
  1. 编写启动类
import com.lb.springboot.service.GreetingService;
import com.lb.springboot.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.UUID;

@EnableAutoConfiguration
public class ConsumerApp {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 指定URL调用DUBBO付服务
     * 生产环境不推荐使用
     */
    @DubboReference(version = "1.0",url = "dubbo://127.0.0.1:10190")
    private GreetingService greetingService;

    /**
     * 通过ZK注册中心访问DUBBO服务
	 * 指定版本,指定注册中心
     */
    @DubboReference(version = "1.0",registry = "${dubbo.registry.address}")
    private UserService userService;

    public static void main(String[] args) {
		// .run()执行springboot
		// .close()执行完毕销毁spring容器
        SpringApplication.run(ConsumerApp.class).close();
    }

    @Bean
    public ApplicationRunner greetingTest() {
        return args -> logger.info("通过指定URL调用DUBBO服务-SayHi:{}",greetingService.sayHi("yunnasheng"));

    }

    @Bean
    public ApplicationRunner getUserTest() {
        return args -> logger.info("通过注册中心调用DUBBO服务-getUser:{}",userService.get(UUID.randomUUID().toString()));
    }
}

ApplicationRunner会在ConsumerApp启动时自动执行,所以我们可以打印日志看执行结果。

2020-12-31 15:43:06.370  INFO 70299 --- [           main] d.s.b.c.e.WelcomeLogoApplicationListener : 

 :: Dubbo Spring Boot (v2.7.8) : https://github.com/apache/dubbo-spring-boot-project
 :: Dubbo (v2.7.8) : https://github.com/apache/dubbo
 :: Discuss group : dev@dubbo.apache.org

2020-12-31 15:43:06.374  INFO 70299 --- [           main] e.OverrideDubboConfigApplicationListener : Dubbo Config was overridden by externalized configuration {dubbo.application.id=consumer-ws-id, dubbo.application.name=consumer-ws-name, dubbo.application.qos-enable=false, dubbo.config.multiple=true, dubbo.protocol.name=dubbo, dubbo.protocol.port=10190, dubbo.registry.address=zookeeper://10.211.55.5:2182?backup=10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185, dubbo.registry.timeout=30000}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.1)

2020-12-31 15:43:06.420  INFO 70299 --- [           main] com.lb.springboot.ConsumerApp            : Starting ConsumerApp using Java 1.8.0_211 on lbdeMacBook-Pro.local with PID 70299 (/Users/lb/work/github-workspace/springboot-dubbo-zk/springboot-dubbo-zk-consumer/target/classes started by lb in /Users/lb/work/github-workspace/springboot-dubbo-zk)
2020-12-31 15:43:06.420  INFO 70299 --- [           main] com.lb.springboot.ConsumerApp            : No active profile set, falling back to default profiles: default
2020-12-31 15:43:06.684  INFO 70299 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [referenceAnnotationBeanPostProcessor] has been registered.
2020-12-31 15:43:06.684  INFO 70299 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigAliasPostProcessor]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [dubboConfigAliasPostProcessor] has been registered.
2020-12-31 15:43:06.685  INFO 70299 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.context.DubboLifecycleComponentApplicationListener]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [dubboLifecycleComponentApplicationListener] has been registered.
2020-12-31 15:43:06.685  INFO 70299 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [dubboBootstrapApplicationListener] has been registered.
2020-12-31 15:43:06.685  INFO 70299 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.beans.factory.config.DubboConfigDefaultPropertyValueBeanPostProcessor]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [dubboConfigDefaultPropertyValueBeanPostProcessor] has been registered.
2020-12-31 15:43:06.753  INFO 70299 --- [           main] .b.f.a.ConfigurationBeanBindingRegistrar : The configuration bean definition [name : consumer-ws-id, content : Root bean: class [org.apache.dubbo.config.ApplicationConfig]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] has been registered.
2020-12-31 15:43:06.753  INFO 70299 --- [           main] com.alibaba.spring.util.BeanRegistrar    : The Infrastructure bean definition [Root bean: class [com.alibaba.spring.beans.factory.annotation.ConfigurationBeanBindingPostProcessor]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=nullwith name [configurationBeanBindingPostProcessor] has been registered.
2020-12-31 15:43:06.753  INFO 70299 --- [           main] .b.f.a.ConfigurationBeanBindingRegistrar : The configuration bean definition [name : org.apache.dubbo.config.RegistryConfig#0, content : Root bean: class [org.apache.dubbo.config.RegistryConfig]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] has been registered.
2020-12-31 15:43:06.753  INFO 70299 --- [           main] .b.f.a.ConfigurationBeanBindingRegistrar : The configuration bean definition [name : org.apache.dubbo.config.ProtocolConfig#0, content : Root bean: class [org.apache.dubbo.config.ProtocolConfig]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] has been registered.
2020-12-31 15:43:06.795  INFO 70299 --- [           main] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition 'org.apache.dubbo.spring.boot.autoconfigure.DubboAutoConfiguration' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2020-12-31 15:43:07.059  INFO 70299 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8002 (http)
2020-12-31 15:43:07.066  INFO 70299 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-31 15:43:07.066  INFO 70299 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2020-12-31 15:43:07.111  INFO 70299 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-31 15:43:07.111  INFO 70299 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 672 ms
2020-12-31 15:43:07.305  INFO 70299 --- [           main] .a.ConfigurationBeanBindingPostProcessor : The configuration bean [<dubbo:application qosEnable="false" hostname="lbdeMacBook-Pro.local" name="consumer-ws-name" />] have been binding by the configuration properties [{id=consumer-ws-id, name=consumer-ws-name, qos-enable=false}]
2020-12-31 15:43:07.314  INFO 70299 --- [           main] .a.ConfigurationBeanBindingPostProcessor : The configuration bean [<dubbo:registry timeout="30000" address="zookeeper://10.211.55.5:2182?backup=10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185" protocol="zookeeper" port="2182" />] have been binding by the configuration properties [{address=zookeeper://10.211.55.5:2182?backup=10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185, timeout=30000}]
2020-12-31 15:43:07.319  INFO 70299 --- [           main] .a.ConfigurationBeanBindingPostProcessor : The configuration bean [<dubbo:protocol name="dubbo" port="10190" />] have been binding by the configuration properties [{name=dubbo, port=10190}]
2020-12-31 15:43:07.320  INFO 70299 --- [           main] o.a.d.c.s.b.f.a.ReferenceBeanBuilder     : The configBean[type:ReferenceBean] has been built.
2020-12-31 15:43:07.372  INFO 70299 --- [           main] o.a.c.f.imps.CuratorFrameworkImpl        : Starting
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:host.name=localhost
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.version=1.8.0_211
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.vendor=Oracle Corporation
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.class.path=/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/deploy.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.library.path=/Users/lb/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.io.tmpdir=/var/folders/60/5x2rgklx0gz75pm7y9nywgbc0000gn/T/
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.compiler=<NA>
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.name=Mac OS X
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.arch=x86_64
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.version=10.15.7
2020-12-31 15:43:07.377  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.name=lb
2020-12-31 15:43:07.378  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.home=/Users/lb
2020-12-31 15:43:07.378  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.dir=/Users/lb/work/github-workspace/springboot-dubbo-zk
2020-12-31 15:43:07.378  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Initiating client connection, connectString=10.211.55.5:2182,10.211.55.5:2183,10.211.55.5:2184,10.211.55.5:2185 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@73877e19
2020-12-31 15:43:07.391  INFO 70299 --- [.211.55.5:2185)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server 10.211.55.5/10.211.55.5:2185. Will not attempt to authenticate using SASL (unknown error)
2020-12-31 15:43:07.410  INFO 70299 --- [.211.55.5:2185)] org.apache.zookeeper.ClientCnxn          : Socket connection established to 10.211.55.5/10.211.55.5:2185, initiating session
2020-12-31 15:43:07.418  INFO 70299 --- [.211.55.5:2185)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server 10.211.55.5/10.211.55.5:2185, sessionid = 0x4000069fe3c0012, negotiated timeout = 40000
2020-12-31 15:43:07.425  INFO 70299 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager     : State change: CONNECTED
2020-12-31 15:43:07.737  INFO 70299 --- [           main] o.a.d.c.s.b.f.a.ReferenceBeanBuilder     : The configBean[type:ReferenceBean] has been built.
2020-12-31 15:43:08.026  INFO 70299 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-31 15:43:08.213  INFO 70299 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8002 (http) with context path ''
2020-12-31 15:43:08.222  INFO 70299 --- [           main] com.lb.springboot.ConsumerApp            : Started ConsumerApp in 2.045 seconds (JVM running for 2.431)
// 关键日志
2020-12-31 15:43:08.276  INFO 70299 --- [           main] com.lb.springboot.ConsumerApp            : 通过指定URL调用DUBBO服务-SayHi:Hi, lb
2020-12-31 15:43:08.282  INFO 70299 --- [           main] com.lb.springboot.ConsumerApp            : 通过注册中心调用DUBBO服务-getUser:287be186-3e53-4ae7-aafc-4ec151fd7a5c
2020-12-31 15:43:08.287  INFO 70299 --- [tor-Framework-0] o.a.c.f.imps.CuratorFrameworkImpl        : backgroundOperationsLoop exiting
2020-12-31 15:43:08.290  INFO 70299 --- [           main] org.apache.zookeeper.ZooKeeper           : Session: 0x4000069fe3c0012 closed
2020-12-31 15:43:08.290  INFO 70299 --- [ain-EventThread] org.apache.zookeeper.ClientCnxn          : EventThread shut down for session: 0x4000069fe3c0012
2020-12-31 15:43:08.305  INFO 70299 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-12-31 15:43:08.306  INFO 70299 --- [           main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy1@7a485a36 was destroying!
2020-12-31 15:43:08.306  INFO 70299 --- [           main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy0@5cf3157b was destroying!
2020-12-31 15:43:08.306  INFO 70299 --- [           main] f.a.ReferenceAnnotationBeanPostProcessor : class org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor was destroying!

Process finished with exit code 0

我们再看一下provider的控制台

2020-12-31 15:43:08.267  INFO 69009 --- [10190-thread-20] c.l.s.service.impl.GreetingServiceImpl   : execution time : 4 ms.
2020-12-31 15:43:08.281  INFO 69009 --- [10190-thread-21] c.l.s.service.impl.UserServiceImpl       : User get id: 287be186-3e53-4ae7-aafc-4ec151fd7a5c

收到打印结果,说明没有问题 。

结语

要学习更多dubbo知识 推荐阅读Dubbo官方文档:http://dubbo.apache.org/

ApacheDubbo官方案例:https://github.com/apache/dubbo-spring-boot-project

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值