SpringBoot学习(六)-----集成Dubbo

集成Dubbo

阿里巴巴提供了Dubbo集成SpringBoot开源项目

可以在GitHub上 https://github.com/apache/dubbo-spring-boot-project查看入门教程

实现Dubbo项目

第一个项目(公共接口项目)

创建公共接口项目时,使用普通的maven项目创建即可

创建实体类

package com.ys.pojo;

import java.io.Serializable;

public class Student implements Serializable {
    private  static  final  long serialVersionUID=454545464L;
    private Integer id;
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Student() {
    }

    public Student(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

  创建公共接口

package com.ys.service;

import com.ys.pojo.Student;
//公共的接口项目
public interface StudentService {
//    利用id查询学生
    Student queryStudent(Integer id);
}

第二个项目(提供者)

使用Springboot框架,创建时不添加依赖

自己手动添加Dubbo的支持依赖以及zookeeper依赖

在pom.xml中添加公共接口项目的坐标

由于会造成日志的依赖重复添加,所以使用在zookeeper下使用<exclusions>标签(不包括依赖)

<?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.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ys</groupId>
    <artifactId>SpringBoot_011-Dubbo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot_011-Dubbo2</name>
    <description>SpringBoot_011-Dubbo2</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
<!--添加公共项目的坐标,即SpringBoot_011-Dubbo1的坐标-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>SpringBoot_011-Dubbo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
<!--添加Springboot集成Dubbo的依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
<!--        添加zookeeper客户端依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
          <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>



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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

编写公共接口的实现类

package com.ys.service;

import com.ys.pojo.Student;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

@Component//可省略
@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 500)
//@DubboService 使用注解暴露服务,连接接口
//@DubboService(interfaceClass = 指定公共接口,version = 自定义版本号,timeout = 超时时间单位ms)
public class StudentServiceImpl implements StudentService{
    @Override
    public Student queryStudent(Integer id) {
        Student student1=new Student(1001,"张三",25);
        Student student2=new Student(1002,"李四",26);
        if (id==student1.getId()){
            return student1;
        }else if (id==student2.getId()){
            return student2;
        }
        return null;
    }
}

编辑Springboot的配置文件

#配置服务名称
spring.application.name=studentService-provider

#配置dubbo扫描的包
dubbo.scan.base-packages=com.ys.service

#配置dubbo的协议(名字和端口号)
#dubbo.protocol.name=dubbo
#dubbo.protocol.port=20881

#dubbo注册中心
dubbo.registry.address=zookeeper://localhost:2181

最后在主启动类上加上注解@EnableDubbo

package com.ys;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
//@EnableDubbo  启用Dubbo
public class SpringBoot011Dubbo2Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBoot011Dubbo2Application.class, args);
    }

}

第三个项目(消费者)

使用消费者调用提供者的接口

使用Springboot框架创建,选择web依赖且自己添加依赖

由于会造成日志的依赖重复添加,所以使用在zookeeper下使用<exclusions>标签(不包括依赖)

<?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.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ys</groupId>
    <artifactId>SpringBoot_011-Dubbo3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot_011-Dubbo3</name>
    <description>SpringBoot_011-Dubbo3</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--添加公共项目的坐标,即SpringBoot_011-Dubbo1的坐标-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>SpringBoot_011-Dubbo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--添加Springboot集成Dubbo的依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!--        添加zookeeper客户端依赖-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
           <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

创建controller类,调用接口、方法

package com.ys.controller;

import com.ys.pojo.Student;
import com.ys.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 {
    //引用远程服务,把创建好的代理对象,注入给StudentService
    @DubboReference(interfaceClass = StudentService.class,version = "1.0")
    private StudentService service;
    @GetMapping("/queryStudent")
    public String queryStudent(){
       Student student= service.queryStudent(1001);
       return "调用远程接口,获取对象 :"+student;
    }
}

配置Springboot配置文件

#指定服务名称
spring.application.name=consumer-application

#指定注册中心
dubbo.registry.address=zookeeper://localhost:2181

最后在主启动类上加上注解@EnableDubbo

package com.ys;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
//启动Dubbo
public class SpringBoot011Dubbo3Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBoot011Dubbo3Application.class, args);
    }

}

测试

打开zookeeper客户端,进行测试

 先启动第二个项目(提供者),完成后启动第三个项目(消费者)

在网页上进行测试

 关于zookeeper客户端安装(Windows)

前往官网下载对应版本的安装包

Index of /apache/zookeeper (tsinghua.edu.cn)

 下载完成后,解压到自己想存放的文件夹内

打开文件进入到 conf目录下

 将zoo_sample.cfg复制一份,并改名叫zoo.cfg,对zoo.cfg点击编辑

没有编辑或者打不开,可以改变文件后缀,改为txt,改完后打开编辑保存再改回

文件配置如下:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=D:/java/apache-zookeeper-3.5.6-bin/apache-zookeeper-3.5.6-bin/data
# the port at which the clients will connect
clientPort=2181
admin.serverPort=8888
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

保存后,再进入bin目录下,点击zkServer.cmd进行客户端的启动

 如果出现闪退现象,可以右击zkServer.cmd点击编辑在最后一行加上pause

可以查看错误原因

zookeeper需要用到jdk所以,需要在电脑上配置好jdk的环境变量JAVA_HOME。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值