U2647's blog 一个热爱学习的 Java 程序员,喜欢 Vue,喜欢深度学习 Dubbo Flutter SpringBoot Debug Notes Java LeetCode Python Redis Android DesignPattern mdi-home-outline 首页 mdi-cloud-outline 标签云 mdi-timeline-text-outline 时间轴 mdi-draw-pen 文章总数 62
Dubbo 学习笔记(二) Spring Boot 整合 Dubbo Dubbo 学习笔记(二) Spring Boot 整合 Dubbo Dubbo RPC ZooKeeper Dubbo 教程 Spring Boot mdi-cursor-default-click-outline 点击量 62

0. 槽点

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

第一个是 apache推出的依赖:

<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>

另一个是 alibaba 推出的

    <dependency>
        <groupId>com.alibaba.spring.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>

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

    <dependency>
        <groupId>io.dubbo.springboot</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
        <version>1.0.0</version>
    </dependency>

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

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

1. 创建项目

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

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

整个项目的目录结构为:

duboot
    |-duboot-api
    |-duboot-user-provider
    |-duboot-web

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

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

2. 添加依赖

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

<?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.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zdran.duboot</groupId>
    <artifactId>user-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>user-provider</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.zdran.duboot</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--dubbo-springBoot依赖-->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!--zookeeper依赖-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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

3. 添加配置

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

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

server:
  port: 8086

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

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

4. 实现服务

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

package com.zdran.duboot.api.service;

/**
 * Create by ranzd on 2018/12/15
 *
 * @author cm.zdran@gmail.com
 */
public interface HelloDubbo {
    String sayHello(String name);
}

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

package com.zdran.duboot.user.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.zdran.duboot.api.service.HelloDubbo;

/**
 * Create by ranzd on 2018/12/15
 *
 */
@Service(version = "1.0", timeout = 50000)
public class HelloDubboImpl implements HelloDubbo {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

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

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

package com.zdran.duboot.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zdran.duboot.api.service.HelloDubbo;
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 2018/12/16
 *
 * @author cm.zdran@gmail.com
 */

@RestController
public class HelloController {

    @Reference(version = "1.0")
    private HelloDubbo helloDubbo;

    @GetMapping("/duboot/hello/{name}")
    public String hello(@PathVariable("name") String name) {
        return helloDubbo.sayHello(name);
    }
}

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

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
我的GitHub 我的LeetCode 我的掘金
Powered by Hexo Powered by three-cards
Copyright © 2017 - {{ new Date().getFullYear() }} 某ICP备xxxxxxxx号