天天看点

SpringBoot整合dubbo+zookeeper(详细步骤)SpringBoot整合dubbo+zookeeper

SpringBoot整合dubbo+zookeeper

项目源码:https://gitee.com/residual-temperature/dubbo-zookeeper-springboot

前期准备

  1. 要有zookeeper环境如果没有可以去zookeeper官网下载
  2. 要搭建好dubbo-admin如果没有可以去https://github.com/apache/dubbo-admin下载搭建
  3. 创建三个springboot项目,目录如下
    SpringBoot整合dubbo+zookeeper(详细步骤)SpringBoot整合dubbo+zookeeper
  4. 要有dubbo相关的jar包,jar导错会不能运行
<!--dubbo相关-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>

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

具体实现

service项目下

service项目下一般放入实体类、枚举类、service接口类等

service具体目录

SpringBoot整合dubbo+zookeeper(详细步骤)SpringBoot整合dubbo+zookeeper

Student类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
    private int id;
    private String name;
    private String className;
}
           

StudentServiec接口

public interface StudentService {

    //根据学生id查询学生信息
    public Student findStudentById(int id);

}
           

provider项目下

提供者一般放入service接口实现类、持久层的实现

具体目录

SpringBoot整合dubbo+zookeeper(详细步骤)SpringBoot整合dubbo+zookeeper

pom.xml文件中的具体jar包

<!--dubbo相关-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>

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

        <!-- service引入 -->
        <dependency>
            <groupId>com.su</groupId>
            <artifactId>dubbo-springboot-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
           

application.properties配置文件

server.port=8100
#声明dubbo服务提供者的名称: 保证唯一性
dubbo.application.name = dubbo-provider-springboot
#声明dubbo的端口号
dubbo.protocol.port = 20880
#声明dubbo的协议
dubbo.protocol.name = dubbo
#注册中心
dubbo.registry.address = zookeeper://localhost:2181 #也可以填服务器的地址
#要扫描的包
dubbo.scan.basePackages  = com.su.provider.service.impl
           

StudentServiceImpl类

import com.su.pojo.Student;
import com.su.service.StudentService;
import org.apache.dubbo.config.annotation.DubboService;

//规定版本号version,这个是实现一个一个提供者多个实现
//interfaceClass,引用远程接口服务通过放射引用
@DubboService(version = "1.0.0",interfaceClass = StudentService.class)
public class StudentServiceImpl implements StudentService {
    @Override
    public Student findStudentById(int id) {
        Student student = new Student();
        student.setId(id);
        student.setName("小夏");
        student.setClassName("一班");
        return student;
    }
}
           

提示:这个里的 @DubboService注解是新版本dubbo jar包支持的,如果是旧版本的dubbo jar包应该用 @Service 但是要注意是dubbo下的Service注解而不是springboot自带的service注解

consumer项目下

消费者中一般放入controller、handler、interceptor

具体目录

SpringBoot整合dubbo+zookeeper(详细步骤)SpringBoot整合dubbo+zookeeper

pom.xml文件中的具体jar包

<!--dubbo相关-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>

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

        <!-- service引入 -->
        <dependency>
            <groupId>com.su</groupId>
            <artifactId>dubbo-springboot-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
           

application.properties配置文件

server.port=8200
#声明dubbo服务提供者的名称: 保证唯一性
dubbo.application.name = dubbo-consumer-springboot
#注册中心
dubbo.registry.address = zookeeper://localhost:2181
#要扫描的包
dubbo.scan.base-packages = com.su.consumer.controller
           

StudentController类

import com.su.pojo.Student;
import com.su.service.StudentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {

    //与之前提供者提供的版本号对应
    @DubboReference(version = "1.0.0")
    private StudentService studentService;

    @GetMapping("/get")
    public Student findStudentById(int id){
       return studentService.findStudentById(id);
    }
}
           

提示:这个里的 @DubboReference注解是新版本dubbo jar包支持的,如果是旧版本的dubbo jar包应该用 @Reference 记得要匹配版本号

运行测试

先启动提供者,后启动消费者

运行结果

SpringBoot整合dubbo+zookeeper(详细步骤)SpringBoot整合dubbo+zookeeper

启动监控中心查看

SpringBoot整合dubbo+zookeeper(详细步骤)SpringBoot整合dubbo+zookeeper
SpringBoot整合dubbo+zookeeper(详细步骤)SpringBoot整合dubbo+zookeeper
SpringBoot整合dubbo+zookeeper(详细步骤)SpringBoot整合dubbo+zookeeper

从监控中心可以看出提供者和消费都被正常注册

继续阅读