使用springboot+dubbo搭建RPC入门案例



先来一张简单的架构图吧,这个图是自己使用processOn画的(这里推荐一波processOn线上作图神器):
springboot+dubbo简单分布式RPC调用demo-LMLPHP


1、创建一个比较原生的Maven项目dubbo-common

这个项目中只定义公共接口和实体,以供后面的provider工程项目和consumer工程项目使用(如下图所示):
springboot+dubbo简单分布式RPC调用demo-LMLPHP

TeamService中的代码也很简单,如下:

package com.chandler.dubbo.service;

import com.chandler.dubbo.entity.Team;

public interface TeamService {
    Team getTeamInfo(String city);
}

2、下载与配置zookeeper

下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ ,至于怎么配置和使用,可以自己移驾度娘,上面很多,其实也比较简单。


3、创建dubbo-provider项目

首先从下图所示这里创建一个springboot项目:
springboot+dubbo简单分布式RPC调用demo-LMLPHP

创建后的工程大概如下图所示,这个provider项目很简单,就是一个实现了dubbo-common项目中的那个接口的service方法。
springboot+dubbo简单分布式RPC调用demo-LMLPHP

项目创建完了后我们首先要把dubbo-common中的那个划红线的jar包手动添加到lib库中,以备后面使用(正常来说我们肯定是将类似于dubbo-common这样一个公共工程上传到Maven公共镜像仓库或者是公司内部的是有个Maven镜像仓库中去,以备提服务的项目和消费服务的项目通过pom文件直接添加依赖就可以使用。这里我们为了简单演示就直接手动添加算了)。手动添加步骤如下:
File-->Project Structure-->Modules-->Dependencies-->右侧+号找到那个jar包-->点击底部apply-->点击ok
springboot+dubbo简单分布式RPC调用demo-LMLPHP

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 https://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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.chandler.dubbo</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-provider</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <!--boot依赖-->
        <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>

        <!-- 引入dubbo的依赖 -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- 引入zookeeper的依赖 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <!-- slf4j依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

application.yml中的内容:

server.port=8081
spring.application.name=dubbo-provider
#本地zookeeper注册中心地址
spring.dubbo.registry=zookeeper://localhost:2181

MyServiceImpl代码:(由于这里只是为了简单演示,所以就没有使用数据库什么的了,直接在static代码块中使用集合存储几个对象以备后面查询)

package com.chandler.dubbo.dubboprovider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.chandler.dubbo.entity.Team;
import com.chandler.dubbo.service.TeamService;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
//这个service注解是dubbo中的,目的就是将这个服务暴露出去。
//里面可以指定代理版本,代理名称还有很多的其他可选配置项,dubbo官网的文档中都有说明
@Service(version = "1.0.0",proxy = "TeamServer")
public class MyserviceImpl implements TeamService {

    private static HashMap<String,Team> teamHashMap = new HashMap<>(16);
    static {
        Team team1 = new Team();
        team1.setCity("chicago");
        team1.setName("bulls");
        team1.setTopPlayer("jordan&rose");
        Team team2 = new Team();
        team2.setTopPlayer("james&kobe");
        team2.setName("lakers");
        team2.setCity("loss angeles");
        teamHashMap.put("chicago",team1);
        teamHashMap.put("lal",team2);
    }
    @Override
    public Team getTeamInfo(String s) {
        return teamHashMap.get(s);
    }
}

最后是DubboProviderApplication启动类:

package com.chandler.dubbo.dubboprovider;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration
public class DubboProviderApplication {

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

}

4、创建dubbo-consumer项目

同创建dubbo-provider一样的步骤创建consumer项目,项目结构如下图:
springboot+dubbo简单分布式RPC调用demo-LMLPHP

然后呢,也同dubbo-provider项目中一样手动添加dubbo-common项目的jar包到lib库中。
接下来就是pom文件内容,和provider项目中的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 https://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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.chandler.dubbo</groupId>
    <artifactId>dubbo-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-consumer</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <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>

        <!-- 引入dubbo的依赖 -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- 引入zookeeper的依赖 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <!-- slf4j依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.60</version>
        </dependency>
    </dependencies>

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

</project>

TeamService类代码:

package com.chandler.dubbo.dubboconsumer.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.chandler.dubbo.entity.Team;
import org.springframework.stereotype.Service;

@Service
public class TeamService {
    @Reference(proxy = "TeamServer",version = "1.0.0")
    com.chandler.dubbo.service.TeamService teamService;

    public Team getTeam(String cityName){
        return teamService.getTeamInfo(cityName);
    }
}

TeamController类代码:

package com.chandler.dubbo.dubboconsumer.controller;

import com.chandler.dubbo.dubboconsumer.service.TeamService;
import com.chandler.dubbo.entity.Team;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
public class TeamController {

    @Resource
    private TeamService teamService;
    @GetMapping("/getTeam")
    public Team getTeam(@RequestParam("cityName") String cityName){
        return teamService.getTeam(cityName);
    }
}

DubboConsumerApplication启动类代码:

package com.chandler.dubbo.dubboconsumer;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration
public class DubboConsumerApplication {

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

}

最后就可以依次启动zookeeper、dubbo-provider和dubbo-consumer项目了,然后可以使用浏览器或者postman测试dubbo-consumer项目中的controller方法了。

04-29 08:23