天天看點

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

從監控中心可以看出提供者和消費都被正常注冊

繼續閱讀