Dubbo 学习笔记 (三) Spring Boot 整合 Dubbo(官方版)
0. 前言
这次使用的是 Apache 推出的 Spring Boot 与 Dubbo 整合的依赖。
https://github.com/apache/incubator-dubbo-spring-boot-project
最新的版本是 0.2.1
star:2383
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
1. 创建项目
创建一个项目,三个模块分别是服务提供者、服务消费者、服务暴露的 API。
项目结构:
official
|-official-api
|-official-account-provider
|-official-web
主 POM 的依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zdran.duboot</groupId>
<packaging>pom</packaging>
<artifactId>official</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>official</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
<dubbo.version>2.6.5</dubbo.version>
</properties>
<modules>
<module>official-api</module>
<module>official-account-provider</module>
<module>official-web</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 配置服务提供者
添加依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zdran.duboot</groupId>
<artifactId>official</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.zdran.official</groupId>
<artifactId>account-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>account-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
<dubbo.version>2.6.5</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>com.zdran.duboot</groupId>
<artifactId>official-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.11.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
更新 application.yml
的配置:
server:
port: 8086
dubbo:
application:
name: Provide
registry:
address: zookeeper://39.105.78.88:2181
protocol:
name: dubbo
port: 20880
scan:
base-packages: com.zdran.official.user.service
注意:配置项是 dubbo.xxx,不是 spring.dubbo.xxx
实现 Service:
package com.zdran.official.user.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.zdran.duboot.official.api.OfficialHelloApi;
/**
* Create by ranzd on 2019/1/28
*
* @author ranzd@chinaunicom.cn
*/
@Service(version = "1.0", timeout = 50000)
public class OfficialHelloApiImpl implements OfficialHelloApi {
@Override
public String sayHello(String name) {
return "helll, " + name;
}
}
接口定义在了 official-api 模块
3. 实现消费者
添加依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zdran.official</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>web</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.6.5</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>com.zdran.duboot</groupId>
<artifactId>official-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.11.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注意:curator-framework 这个依赖跟使用的 zk 版本有关系
curator-framework 2.x.x 支持的 zk 版本为 3.4.x 和 3.5.xcurator-framework 3.x.x 支持的 zk 版本为 3.5.x
更新 application.yml
的配置:
server:
port: 8085
dubbo:
application:
name: conusmer
registry:
address: zookeeper://39.105.78.88:2181
protocol:
name: dubbo
port: 20880
scan:
base-packages: com.zdran.official.web
创建消费者的服务
package com.zdran.official.web.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zdran.duboot.official.api.OfficialHelloApi;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* Create by ranzd on 2019/1/29
*
* @author ranzd@chinaunicom.cn
*/
@RestController
public class AccountController {
@Reference(version = "1.0")
private OfficialHelloApi officialHelloApi;
@GetMapping("/account/{name}")
public String sayHello(@PathVariable(name = "name") String name) {
return officialHelloApi.sayHello(name);
}
}
先启动生产者,再启动消费者,启动的时候如果看到 下面的日志说明启动成功了:
2019-02-12 11:09:14.780 INFO 3428 --- [ main] a.b.d.c.e.WelcomeLogoApplicationListener :
:: Dubbo Spring Boot (v0.2.1.RELEASE) : https://github.com/apache/incubator-dubbo-spring-boot-project
:: Dubbo (v2.6.5) : https://github.com/apache/incubator-dubbo
:: Discuss group : dev@dubbo.apache.org
2019-02-12 11:09:14.783 INFO 3428 --- [ main] e.OverrideDubboConfigApplicationListener : Dubbo Config was overridden by externalized configuration {dubbo.application.name=Provide, dubbo.application.qos-enable=false, dubbo.config.multiple=true, dubbo.protocol.name=dubbo, dubbo.protocol.port=20880, dubbo.registry.address=zookeeper://39.105.78.88:2181, dubbo.scan.base-packages=com.zdran.official.user.service}
访问一下 http://localhost:8085/account/dubbo
试试。
4. 一个疑问
有个比较疑惑的地方一直没有搞明白。就是 curator-framework 这个依赖。
明明在 dubbo-spring-boot-starter 里是包含这个依赖的,不知道为什么还需要重新引入?
版权声明: 本文为 InfoQ 作者【U+2647】的原创文章。
原文链接:【http://xie.infoq.cn/article/bbc002e36253800cbc6894b3b】。文章转载请联系作者。
U+2647
evolving code monkey 2018.11.05 加入
https://zdran.com/
评论