SpringBoot+Dubbo+zookeeper 快速入门案例

Posted 龙源lll

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot+Dubbo+zookeeper 快速入门案例相关的知识,希望对你有一定的参考价值。

项目目录结构:

第一步:创建一个SpringBoot项目,这里选择Maven项目或者Spring Initializer都可以,这里创建了一个Maven项目(SpringBoot-Dubbo),pom.xml文件如下:

<?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 http://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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.ly</groupId>
    <artifactId>SpringBoot-Dubbo</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>

第二步:添加Module,以dubbo-provider为例

可以使用Maven创建或者使用Spring Initializr:

第三步:服务提供方dubbo-provider相关配置

在pom.xml文件中添加dubbo使用依赖,以及zookeeper的依赖:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringBoot-Dubbo</artifactId>
        <groupId>com.ly</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.qunhongtech</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-customer</name>
    <modelVersion>4.0.0</modelVersion>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>
        
        <!--dubbo依赖-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <!--zookeeper客户端依赖-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.13</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
</project>

修改配置文件:application.yml

server:
  port: 8081             #Tomcat端口号
dubbo:
  application:
    name: dubbo-provider #应用名称
  protocol:
    name: dubbo
    port: 20880          #dubbo服务暴漏的端口号
  registry:
    address: zookeeper://127.0.0.1:2181  #zookeeper服务的地址以及端口号

第四步:服务消费方dubbo-customer相关配置

在pom.xml文件中添加dubbo使用依赖,以及zookeeper的依赖:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringBoot-Dubbo</artifactId>
        <groupId>com.ly</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ly</groupId>
    <artifactId>dubbo-customer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-customer</name>


    <dependencies>

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

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.13</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
</project>

修改配置文件:application.yml

server:
	port: 8082             #Tomcat端口号
dubbo:
  application:
    name: dubbo-customer   #应用名称
  registry:
    address: zookeeper://127.0.0.1:2181  #zookeeper服务的地址以及端口号

第五步:在dubbo-api中新建接口,并在dubbo-provider中实现

在dubbo-api中创建一个接口:HelloService,使用时需要在dubbo-provider以及dubbo-customer
中添加依赖:

public interface HelloService 

    String hello();


在dubbo-provider中实现HelloService接口:

创建接口实现类HelloServiceImpl用来实现HelloService接口:实现代码如下,注意这里使用的注解@Service不是用spring的service注解,而是使用的dubbo的注解:import com.alibaba.dubbo.config.annotation.Service;

@Service //发布服务
public class HelloServiceImpl implements HelloService 

    @Override
    public String hello() 
        return "Hello World...";
    

并在启动器DubboProviderApplication中添加注解:@EnableDubbo

第六步:试着在dubbo-customer中远程调用HelloService接口:

新建一个HelloController类,代码如下,想要引入远程服务时需要使用dubbo的Reference注解:

@RestController
public class HelloController 

    @Reference //引用服务
    private HelloService helloService;

    @GetMapping("/hello")
    private String hello()

        System.out.println("Hello World...");

        return helloService.hello();
    


测试代码,先启动服务提供方:dubbo-provider,发生错误,因为我们还没有启动zookeeper

下载安装zookeeper,打开连接选择合适的版本直接安装即可:https://zookeeper.apache.org/releases.html



下载下来的压缩包可以在linux或windows中使用,这里以windows为例:

解压压缩包,找到bin目录并打开:
目录如下,找到zkServer.cmd,双击启动即可:


运行结果如下:

再次依次启动:服务提供发:dubbo-provider服务消费方:dubbo-customer,打开浏览器进行测试,结果如下:

以上是关于SpringBoot+Dubbo+zookeeper 快速入门案例的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合Dubbo&ZooKeeper

dubbo+zookeeper+springboot构建服务

Springboot整合Dubbo和Zookeeper

Springboot+dubbo+zookeeper整合

SpringBoot--Dubbo及Zookeeper介绍 & 安装zookeeper

第一次搭建springboot+dubbo+zookeeper项目小结