Dubbo 学习笔记(三) Spring Boot 整合 Dubbo(官方版)

阅读 129
收藏 1
2019-02-15
原文链接:zdran.com

Dubbo 学习笔记(三) Spring Boot 整合 Dubbo(官方版)


0. 前言

这次使用的是 Apache 推出的 Spring Boot 与 Dubbo 整合的依赖。

1<dependency>
2    <groupId>com.alibaba.boot</groupId>
3    <artifactId>dubbo-spring-boot-starter</artifactId>
4    <version>0.2.1.RELEASE</version>
5</dependency>
6

1. 创建项目

创建一个项目,三个模块分别是服务提供者、服务消费者、服务暴露的API。

项目结构:

1official
2    |-official-api
3    |-official-account-provider
4    |-official-web

主 POM 的依赖:

1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4    <modelVersion>4.0.0</modelVersion>
5    <groupId>com.zdran.duboot</groupId>
6    <packaging>pom</packaging>
7    <artifactId>official</artifactId>
8    <version>0.0.1-SNAPSHOT</version>
9    <name>official</name>
10    <description>Demo project for Spring Boot</description>
11
12    <parent>
13        <groupId>org.springframework.boot</groupId>
14        <artifactId>spring-boot-starter-parent</artifactId>
15        <version>2.1.2.RELEASE</version>
16    </parent>
17
18    <properties>
19        <java.version>1.8</java.version>
20        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22        <spring-boot.version>2.1.1.RELEASE</spring-boot.version>
23        <dubbo.version>2.6.5</dubbo.version>
24    </properties>
25    <modules>
26        <module>official-api</module>
27        <module>official-account-provider</module>
28        <module>official-web</module>
29    </modules>
30
31    <build>
32        <plugins>
33            <plugin>
34                <groupId>org.springframework.boot</groupId>
35                <artifactId>spring-boot-maven-plugin</artifactId>
36            </plugin>
37        </plugins>
38    </build>
39</project>

2. 配置服务提供者

添加依赖:

1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4    <modelVersion>4.0.0</modelVersion>
5    <parent>
6        <groupId>com.zdran.duboot</groupId>
7        <artifactId>official</artifactId>
8        <version>0.0.1-SNAPSHOT</version>
9    </parent>
10    <groupId>com.zdran.official</groupId>
11    <artifactId>account-provider</artifactId>
12    <version>0.0.1-SNAPSHOT</version>
13    <name>account-provider</name>
14    <description>Demo project for Spring Boot</description>
15
16    <properties>
17        <java.version>1.8</java.version>
18        <spring-boot.version>2.1.1.RELEASE</spring-boot.version>
19        <dubbo.version>2.6.5</dubbo.version>
20    </properties>
21
22    <dependencies>
23        <dependency>
24            <groupId>com.zdran.duboot</groupId>
25            <artifactId>official-api</artifactId>
26            <version>0.0.1-SNAPSHOT</version>
27        </dependency>
28        <!-- Dubbo Spring Boot Starter -->
29        <dependency>
30            <groupId>com.alibaba.boot</groupId>
31            <artifactId>dubbo-spring-boot-starter</artifactId>
32            <version>0.2.1.RELEASE</version>
33        </dependency>
34        <dependency>
35            <groupId>com.alibaba</groupId>
36            <artifactId>dubbo</artifactId>
37            <version>${dubbo.version}</version>
38        </dependency>
39        <dependency>
40            <groupId>io.netty</groupId>
41            <artifactId>netty-all</artifactId>
42        </dependency>
43
44        <dependency>
45            <groupId>org.springframework.boot</groupId>
46            <artifactId>spring-boot-starter-test</artifactId>
47            <version>2.1.2.RELEASE</version>
48            <scope>test</scope>
49        </dependency>
50
51        <dependency>
52            <groupId>org.apache.curator</groupId>
53            <artifactId>curator-framework</artifactId>
54            <version>2.11.1</version>
55        </dependency>
56    </dependencies>
57
58    <build>
59        <plugins>
60            <plugin>
61                <groupId>org.springframework.boot</groupId>
62                <artifactId>spring-boot-maven-plugin</artifactId>
63            </plugin>
64        </plugins>
65    </build>
66</project>
67

更新 application.yml 的配置:

1server:
2  port: 8086
3
4dubbo:
5  application:
6    name: Provide
7  registry:
8    address: zookeeper://39.105.78.88:2181
9  protocol:
10    name: dubbo
11    port: 20880
12  scan:
13    base-packages: com.zdran.official.user.service 

注意:配置项是 dubbo.xxx,不是 spring.dubbo.xxx

实现 Service:

1package com.zdran.official.user.service;
2
3import com.alibaba.dubbo.config.annotation.Service;
4import com.zdran.duboot.official.api.OfficialHelloApi;
5
6/**
7 * Create by ranzd on 2019/1/28
8 *
9 * @author ranzd@chinaunicom.cn
10 */
11@Service(version = "1.0", timeout = 50000)
12public class OfficialHelloApiImpl implements OfficialHelloApi {
13    @Override
14    public String sayHello(String name) {
15        return "helll, " + name;
16    }
17}
18

接口定义在了official-api模块

3. 实现消费者

添加依赖:

1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4    <modelVersion>4.0.0</modelVersion>
5    <parent>
6        <groupId>org.springframework.boot</groupId>
7        <artifactId>spring-boot-starter-parent</artifactId>
8        <version>2.1.2.RELEASE</version>
9        <relativePath/> <!-- lookup parent from repository -->
10    </parent>
11    <groupId>com.zdran.official</groupId>
12    <artifactId>web</artifactId>
13    <version>0.0.1-SNAPSHOT</version>
14    <name>web</name>
15    <description>Demo project for Spring Boot</description>
16
17    <properties>
18        <java.version>1.8</java.version>
19        <dubbo.version>2.6.5</dubbo.version>
20    </properties>
21
22    <dependencies>
23        <dependency>
24            <groupId>com.zdran.duboot</groupId>
25            <artifactId>official-api</artifactId>
26            <version>0.0.1-SNAPSHOT</version>
27        </dependency>
28        <dependency>
29            <groupId>org.springframework.boot</groupId>
30            <artifactId>spring-boot-starter-web</artifactId>
31        </dependency>
32
33        <!-- Dubbo Spring Boot Starter -->
34        <dependency>
35            <groupId>com.alibaba.boot</groupId>
36            <artifactId>dubbo-spring-boot-starter</artifactId>
37            <version>0.2.1.RELEASE</version>
38        </dependency>
39        <dependency>
40            <groupId>com.alibaba</groupId>
41            <artifactId>dubbo</artifactId>
42            <version>${dubbo.version}</version>
43        </dependency>
44        <dependency>
45            <groupId>io.netty</groupId>
46            <artifactId>netty-all</artifactId>
47
48        </dependency>
49
50        <dependency>
51            <groupId>org.springframework.boot</groupId>
52            <artifactId>spring-boot-starter-test</artifactId>
53            <version>2.1.2.RELEASE</version>
54            <scope>test</scope>
55        </dependency>
56
57        <dependency>
58            <groupId>org.apache.curator</groupId>
59            <artifactId>curator-framework</artifactId>
60            <version>2.11.1</version>
61        </dependency>
62    </dependencies>
63
64    <build>
65        <plugins>
66            <plugin>
67                <groupId>org.springframework.boot</groupId>
68                <artifactId>spring-boot-maven-plugin</artifactId>
69            </plugin>
70        </plugins>
71    </build>
72
73</project>
74

注意:curator-framework 这个依赖跟使用的zk版本有关系

curator-framework 2.x.x 支持的zk版本为 3.4.x 和3.5.x
curator-framework 3.x.x 支持的zk版本为 3.5.x

更新 application.yml 的配置:

1server:
2  port: 8085
3
4dubbo:
5  application:
6    name: conusmer
7  registry:
8    address: zookeeper://39.105.78.88:2181
9  protocol:
10    name: dubbo
11    port: 20880
12  scan:
13    base-packages: com.zdran.official.web

创建消费者的服务

1package com.zdran.official.web.controller;
2
3import com.alibaba.dubbo.config.annotation.Reference;
4import com.zdran.duboot.official.api.OfficialHelloApi;
5import org.springframework.web.bind.annotation.GetMapping;
6import org.springframework.web.bind.annotation.PathVariable;
7import org.springframework.web.bind.annotation.RestController;
8
9/**
10 * Create by ranzd on 2019/1/29
11 *
12 * @author ranzd@chinaunicom.cn
13 */
14@RestController
15public class AccountController {
16
17    @Reference(version = "1.0")
18    private OfficialHelloApi officialHelloApi;
19
20    @GetMapping("/account/{name}")
21    public String sayHello(@PathVariable(name = "name") String name) {
22        return officialHelloApi.sayHello(name);
23    }
24
25}
26

先启动生产者,再启动消费者,启动的时候如果看到 下面的日志说明启动成功了:

12019-02-12 11:09:14.780  INFO 3428 --- [           main] a.b.d.c.e.WelcomeLogoApplicationListener : 
2
3 :: Dubbo Spring Boot (v0.2.1.RELEASE) : https://github.com/apache/incubator-dubbo-spring-boot-project
4 :: Dubbo (v2.6.5) : https://github.com/apache/incubator-dubbo
5 :: Discuss group : dev@dubbo.apache.org
6
72019-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 里是包含这个依赖的,不知道为什么还需要重新引入?

转载请注明出处
本文链接:zdran.com/20190212.ht…

评论