Dubbo Spring集成

Dubbo是一个提供RPC远程调用的分布式服务框架。
http://dubbo.apache.org/en-us/docs/user/quick-start.html

他能做什么呢。

一 透明化的服务调用方式,像调用本地方法一样调用远程方法,无API侵入,只需在配置文件中配置服务。
二 软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
三 服务自动注册与发现,不需要写死服务提供方地址,注册中心根据接口名可以查询服务提供方的IP地址,且能够平滑增加或删除服务提供者。

Spring集成

在这里插入图片描述
与spring集成,这里选择了xml配置来实现。

在项目中,当我们在多个Tomcat部署不同的系统时, 例如A系统(TomcatA)想调用B系统(TomcatB)中的服务, 这时Dubbo就有了用武之地. 首先我们需要B系统在注册中心将自己的Url注册进去, 然后注册中心将Url返还给系统A, 那么系统A就可以调用了. 当然了我这里说的只是Dubbo的一种用法, 在这说的是Dubbo的远程调用功能.

需求:
假如我们需要修改一个电商前台的商品价格。那么我们可以在后台console修改。
product项目,console项目。
价格修改功能抽象成了接口,放在interface接口里。我们可以在console项目中调用producet项目的接口完成修改,但当这两个项目并非放在一个tomcat中时,我们就需要用到远程调用了。

结构:
在这里插入图片描述
在这里插入图片描述
思路:
我们无法直接调用interface接口,但我们可以通过dubbo完成同样功能。由于dubbo是无侵入的,我们可以在项目中增加一份配置文件。
针对服务提供方,也就是product项目,加上dubbo-demo-provider.xml文件,使用spring配置公开服务。

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- provider's application name, used for tracing dependency relationship -->
     <! -  provider的应用程序名称,用于跟踪依赖关系 - >
    <dubbo:application name="demo-provider"/> 
    
    <!-- use multicast registry center to export service --> 
    <! - 使用多播注册中心导出服务 - >
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    
    <!-- use dubbo protocol to export service on port 20880 --> 
    <! - 使用dubbo协议在端口20880上导出服务 - >
    <dubbo:protocol name="dubbo" port="20880"/>
    
    <!-- service implementation, as same as regular local bean --> 
    <! - 服务实现,与常规本地bean相同 - >
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/> 
    
    <!-- declare the service interface to be exported -->
 <! - 声明要导出的服务接口 - > 
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>
</beans>

该演示使用多播作为注册表,因为它很简单,不需要额外的安装。 如果您喜欢像zookeeper这样的注册,请在这里查看示例。https://github.com/apache/incubator-dubbo-samples

暴露服务后加载spring配置。
 1111111111111111111111111111111111111
项目结构
在这里插入图片描述
我们将服务接口公开后,我们再用spring配置引用远程服务。
dubbo-demo-consumer.xml

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

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="demo-consumer"/>
    <!-- use multicast registry center to discover service -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
</beans>

这个时候就可以调用demoService了。
在这里插入图片描述
启动:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值