通过Dubbo注解实现RPC调用

启动Dubbo服务有2个方式,1是通过xml配置,2是通过注解来实现,这点和Spring相似。

1、采用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="mall-seller-provider"  />
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
   <!--  <dubbo:registry address="multicast://xx.xx.xx.xx:1234" /> -->
 
     <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://xx.xx.xx.xx:2181" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口(注意是接口,不是实现类) -->
    <dubbo:service interface="cn.itcats.mall.seller.service" ref="sellerService" />
    
    <!--注入实现类,id和上面的暴露的服务接口ref要一致,dubbo就是通过这个来注册对应的服务 -->
    <bean id="sellerService" class="cn.itcats.mall.seller.service.impl.GoodsServiceImpl"></bean>
    
</beans>

消费方调用方式配置如下:

<?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="mall-seller-consumer"/>
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!-- <dubbo:registry address="multicast://xx.xx.xx.xx:1234" /> -->
    <dubbo:registry address="zookeeper://xx.xx.xx.xx:2181" />
    <!-- 生成远程服务代理,可以和本地bean一样调用 -->
    <dubbo:reference id="sellerService" interface="cn.itcats.mall.seller.service" />
</beans>

总结:

<dubbo:service interface="cn.itcats.mall.seller.service" ref="sellerService" />把接口暴露出去,再ref引用接口实现类,在程序启动的时候会自动注册到zookeeper


<dubbo:reference id="sellerService" interface="cn.itcats.mall.seller.service" />   消费方引用远端提供的sevice接口

2、通过注解配置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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
    <!--一般为maven工程名 -->
    <dubbo:application name="mall-seller-service"/>  
    <dubbo:registry address="zookeeper://10.37.xx.xx:2181"/>
    <!-- 书写提供方的实现类包名 -->
    <dubbo:annotation package="cn.itcats.seller.service.impl" />  

</beans>

消费方:

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 引用dubbo 服务 -->
        <!--一般为maven工程名 -->
	<dubbo:application name="pinyougou-shop-web" />
	<dubbo:registry address="zookeeper://10.37.129.3:2181"/>
        <!-- 书写消费方的包名 -->
	<dubbo:annotation package="com.pinyougou.shop.controller" /> 

Class类 xxxServiceImpl上引入的注解为com.alibaba.dubbo.config.annotation的Service,而不是springframework包中的service,这样Service服务就被注册到dubbo中了

总结:

使用注解方式在xml配置方面好像没有什么区别。
但是在Class类上 xxxController中依然引入springframework中的 @Controller注解,但是在引用某service服务时候,不使用spring framwork提供的@Autowired或@Resource注解,
而采用
com.alibaba.dubbo.xxx中的@Reference注入Service。
通过@Reference注解,dubbo会在扫描的时候会自动帮我们代理接口,然后通过rpc调用远程服务。

猜你喜欢

转载自blog.csdn.net/itcats_cn/article/details/81807526