springboot+dubbo简单分布式RPC调用demo
使用springboot+dubbo搭建RPC入门案例
本文背景简述:
最近在学习公司的一套RPC框架,初步接触的时候感觉挺复杂的。但是知道其原理肯定是和dubbo很相似的,毕竟都是RPC框架嘛,只是各自使用的范围和使
用者群体有些不同罢了。于是就去dubbo官网熟悉了其相关文档和案例教程,然后结合网上大佬的一些资料,自己搭了个简单的分布式远程调用案例。这里
强调一个东西--记住官方文档第一!!!下面分享一下我的案例代码吧,也希望本案例能对在读的读者启动一点帮助......
先来一张简单的架构图吧,这个图是自己使用processOn画的(这里推荐一波processOn线上作图神器):
1、创建一个比较原生的Maven项目dubbo-common
这个项目中只定义公共接口和实体,以供后面的provider工程项目和consumer工程项目使用(如下图所示):
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项目:
创建后的工程大概如下图所示,这个provider项目很简单,就是一个实现了dubbo-common项目中的那个接口的service方法。
项目创建完了后我们首先要把dubbo-common中的那个划红线的jar包手动添加到lib库中,以备后面使用(正常来说我们肯定是将类似于dubbo-common这样一个公共工程上传到Maven公共镜像仓库或者是公司内部的是有个Maven镜像仓库中去,以备提服务的项目和消费服务的项目通过pom文件直接添加依赖就可以使用。这里我们为了简单演示就直接手动添加算了)。手动添加步骤如下:
File-->Project Structure-->Modules-->Dependencies-->右侧+号找到那个jar包-->点击底部apply-->点击ok
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);
}
}
到这一步就可以启动项目了,启动项目后可以使用dubbo的管理控制台查看和管理服务,具体怎么弄可以参考官网文档:
http://dubbo.apache.org/zh-cn/docs/admin/introduction.html,我们接着在创建消费者项目。
4、创建dubbo-consumer项目
同创建dubbo-provider一样的步骤创建consumer项目,项目结构如下图:
然后呢,也同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方法了。