Dubbo初始

Dubbo架构

image-20220311161430200

消费者:调用提供者,但是不能直接调,需要借助注册中心

节点角色说明:

●Provider:暴露服务的服务提供方
●Container: 服务运行容器
●Consumer: 调用远程服务的服务消费方
●Registry:服务注册与发现的注册中心
●Monitor:统计服务的调用次数和调用时间的监控中心

过程:

0start:服务的提供者要运行在一个容器里面,比如运行在tomcat里面,需要将tomcat启动起来。

1注册:启动起来之后,该服务就会注册到注册中心里(将服务调用的ip、端口、服务发布url放到注册中心里面去。)。

2.subscribe:我想调用提供者提供的服务,我这时去找注册中心去找(告诉服务中心将服务的相关信息给消费者)

3.notify:消费者要一次服务,注册中心给一次。

这时消费者拿到服务的信息

4.invoke:就是rpc的过程,进行调用。不用我们管,dubbo内部自动实现。

5.Monitor:服务监控。统计某个服务调用了多少次。

asyn:异步。sync:同步。只有rpc调用时同步的,其他的都是异步的。

Zookeeper安装

先安装java1.8

解压,进入配置文件夹,复制配置文件,并修改里面的配置使其生效。复制会话窗口,创建目录,将该目录复制修改到配置文件里面。启动zk

zk默认端口为2181

Mode:standalone(当前没有搭建集群,是单节点在运行)

Dubbo快速入门

image-20220311165233891

注意这里的controller调用service是远程调用,是两个工程分别部署在两台机器上。

jar包依赖

        <!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubb -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
        <!--ZooKeeper客户端实现 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>

image-20220311201738613

开始配置dubbo

<?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" xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

   <!--spring包扫描-->
   <!--<context:component-scan base-package="com.itheima.service"/>-->

   <!--dubbo的配置-->
   <!--1、配置项目的名称,唯一-->
   <dubbo:application name="dubbo-service"/>
   <!--2、配置注册中心的地址-->
   <dubbo:registry address="zookeeper://101.42.248.44:2181"/>
   <!--3、配置dubbo包扫描-->
   <dubbo:annotation package="com.itheima.service.impl"/>


</beans>

image-20220311202324153

添加spring的配置,让其扫描加载刚在配置dubbo的配置文件applicationContext.xml

在这里插入图片描述

配置zk

上面的扫描时扫面springmvc的注解

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--打开注解驱动-->
    <mvc:annotation-driven/>
    <!--扫描包-->
    <context:component-scan base-package="com.itheima.controller"/>

    <!--dubbo的配置-->
    <!--1、配置项目的名称,唯一-->
    <dubbo:application name="dubbo-web">
        <dubbo:parameter key="qos.port" value="33333"/>
    </dubbo:application>
    <!--2、配置注册中心的地址-->
    <dubbo:registry address="zookeeper://101.42.248.44:2181"/>
    <!--3、配置dubbo包扫描-->
    <dubbo:annotation package="com.itheima.controller"/>
</beans>

image-20220311205118002

创建一个公共接口模块,减少重复代码开发,易于接口调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值