天天看点

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 inter 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 inter id="userManualInterface"  check="false"/><!-- 定义要使用接口的bean -->
    </beans>
               

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