Spring boot 2.1 + Dubbo 2.7.1 + Zookeeper 3.4.14 Demo 搭建

Zookeeper 安装

  • 下载:zookeeper-3.4.14.tar.gz

  • 解压

  • 配置:
    将 conf/zoo_sample.cfg 复制为 zoo.cfg
    修改 zoo.cfg

    dataDir=D:\\zookeeper-3.4.14\\data
    dataLogDir=D:\\zookeeper-3.4.14\\log
    

    创建文件夹 data log

  • 启动:
    bin/zkServer.cmd
    Spring boot 2.1 + Dubbo 2.7.1 + Zookeeper 3.4.14 Demo 搭建_第1张图片

Dubbo 样例代码

  • 创建项目
    父项目 dubbo-demo 子项目 dubbo-demo-api、dubbo-demo-consumer、dubbo-demo-provider

  • 父项目结构
    Spring boot 2.1 + Dubbo 2.7.1 + Zookeeper 3.4.14 Demo 搭建_第2张图片

  • 父项目pom.xml

    
    <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>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.1.3.RELEASEversion>
            <relativePath/> 
        parent>
    
        <groupId>org.apache.dubbogroupId>
        <version>1.0-SNAPSHOTversion>
        <modelVersion>4.0.0modelVersion>
        <artifactId>dubbo-demoartifactId>
        <packaging>pompackaging>
    
        <modules>
            <module>dubbo-demo-apimodule>
            <module>dubbo-demo-consumermodule>
            <module>dubbo-demo-providermodule>
        modules>
    
        <properties>
            <java.version>1.8java.version>
            <spring-boot.version>2.1.1.RELEASEspring-boot.version>
            <dubbo.version>2.7.1dubbo.version>
        properties>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>${dubbo.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubboartifactId>
                <version>${dubbo.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-recipesartifactId>
                <version>2.7.0version>
            dependency>
    
        dependencies>
    
        <build>
            <plugins>
    
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
    
            plugins>
        build>
    project>
    
  • debbo-demo-api 项目提供接口
    Spring boot 2.1 + Dubbo 2.7.1 + Zookeeper 3.4.14 Demo 搭建_第3张图片

  • debbo-demo-api 项目pom.xml很简单,继承父亲,完全不用其他的

    
    <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>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-demoartifactId>
            <version>1.0-SNAPSHOTversion>
        parent>
    
        <artifactId>dubbo-demo-apiartifactId>
        <packaging>warpackaging>
        <version>1.0-SNAPSHOTversion>
        <modelVersion>4.0.0modelVersion>
    
    project>
    
  • dubbo-demo-provider 引入 dubbo-demo-api ,实现接口
    Spring boot 2.1 + Dubbo 2.7.1 + Zookeeper 3.4.14 Demo 搭建_第4张图片

  • 配置文件 application.properties

    # demo-provider配置文件
    
    # 端口配置
    server.port=8081
    
    ## Dubbo 服务提供者配置
    # provider应用名称
    spring.application.name=demo-provider
    
    # Dubbo组件扫描的基础包
    dubbo.scan.base-packages=org.apache.dubbo.demo
    
    dubbo.application.name=dubbo-provider
    
    #注册中心地址
    dubbo.registry.address=zookeeper://192.168.2.101:2181
    
    # Dubbo 协议与端口
    dubbo.protocol.name=dubbo
    dubbo.protocol.port=12345
    
  • pom.xml

    
    <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>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-demoartifactId>
            <version>1.0-SNAPSHOTversion>
        parent>
    
        <version>1.0-SNAPSHOTversion>
        <modelVersion>4.0.0modelVersion>
        <artifactId>dubbo-demo-providerartifactId>
    
        <dependencies>
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-demo-apiartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
    
        dependencies>
    
    project>
    
    
  • spring boot 启动类

    package org.apache.dubbo.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
  • dubbo-demo-consumer 作为服务的消费者(方法调用方)引入 dubbo-demo-api ,调用接口

    Spring boot 2.1 + Dubbo 2.7.1 + Zookeeper 3.4.14 Demo 搭建_第5张图片
    一个普通的Controller

  • 配置文件 application.properties\

    # demo-consumer配置文件
    
    #端口配置,为防止端口冲突,该模块端口使用18080
    server.port=18080
    
    dubbo.application.name=dubbo-consumer
    
    #注册中心地址
    dubbo.registry.address=zookeeper://localhost:2181
    
    # 应用名称,配置模块项目名称即可
    spring.application.name=demo-consumer
    
  • pom.xml

    
    <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>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-demoartifactId>
            <version>1.0-SNAPSHOTversion>
        parent>
    
        <version>1.0-SNAPSHOTversion>
        <modelVersion>4.0.0modelVersion>
        <artifactId>dubbo-demo-consumerartifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-demo-apiartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
        dependencies>
    
    project>
    
  • sprint boot 启动类

    package org.apache.dubbo.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    }
    
  • 先启动 provider,再启动 consumer,可以在zookeeper控制台看见连接的输出
    Spring boot 2.1 + Dubbo 2.7.1 + Zookeeper 3.4.14 Demo 搭建_第6张图片

  • 访问 http://localhost:18080/hello/world 即可看见神奇的结果
    Spring boot 2.1 + Dubbo 2.7.1 + Zookeeper 3.4.14 Demo 搭建_第7张图片

Dubbo 监控页面

  • GitHub 地址 https://github.com/apache/incubator-dubbo-admin
    git clone https://github.com/apache/incubator-dubbo-admin.git
    
  • 修改配置文件 dubbo-admin-server/src/main/resources/application.properties
    admin.registry.address=zookeeper://127.0.0.1:2181
    admin.config-center=zookeeper://127.0.0.1:2181
    admin.metadata-report.address=zookeeper://127.0.0.1:2181
    
  • 打包
    mvn clean package
    
  • 启动
    cd dubbo-admin-distribution/target
    java -jar dubbo-admin-0.1.jar
    
  • 访问 localhost:8080,神奇的页面就有了,也能看见我们注册的服务
    Spring boot 2.1 + Dubbo 2.7.1 + Zookeeper 3.4.14 Demo 搭建_第8张图片

问题与总结

  • 父项目的pom的packaging方式为pom
  • 我的消费者是个web Controller,所以 consumer 的 pom packaging 方式为 war
  • 在父pom里面加上下面的依赖的原因是会报一个少包的异常
    <dependency>
        <groupId>org.apache.curatorgroupId>
        <artifactId>curator-recipesartifactId>
        <version>2.7.0version>
    dependency>
    
  • 在provider里面用的 @Service 注解是用的Duddo的注解包,并且指定了version,在调用方法的地方也是需要指定 version 的
  • 项目的方式是 zookeeper 的,也可以用其他的,或者采用直连的方式,直连的时候需要在调用service的地方指定 url=
  • 在我的controler里面采用的是 @PathVariable 的方式从路径里面获取参数,如果没有 name 的参数,会找不到,我还以为可以映射进来 把 name 映射成 null
  • 期待后续将老项目迁移过来

你可能感兴趣的:(spring-boot,dubbo)