Spring Boot整合Dubbo+Zookeeper实现RPC调用

1,351 阅读3分钟

Spring Boot整合Dubbo+Zookeeper实现RPC调用

技术栈说明

Dubbo:Dubbo作为RPC框架,能在多个服务之间实现远程服务的调用。比如有两个独立的微服务A和B,A服务想要调用B服务时,因为两者不在同个内存空间中,不能直接调用,所以可以通过Dubbo实现这点。

功能和Spring Cloud的Feign相同,两者都是应用于微服务架构的远程调用框架

Zookeeper:作为注册中心去管理Dubbo服务,这点和Eureka、Nacos相同。

概述

通过一个示例说明Dubbo+Zookeeper在Spring Boot中的应用。

现有两个服务provider和consumer,即生产者和消费者:

实现步骤

  1. 搭建Zookeeper集群服务作为注册中心(docker-compose)
  2. Spring Boot项目,添加依赖,并创建三个Module:
    • api:提供Service接口,但不实现
    • provider:实现api的Service接口
    • consumer:调用api的Service接口,并对外提供访问接口

期望结果

调用consumer服务的访问接口,能访问到数据,则说明provider实现的服务成功注册到zookeeper注册中心,并被consumer获取到。

实现

Zookeeper集群搭建(docker-compose)

zookeeper.yml:

version: "3"
services:
  zk1:
    image: zookeeper
    network_mode: mynetwork
    container_name: zk1
    restart: always
    hostname: zk1
    # 端口映射,三个容器的对外端口是不同的
    ports:
      - 2181:2181
      - 8081:8080
    # 路径映射,路径也要注意不同
    volumes:
      - /etc/localtime:/etc/localtime
      - /home/mycontainers/zk1/data:/data
      - /home/mycontainers/zk1/datalog:/datalog
    environment:
      # 当前zk实例的id
      - ZOO_MY_ID=1
      # 整个zk集群的机器、端口列表
      - ZOO_SERVERS=server.1=0.0.0.0:2888:3888;2181 server.2=zk2:2888:3888;2181 server.3=zk3:2888:3888;2181
  zk2:
    image: zookeeper
    network_mode: mynetwork
    container_name: zk2
    restart: always
    hostname: zk2
    # 端口映射,三个容器的对外端口是不同的
    ports:
      - 2182:2181
      - 8082:8080
    # 路径映射,路径也要注意不同
    volumes:
      - /etc/localtime:/etc/localtime
      - /home/mycontainers/zk2/data:/data
      - /home/mycontainers/zk2/datalog:/datalog
    environment:
      # 当前zk实例的id
      - ZOO_MY_ID=2
      # 整个zk集群的机器、端口列表
      - ZOO_SERVERS=server.1=zk1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zk3:2888:3888;2181
  zk3:
    image: zookeeper
    network_mode: mynetwork
    container_name: zk3
    restart: always
    hostname: zk3
    # 端口映射,三个容器的对外端口是不同的
    ports:
      - 2183:2181
      - 8083:8080
    # 路径映射,路径也要注意不同
    volumes:
      - /etc/localtime:/etc/localtime
      - /home/mycontainers/zk3/data:/data
      - /home/mycontainers/zk3/datalog:/datalog
    environment:
      # 当前zk实例的id
      - ZOO_MY_ID=3
      # 整个zk集群的机器、端口列表
      - ZOO_SERVERS=server.1=zk1:2888:3888;2181 server.2=zk2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181

启动服务:

docker-compose -f zookeeper.yml up

访问Zookeeper:http://mylocalhost:8081/commands

有Json结果输出表示Zookeeper服务启动成功。

Spring Boot添加Dubbo依赖

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.3.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>


<!--使用阿里云的Maven源-->
<repositories>
    <repository>
        <id>aliyunmaven</id>
        <name>aliyun</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </repository>
</repositories>

dubbo-api代码

仅提供接口,不提供实现,让provider去实现代码,来验证provider和consumer之间的脱藕。

/service
	TimeService
package com.cc.service;

import java.time.LocalDateTime;

public interface TimeService {
    LocalDateTime getTime();
}

dubbo-provider代码

provider和consumer的pom.xml是一致的:

<dependencies>
    <!--springboot启动器-->
    <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>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

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

    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-registry-zookeeper</artifactId>
    </dependency>

    <dependency>
        <groupId>com.com.cc</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

application.yml

server:
  port: 8081

dubbo:
  application:
    name: provider-app
  registry:
    address: zookeeper://mylocalhost:2181

provider去实现类的接口,TimeServiceImpl.java:

package com.cc.service.impl;

import com.cc.service.TimeService;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDateTime;

// 使用这个注解,可以将该接口实现注册到注册中心
@DubboService
public class TimeServiceImpl implements TimeService {
    @Override
    public LocalDateTime getTime() {
        return LocalDateTime.now();
    }
}

启动类需要添加@EnableDubbo注解:

@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

dubbo-consumer代码

application.yml:

server:
  port: 8082

dubbo:
  application:
    name: consumer-app
  registry:
    address: zookeeper://mylocalhost:2181

对外提供访问接口:

@RestController
public class ConsumerController {
    @DubboReference
    private TimeService timeService;

    @GetMapping("/")
    public String getTime() {
        return timeService.getTime().format(DateTimeFormatter.ISO_DATE_TIME);
    }
}

记得添加@Dubbo注解:

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

验证

现在去访问:http://localhost:8082/

有结果则表示验证通过。