SpringBoot 2.0 + Apache Dubbo 2.7.3 最新版整合方案

∥☆過路亽.° 提交于 2019-12-01 01:17:08

前言

2018年2月16日,Apache Dubbo 加入 Apache 基金会孵化器。2019年5月16日,Apache 软件基金会董事会决议通过了 Apache Dubbo 的毕业申请,这意味着 Apache Dubbo 正式成为 Apache 的顶级项目。

配置

Dubbo 捐精给 Apache 以后,也就意味着之前 Dubbo 以及 spring-boot-starter 坐标也变了,当然之前的还可以用,但是要使用新版本的小伙伴建议看这里。

Zookeeper 注册中心版

pom.xml 引入坐标:

<!-- 升级 apache dubbo -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.3</version>
</dependency>
<!-- Zookeeper -->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.5.3-beta</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.2.0</version>
</dependency>
<!-- 最新 starter -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.3</version>
</dependency>

配置文件:

dubbo.application.id=mail
dubbo.application.name=mail
dubbo.registry.address=zookeeper://106.13.122.117:2181
dubbo.provider.threads=10
dubbo.provider.threadpool=fixed
dubbo.provider.loadbalance=roundrobin
dubbo.server=true
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1

启动类:

@SpringBootApplication
//必须配置包扫描、否则Dubbo无法注册服务,实现类加扫描也不行,日狗不是
@DubboComponentScan(basePackages = "com.itstyle.mail.service.impl")
public class Application  {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);
    
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
        logger.info("邮件服务项目启动");
    }
}

提供者接口实现:

# 引入 apache 包、之前的还可以用,不过已过时
import org.apache.dubbo.config.annotation.Service;

@Service(version = "1.0.0")
public class MailServiceImpl implements IMailService {

}

消费引用:

import org.apache.dubbo.config.annotation.Reference;

@RestController
@RequestMapping("/mail")
public class mailController {

    @Reference(version = "1.0.0")
    private IMailService mailService;

}

Nacos 注册中心版

<!-- 升级 apache dubbo -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.3</version>
</dependency>
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-registry-nacos</artifactId>
    <version>2.7.3</version>
</dependency>
<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>1.1.3</version>
</dependency>

配置文件:

nacos.config.server-addr=47.104.197.9:8848
dubbo.application.name = spring-boot-mail
dubbo.registry.address = nacos://47.104.197.9:8848
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1

启动类:

@SpringBootApplication
//必须配置包扫描、否则Dubbo无法注册服务
@EnableDubbo(scanBasePackages  = "com.itstyle.mail.service.impl")
public class Application  {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);
    
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
        logger.info("邮件服务项目启动");
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!