Dubbo入门,DubboDemo怎么写?

dubbo发布版本和新特性

主要维护2.6.x和2.7.x两个大版本。

架构

在这里插入图片描述
节点角色说明:

节点角色说明
Provider服务提供方
Consumer服务消费方
Registry注册中心
Monitor统计服务调用次数和调用时间的监控中心
Container服务运行容器

调用关系说明:
0.服务容器负责启动,加载,运行服务提供着
1.服务提供者在启动时,向注册中心注册自己提供的服务
2.服务消费者在启动时,向注册中心订阅自己所需的服务
3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4.服务消费者,从提供地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据监控到监控中心。
注意:

注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在本地缓存了提供者列表
虚线都是异步访问,实线都是同步访问,蓝色都是初始化。红色都是程序运行过程中执行的。
所有的角色都可以在单独的服务器上,遵守特定的协议。

创建一个dubboDemo

1、创建一个maven Project

在这里插入图片描述

2、分别创建3个子模块。api提供接口,service实现接口,提供服务,consumer从注册中心调用接口。

在这里插入图片描述

3、在父工程中引入dubbo的jar包

<?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>

    <groupId>org.example</groupId>
    <artifactId>dubbo-study</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.6</version>
        </dependency>
    </dependencies>
    <modules>
        <module>dubbo-service</module>
        <module>dubbo-consumer</module>
        <module>dubbo-api</module>
    </modules>


</project>

4、在api工程中写demoService接口

在这里插入图片描述

5、在service工程的pom中引入api工程的依赖

<?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>dubbo-study</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <artifactId>dubbo-service</artifactId>

</project>

6、实现api中的DemoService接口

在这里插入图片描述

7、在service工程的resources下创建provider.xml,注册服务。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!--提供方应用名,用于在管理工具中区分是哪个提供方-->
    <dubbo:application name="hello-world-app"/>
    <!--使用multicast广播注册中心暴露服务地址    -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!--用dubbo协议在20880端口暴露服务    -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--声明需要暴露的服务接口   只能注册接口,引用实现类 -->
    <dubbo:service interface="service.DemoService" ref="demoService"/>
    <!--和本地bean一样实现服务-->
    <bean id="demoService" class="service.impl.DemoServiceImpl"></bean>
</beans>

8、在consumer工程的pom引入api工程的依赖(截图省略)

9、在consumer工程的resources下创建consumer.xml,调用服务。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,在管理工具中区别是哪个consumer不要与提供方一样 -->
    <dubbo:application name="consumer-hellowold-app"/>
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="service.DemoService"/> 
</beans>

10、开始测试,分别在service和consumer中写测试类,并启动进行测试

这是service中的test
在这里插入图片描述
这是consumer中的test
在这里插入图片描述

报错解决:

本地同时启动service和consumer会导致端口冲突,在resources下创建dubbo.properties修改端口

dubbo.application.qos.enable=true
dubbo.application.qos.port=33333
dubbo.application.qos.accept.foreign.ip=false
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值