java项目中使用dubbo实战

上篇文章讲到了如何搭建zookeeper+dubbo平台,但搭建平台的最终目的还是在项目中应用,接下来我再说下如何在java代码中实际应用。

dubbo作为RPC的管理工具,它的作用是管理服务的使用者调用服务提供者的情况。

在实际应用中,可建立一个独立的java project,里面的内容全部都是接口的定义,没有实现,然后让服务的提供者和使用者都依赖于这个项目,provider负责实现接口,而customer只需要直接调用接口所定义的方法即可,这个公共项目的代码不再赘述。

  • 服务提供者的配置:(新建配置文件spring-dubbo.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:tx="http://www.springframework.org/schema/tx"
    	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		http://www.springframework.org/schema/beans/spring-beans.xsd 
    		http://www.springframework.org/schema/tx 
    		http://www.springframework.org/schema/tx/spring-tx.xsd
    		http://www.springframework.org/schema/context
        	http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/mvc
    		http://www.springframework.org/schema/mvc/spring-mvc.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop.xsd
    		http://code.alibabatech.com/schema/dubbo
    		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    	<dubbo:application name="mywebappname" /> <!-- 设置应用的名字 -->
    	<dubbo:registry address="zookeeper://127.0.0.1:2181?backup=127.0.0.2:2181" /> <!-- 配置该项目指向的zookeeper服务器 -->
    	<dubbo:protocol name="dubbo" port="20888" /> <!-- 配置dubbo协议端口 -->
    	<bean id="userManualInterfaceImpl" class="com.tonson.dubbo.interface.impl.UserManualInterfaceImpl" /> <!-- 接口的具体实现类 -->
    	<dubbo:service interface="com.tonson.dubbo.interface.UserManualInterface" ref="userManualInterfaceImpl" timeout="3000" /> <!-- 声明具体是提供哪个接口的服务 -->
    </beans>
  • 服务调用者的配置:(新建配置文件spring-dubbo.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:tx="http://www.springframework.org/schema/tx"
    	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		http://www.springframework.org/schema/beans/spring-beans.xsd 
    		http://www.springframework.org/schema/tx 
    		http://www.springframework.org/schema/tx/spring-tx.xsd
    		http://www.springframework.org/schema/context
        	http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/mvc
    		http://www.springframework.org/schema/mvc/spring-mvc.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop.xsd
    		http://code.alibabatech.com/schema/dubbo
    		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    	<dubbo:application name="mywebappname" /> <!-- 设置应用的名字 -->
    	<dubbo:registry address="zookeeper://127.0.0.1:2181?backup=127.0.0.2:2181" /> <!-- 配置该项目指向的zookeeper服务器 -->
    	<dubbo:protocol name="dubbo" port="20888" /> <!-- 配置dubbo协议端口 -->
    	<dubbo:reference interface="com.tonson.dubbo.interface.UserManualInterface" id="userManualInterface"  check="false"/><!-- 定义要使用接口的bean -->
    </beans>

这样就已经将服务的provider和customer通过dubbo连接了起来,在java的编码中,跟正常的spring依赖注入的方式一样就可以。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值