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

2019-12-05 22:40:49 浏览数 (1)

前言

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

配置

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

Zookeeper 注册中心版

pom.xml 引入坐标:

代码语言:javascript复制
<!-- 升级 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>

配置文件:

代码语言:javascript复制
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

启动类:

代码语言:javascript复制
@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("邮件服务项目启动");
    }
}

提供者接口实现:

代码语言:javascript复制
# 引入 apache 包、之前的还可以用,不过已过时
import org.apache.dubbo.config.annotation.Service;

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

}

消费引用:

代码语言:javascript复制
import org.apache.dubbo.config.annotation.Reference;

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

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

}
Nacos 注册中心版
代码语言:javascript复制
<!-- 升级 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>

配置文件:

代码语言:javascript复制
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

启动类:

代码语言:javascript复制
@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("邮件服务项目启动");
    }
}

0 人点赞