美文网首页
SpringBoot 整合Dubbo

SpringBoot 整合Dubbo

作者: 东方不喵 | 来源:发表于2019-02-09 20:12 被阅读81次

SpringBoot 2.0 + Dubbo 2.6

Provider端
Consumer端-1
Consumer端-2

  1. 三个项目引入Maven依赖
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>
  1. 配置注册中心
    官方文档:
    http://dubbo.apache.org/zh-cn/docs/user/references/registry/redis.html

如果使用Redis配置方式,需要另外引入 jar

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
  1. 配置
provider:

配置Yaml

dubbo:
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: zookeeper://127.0.0.1:2181
  application:
    name: dubbo-provider-demo1
#  scan:
#    base-packages: com.example.dubbo.services.impl

注解式配置包扫描

@Configuration
@DubboComponentScan(basePackages = "com.example.dubbo.services.impl")
public class DubboConfig {
}

Consumer

配置Yaml

dubbo:
  registry:
    address: zookeeper://127.0.0.1:2181
  application:
    name: dubbo-consumer-demo1
  consumer:
    timeout: 3000
  1. 使用
    Dubbo接口的索引是类全路径,所以只是类名相同,则无效。
com.example.dubbo.services.TestService

创建Provider:
使用注解
@com.alibaba.dubbo.config.annotation.Service(timeout = 500)

package com.example.dubbo.services.impl;

import com.example.dubbo.local.services.TestLocalService;
import com.example.dubbo.services.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@com.alibaba.dubbo.config.annotation.Service(timeout = 500)
public class TestServiceImpl implements TestService {

    @Autowired
    private TestLocalService testLocalService;

    @Override
    public String getMessage(String message) {
        return testLocalService.test() + "\t" + message;
    }
}

使用 com.alibaba.dubbo.config.annotation.Reference 进行引用

@RestController
public class TestClientController {

    @Reference
    private TestService testService;

    @GetMapping({"", "client"})
    public String test() {
        return testService.getMessage("-TestClientController");
    }
}

到此就完成了Dubbo的基本配置与使用

问题:

在Yaml文件中,有下列配置项:(此配置项是否有效?

dubbo:
  scan:
    base-packages: com.example.dubbo.services.impl

该配置项对应源码
com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration

    /**
     * Creates {@link ServiceAnnotationBeanPostProcessor} Bean
     *
     * @param environment {@link Environment} Bean
     * @return {@link ServiceAnnotationBeanPostProcessor}
     */
    @ConditionalOnProperty(name = BASE_PACKAGES_PROPERTY_NAME)
    @ConditionalOnClass(ConfigurationPropertySources.class)
    @Bean
    public ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor(Environment environment) {
        Set<String> packagesToScan = environment.getProperty(BASE_PACKAGES_PROPERTY_NAME, Set.class, emptySet());
        return new ServiceAnnotationBeanPostProcessor(packagesToScan);
    }

通过 org.springframework.boot.context.properties.source.ConfigurationPropertySources 断点,可以确认是具备的。
也就是:


源码截图.jpg

由上图也可以看出参数都是具备,但并不会执行该处的代码。

com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration
注入@Reference的代码却是有效的

    /**
     * Creates {@link ReferenceAnnotationBeanPostProcessor} Bean if Absent
     *
     * @return {@link ReferenceAnnotationBeanPostProcessor}
     */
    @ConditionalOnMissingBean
    @Bean(name = ReferenceAnnotationBeanPostProcessor.BEAN_NAME)
    public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor() {
        return new ReferenceAnnotationBeanPostProcessor();
    }

而当使用注解式的时候

@Configuration
@DubboComponentScan(basePackages = "com.example.dubbo.services.impl")
public class DubboConfig {
}

则上面那段代码中的类
com.alibaba.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor
被使用了。
该实验似乎证明了上面 scanbasePackages 的Yaml配置是无效的。但无效却并没有被注释过期。

所有有没有大佬可以解释一下上述线现象。

相关文章

  • io.dubbo.springboot版本不兼容dubbo-2.

    遇到问题:在dubbo整合springboot的时候,使用io.dubbo.springboot的jar包,配合的...

  • SpringBoot 整合 Dubbo错误收集

    SpringBoot 整合 Dubbo CuratorFrameworkFactory找不到 错误信息:java....

  • spring boot 整合dubbo

    #项目介绍 Springboot 整合 Dubbo/ZooKeeper 码云地址:https://gitee.co...

  • SpringBoot整合dubbo

    1、添加依赖 2、生产者配置文件 3、消费者配置文件 4、定义service接口 5、生产者实现service接口...

  • SpringBoot整合dubbo

    首先创建一个多模块Maven项目,再创建一个生产者子项目和一个消费者子项目。父模块Maven POM文件如下: 生...

  • SpringBoot Dubbo 整合

    官方参考文档http://dubbo.apache.org/zh-cn/docs/user/configurati...

  • SpringBoot整合dubbo

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,...

  • SpringBoot整合Dubbo

    前期准备:下载安装zookeeper作为服务注册中心 安装步骤 解压 将zoo_sample.cfg文件名修改为z...

  • springboot整合dubbo

    导入依赖 服务调用者和服务发布者都需要引入该依赖 该依赖不定时更新,可以关注最新版本http://maven.al...

  • Springboot整合dubbo

    要的工具,zookeeper,dubbo首先Pom,yaml配置一下 然后下载zookeeper,dubbo前往g...

网友评论

      本文标题:SpringBoot 整合Dubbo

      本文链接:https://www.haomeiwen.com/subject/yqjzsqtx.html