Dubbo 学习笔记(二) Spring Boot 整合 Dubbo | zdRan's blog

142 阅读7分钟

Dubbo 学习笔记(一) Hello,Dubbo


0. 槽点

Spring Boot 与 Dubbo 整合的依赖有3个版本。

第一个是 apache推出的依赖:

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

另一个是 alibaba 推出的

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

还有一个是个人开发者推出的

1    <dependency>
2        <groupId>io.dubbo.springboot</groupId>
3        <artifactId>spring-boot-starter-dubbo</artifactId>
4        <version>1.0.0</version>
5    </dependency>

这三个依赖的配置好像都不太一样,有些依赖还得添加额外的依赖。所以你在看博客的时候一定要看清楚依赖,尤其是第一个和第二个。

这篇文章使用的是个人开发者推出的依赖,如果你不想使用这个版本的依赖,就不用再往下看了。

1. 创建项目

还记得我们在第一篇文章中说的,RPC 调用主要有三个东西。服务提供者(生产者)、服务调用者(消费者)、服务注册中心。

创建一个名称为 duboot 的项目。然后在这个项目里创建三个 spring boot 模块。

整个项目的目录结构为:

1duboot
2    |-duboot-api
3    |-duboot-user-provider
4    |-duboot-web

一个非常简单的 RPC 调用的项目框架。其中 duboot-api 定义需要发布的接口。或者叫需要发布的服务。duboot-user-provider 负责实现对应的服务。即服务提供者(生产者),duboot-web 使用服务,即服务的调用者(消费者)。

注意:user与web模块都是 Spring Boot 项目

2. 添加依赖

这里使用的依赖是个人推出的依赖包,而且user-provider模块和web模块的依赖是一样的。

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.1.RELEASE</version>
9        <relativePath/> <!-- lookup parent from repository -->
10    </parent>
11    <groupId>com.zdran.duboot</groupId>
12    <artifactId>user-provider</artifactId>
13    <version>0.0.1-SNAPSHOT</version>
14    <name>user-provider</name>
15    <description>Demo project for Spring Boot</description>
16
17    <properties>
18        <java.version>1.8</java.version>
19    </properties>
20
21    <dependencies>
22        <dependency>
23            <groupId>com.zdran.duboot</groupId>
24            <artifactId>api</artifactId>
25            <version>0.0.1-SNAPSHOT</version>
26        </dependency>
27        <dependency>
28            <groupId>org.springframework.boot</groupId>
29            <artifactId>spring-boot-starter</artifactId>
30        </dependency>
31
32        <dependency>
33            <groupId>org.springframework.boot</groupId>
34            <artifactId>spring-boot-starter-test</artifactId>
35            <scope>test</scope>
36        </dependency>
37        <!--dubbo-springBoot依赖-->
38        <dependency>
39            <groupId>io.dubbo.springboot</groupId>
40            <artifactId>spring-boot-starter-dubbo</artifactId>
41            <version>1.0.0</version>
42        </dependency>
43        <!--zookeeper依赖-->
44        <dependency>
45            <groupId>org.apache.zookeeper</groupId>
46            <artifactId>zookeeper</artifactId>
47            <version>3.4.8</version>
48        </dependency>
49
50    </dependencies>
51
52    <build>
53        <plugins>
54            <plugin>
55                <groupId>org.springframework.boot</groupId>
56                <artifactId>spring-boot-maven-plugin</artifactId>
57            </plugin>
58        </plugins>
59    </build>
60
61</project>
62

注意:这里依赖了 duboot-api模块,以及zookeeper相关的东西

3. 添加配置

在 application.yml 里添加相关的配置。

注意: user-provider模块与web模块的配置基本一样,只需要修改一下 server.port、application.name、scan这几个值就可以了

1server:
2  port: 8086
3
4spring:
5  dubbo:
6    #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
7    application:            
8      name: duboot-user-provider
9    #注册中心配置,用于配置连接注册中心相关信息。
10    registry:                
11      address: zookeeper://127.0.0.1:2181
12    #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
13    protocol:
14      name: dubbo
15      port: 20880
16    scan: com.zdran.duboot.user.provider  #服务暴露与发现消费所在的package

注册中心可以使用我们上一篇文章中搭建好的 zookeeper 就可以了 。

4. 实现服务

我们首先在 duboot-api模块 里创建一个接口,或者说定义一个服务:

1package com.zdran.duboot.api.service;
2
3/**
4 * Create by ranzd on 2018/12/15
5 *
6 * @author cm.zdran@gmail.com
7 */
8public interface HelloDubbo {
9    String sayHello(String name);
10}
11

然后在 duboot-user-provider模块 里实现这个接口,也叫实现服务

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

这里的 Service 注解是dubbo包里的,不是spring 包里的

然后就可以在 web模块 发起调用了。我们在 web模块 实现一个controller,然后让这个controller去调用刚刚实现好的服务。

1package com.zdran.duboot.web.controller;
2
3import com.alibaba.dubbo.config.annotation.Reference;
4import com.zdran.duboot.api.service.HelloDubbo;
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 2018/12/16
11 *
12 * @author cm.zdran@gmail.com
13 */
14
15@RestController
16public class HelloController {
17
18    @Reference(version = "1.0")
19    private HelloDubbo helloDubbo;
20
21    @GetMapping("/duboot/hello/{name}")
22    public String hello(@PathVariable("name") String name) {
23        return helloDubbo.sayHello(name);
24    }
25}
26

访问一下 http://localhost:8085/duboot/hello/dubbo试试看吧。

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