我是靠谱客的博主 直率大地,这篇文章主要介绍使用Dubbo + Springboot + Zookeeper搭建项目Dubbo 初体验,现在分享给大家,希望可以做个参考。

Dubbo 初体验

什么是dubbo

这里直接引用Apache Dubbo官网的介绍,链接: apache dubbo

Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 Java、Golang 等多语言 SDK 实现。使用 Dubbo 开发的微服务原生具备相互之间的远程地址发现与通信能力, 利用 Dubbo 提供的丰富服务治理特性,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。Dubbo 被设计为高度可扩展,用户可以方便的实现流量拦截、选址的各种定制逻辑。

简单来说,dubbo是一个rpc框架,主要用于微服务架构下,帮助管理不同模块之前的方法调用。

dubbo整体结构

在官方文档中,有如下的dubbo结构图:
在这里插入图片描述
dubbo由注册中心(Registry)、服务提供者(Provider)、服务消费者(Consumer)、监控(Monitor)组成。

  • 其中注册中心不是必需的,Consumers和Providers可以采用直连的方式,进行远程调用。注册中心可以使用Nacos,Zookeeper, Multicast,Redis , Simple。一般生产环境比较常用的是Nacos和Zookeeper。
  • 服务提供者向注册中心注册服务,包括服务名称、服务调用地址等信息,以供服务消费者调用。当服务注册后或者服务下线后,注册中心能够通知服务消费者。
  • 服务消费者根据需要调用的服务名称,从注册中心中获取服务调用地址后,发起服务调用。
  • 监控用来统计每个服务调用次数。

Dubbo + Springboot + Zookeeper 实现服务远程调用

本次使用的框架版本如下:
Springboot :2.4.1
Zookeeper : 3.8.0
Dubbo : 3.0.2.1

项目搭建

首先在idea中创建maven项目,创建完成后,删除src目录,创建dubbo-springboot-api、dubbo-springboot-provider、dubbo-springboot-consumer三个子模块。完成后项目整体目录如下:
在这里插入图片描述
其中服务接口主要起到解耦的作用,降低服务提供者和服务消费者的依赖关系。

修改父模块pom.xml

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<dependencyManagement> <dependencies> <!-- 统一jar版本管理,避免使用 spring-boot-parent --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.4.1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-bom</artifactId> <version>3.0.2.1</version> <type>pom</type> <scope>import</scope> </dependency> <!--dubbo 和 springboot 整合的包--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>3.0.2.1</version> </dependency> </dependencies> </dependencyManagement>

添加服务接口

首先在dubbo-springboot-api模块中定义服务接口:

复制代码
1
2
3
4
public interface IUserService { User getUserInfo(int id); }

User实体类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
@Data @AllArgsConstructor @NoArgsConstructor public class User implements Serializable { private static final long serialVersionUID = -1L; String name; Integer age; }

@Data 、@AllArgsConstructor、@NoArgsConstructor注解需要添加lombok依赖,需要在dubbo-springboot-api模块下的pom.xml中添加依赖:

复制代码
1
2
3
4
5
6
7
8
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> </dependencies>

实现服务提供者

在dubbo-springboot-provider模块下的pom.xml文件中引入依赖:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<dependencies> <dependency> <groupId>com.juffy</groupId> <artifactId>dubbo-springboot-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--dubbo 与 spring-boot 整合包--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <!--springboot 启动核心包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--springboot rest --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> </dependency> </dependencies>

在dubbo-springboot-provider模块下,创建UserServiceImpl.java:

复制代码
1
2
3
4
5
6
7
8
9
10
@DubboService(version = "1.0.0") @Service @Slf4j public class UserServiceImpl implements IUserService { @Override public User getUserInfo(int id) { return new User("Tom", 23); } }

在dubbo-springboot-provider模块下,创建DubboProvider.java,作为项目启动类:

复制代码
1
2
3
4
5
6
7
8
9
@SpringBootApplication @EnableDubbo public class DubboProvider { public static void main(String[] args) { SpringApplication.run(DubboProvider.class); } }

在dubbo-springboot-provider模块下,添加springboot项目配置文件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server: port: 8080 spring: application: name: dubbo-springboot-provider dubbo: application: name: dubbo-springboot-provider registry: address: zookeeper://ip:port timeout: 2000 protocol: name: dubbo port: 20890 # 扫描 @DubboService 注解 scan: base-packages: com.juffy.dubboprovider.service.impl

实现服务消费者

在dubbo-springboot-consumer模块下的pom.xml文件中引入依赖:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<dependencies> <dependency> <groupId>com.juffy</groupId> <artifactId>dubbo-springboot-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </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.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> </dependency> </dependencies>

在dubbo-springboot-consumer模块下,创建controller类、springboot启动类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController @Slf4j @RequestMapping("/consumer") public class UserController { @DubboReference(version = "1.0.0",protocol = "dubbo", loadbalance = "random") private IUserService userService; @RequestMapping("/user/{id}") public User getUser(@PathVariable("id") int id) { User user = userService.getUserInfo(id); log.info("response from provider: {}", user); return user; } }

@DubboReference 表示需要发起远程调用。

复制代码
1
2
3
4
5
6
7
8
9
@SpringBootApplication @EnableDubbo public class DubboConsumer { public static void main(String[] args) { SpringApplication.run(DubboConsumer.class); } }

运行

启动dubbo-springboot-provider、dubbo-springboot-consumer。
启动成功后,在浏览器中输入http://localhost:8091/consumer/user/5
在这里插入图片描述
可以看到,不同项目的方法之前通过dubbo实现调用。

zookeeper

我来看看服务是如何在注册中心保存的。
启动服务提供者和消费者后,会来zookeeper上生成一个/dubbo目录,该目录保存已注册的服务名称:
在这里插入图片描述
可以看到com.juffy.dubboapi.IUserService就是我刚刚注册的接口。

进入com.juffy.dubboapi.IUserService目录下:
在这里插入图片描述
每个服务的目录下有configurators、consumers、providers和routers。
providers记录服务的提供者信息。
consumers记录服务的消费者信息。
routers记录消费者路由策略URL元数据信息。
configurators记录服务者动态配置URL元数据信息。

最后

以上就是直率大地最近收集整理的关于使用Dubbo + Springboot + Zookeeper搭建项目Dubbo 初体验的全部内容,更多相关使用Dubbo内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(80)

评论列表共有 0 条评论

立即
投稿
返回
顶部