dubbo初学 dubbo整合zookeeper

初学dubbo,踩了很多坑,网上的文章真的就没有能用的,害,记录一下

适合初学者阅读
首先zookeeper是肯定要装好的,zookeeper如果我们装在阿里云服务器上,记得关闭防火墙与在安全组中打开相应端口

关闭防火墙
systemctl disable firewalld
查看防火墙状态
systemctl status firewalld

定义个父工程

pom

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modules>
        <module>dubbo_api</module>
        <module>dubbo_modular</module>
        <module>dubbo_provider</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <revision>1.0.0-SNAPSHOT</revision>
        <dubbo.version>3.0.6</dubbo.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>com.fancy</groupId>
                <artifactId>dubbo_modular</artifactId>
                <version>${revision}</version>
            </dependency>
            <dependency>
                <groupId>com.fancy</groupId>
                <artifactId>dubbo_api</artifactId>
                <version>${revision}</version>
            </dependency>
            <dependency>
                <artifactId>dubbo_modular</artifactId>
                <groupId>com.fancy</groupId>
                <version>${revision}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

yml
timeout时间我们一定要设置长一点,否则很有可能还没连接上就断了,因为默认时间很短,好像是1s?

server:
  port: 8090
dubbo:
  application:
    name: provider-dubbo
  registry:
    address: zookeeper://your ip:2181
#    address: multicast://224.5.6.7:1234
    timeout: 50000
  protocol:
    name: dubbo
    port: 20881
  scan:
    base-packages: com.fancy.api

api module

api接口定义层,随便定义个接口就行,记得install,给消费者提供者引入
在这里插入图片描述

服务提供者 module

pom
引入dubbo-dependencies-zookeeper就可以了!别去引什么别的包,我真服了,引别的zookeeper的工具包真的就一直错,如果只用zookeeper我们也可以只引zookeeper-client,那些封装好的包我也不知道咋说,我是真用不起来,各种错误

<dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <artifactId>dubbo_api</artifactId>
            <groupId>com.fancy</groupId>
        </dependency>
        <dependency>
            <artifactId>dubbo_modular</artifactId>
            <groupId>com.fancy</groupId>
        </dependency>
    </dependencies>

写个服务 DubboService 注册到远程注册中心的service
在这里插入图片描述
服务提供者就写完啦

服务消费者重新起个工程

pom

    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fancy</groupId>
            <artifactId>dubbo_api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

yml

server:
  port: 8091
dubbo:
  application:
    name: consumer-dubbo
  registry:
    address: zookeeper://your ip:2181
    timeout: 50000
  protocol:
    name: dubbo
    port: 20882
  scan:
    base-packages: com.fancy.api

写个测试类,DubboReference从远程注册中心拿
在这里插入图片描述
over,调用下即可,有问题欢迎留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值