前言

前两天在和粉丝聊天的时候,粉丝跟我说之前在面试的时候被问到SpringBoot这一块的知识被问的有点懵,和我问了不少这方面的东西。事后我想了想不如把这些东西分享出来吧,让更多的人看到,这样不管是对自己知识的一个提升还是对面试的准备都有好处。好了,满满的干货都在下面了!

1.创建maven项目父工程(不使用idea的spring Initializr)

不勾选任何模板,直接使用默认项目模板

删除src文件夹,创建这个项目的目的是为了作为父工程,无需src文件夹,只保留pom文件即可

编辑pom文件

设置父工程打包方式为pom,用于依赖管理

    <packaging>pom</packaging>

添加必要依赖

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <!-- dubbo依赖 apache版 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.7</version>
        </dependency>

        <!-- dubbo所需其他依赖 使用alibaba的dubbo则不需要 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.1</version>
        </dependency>

        <!-- dubbo所需其他依赖 使用alibaba的dubbo则不需要 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.8.0</version>
        </dependency>

        <!-- zookeeper依赖 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.7</version>
        </dependency>
    </dependencies>
</dependencyManagement>

2. 创建生产者

同第一步相同,创建子模块,名为provider

删除src文件夹,该模块仍作为父工程使用

修改pom文件,继承父工程,并设置打包方式为pom

    <parent>
        <groupId>online.hupeng.dubbo</groupId>
        <artifactId>base</artifactId>
        <version>1.0</version>
    </parent>
    <artifactId>provider</artifactId>
    <packaging>pom</packaging>

创建子工程provider-api,

修改pom文件,继承父工程provider
这个工程为生产者和约束者约束api规范,生产者和消费者都需依赖此模块联系彼此
此模块中只写api和实体类,不写实现方式

<parent>
    <groupId>online.hupeng.dubbo</groupId>
    <artifactId>provider</artifactId>
    <version>1.0</version>
</parent>
<artifactId>provider-api</artifactId>
<version>1.0</version>
<!-- maven默认打包方式为jar包,此处可不用显示指定 -->
<packaging>jar</packaging>

编写代码

实体类user

package online.hupeng.dubbo.provider.domain;

import java.io.Serializable;
/*
当实体类作为RPC方法的返回值时,必须实现Serializable接口,dubbo的实现原理就是
消费者远程调用生产者方法,生产者返回序列化后的返回值,消费者通过网络获取到序
列化后的数据再反序列化
*/
/*
此处不实现Serializable接口,远程调用方法时会报错,并提示实体类需继承此接口
*/
public class User implements Serializable {

    private String account;

    private String password;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
    this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

UserService接口

    package online.hupeng.dubbo.provider.service;

    import online.hupeng.dubbo.provider.domain.User;

    public interface UserService {

        User getUserInstance();
    }

执行mvn install命令将jar包打入本地仓库
完毕
在provider下创建子工程provider-service模块(此模块为真正的生产者)

编辑pom文件继承父工程

<parent>
    <groupId>online.hupeng.dubbo</groupId>
    <artifactId>provider</artifactId>
    <version>1.0</version>
</parent>
<artifactId>provider-service</artifactId>
<version>1.0</version>
<name>provider-service</name>
<properties>
    <java.version>1.8</java.version>
</properties>

添加必须依赖

<dependencies>
    <!-- 此处依赖api模块规范接口 -->
    <dependency>
        <groupId>online.hupeng.dubbo</groupId>
        <artifactId>provider-api</artifactId>
        <version>1.0</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>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
    </dependency>

    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
    </dependency>
</dependencies>

UserService的实现类UserServiceImpl

package online.hupeng.dubbo.provider.service.impl;

import online.hupeng.dubbo.provider.domain.User;
import online.hupeng.dubbo.provider.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
    /*
    dubbo注解,指定接口版本号和超时时间,调用方需正确填写版本信息
    */
    @DubboService(version = "1.0", timeout = 300)
    public class UserServiceImpl implements UserService {
    @Override
    public User getUserInstance() {
        User user = new User();
        user.setAccount("admin");
    user.setPassword("admin");
    return user;
    }
}

application.yml配置

dubbo:
    application:
        # 指定该服务名称 
        name: provider
    registry:
        # 通信协议
        protocol: zookeeper
        # 注册中心地址
        address: 127.0.0.1
        # 注册中心端口号
        port: 2181
        # 也可以不配置protocol和port,直接配置为zookeeper://127.0.0.1:2181

    protocol:
    name: dubbo
    # 服务暴露端口
    port: 8081

# 包扫描(此处为扫描dubbo的注解,和SpringBoot无关)
    scan:
    base-packages: online.hupeng.dubbo

为启动类添加dubbo注解@EnableDubbo

package online.hupeng.dubbo.provider;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class ProviderServiceApplication {

    public static void main(String[] args) {
    SpringApplication.run(ProviderServiceApplication.class, args);
    }
}

3. 创建消费者

在根项目下创建consumer模块
编辑pom文件继承父项目

<parent>
    <artifactId>base</artifactId>
    <groupId>online.hupeng.dubbo</groupId>
    <version>1.0</version>
</parent>
<artifactId>consumer</artifactId>
<version>1.0</version>

添加必须依赖

<dependencies>
    <!-- 引入provider-api依赖用以远程调用 -->
    <dependency>
        <groupId>online.hupeng.dubbo</groupId>
        <artifactId>provider-api</artifactId>
        <version>1.0</version>
        <scope>compile</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
    </dependency>

    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
    </dependency>
</dependencies>

编辑controller层代码远程调用

package online.hupeng.dubbo.consumer.controller;

import online.hupeng.dubbo.provider.domain.User;
import online.hupeng.dubbo.provider.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ConsumerController {
    /*
    远程调用注解,需正确对应生产者的版本号,此处不需要@autowird注入对象
    */
    @DubboReference(version = "1.0")
    private UserService userService;

    @ResponseBody
    @GetMapping("/test")
    public User getUser() {
        return userService.getUserInstance();
    }
}

配置application.yml文件

dubbo:
    application:
        name: provider
    registry:
        protocol: zookeeper
        address: 127.0.0.1
        port: 2181

    protocol:
        name: dubbo
        # 服务暴露端口
        port: 8081

    # 包扫描
    scan:
        base-packages: online.hupeng.dubbo
#### 添加SpringBoot启动类

package online.hupeng.dubbo.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApplication {

public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class, args);
}

}

## 4. 测试
按顺序启动zookeeper、provider-service、consumer
访问http://localhost/test
![](https://upload-images.jianshu.io/upload_images/23140115-86109ded3f85528b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

成功!!!
## 最后

前程有光
936 声望618 粉丝