• Dubbo在项目中的两种使用方法


    一、声明需要暴露的服务接口方式

    1.1 service服务层

    【applicationContext-service.xml配置文件】

      在applicationContext-service.xml中需要配置基本的包扫描,且使用一个服务就要暴露一个 服务接口

    <context:component-scan base-package="com.pinyougou.sellergoods.service.impl" />
    
    <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
       
    <dubbo:application name="pinyougou-sellergoods-service"/>  
    
    <dubbo:registry address="zookeeper://192.168.25.148:2181"/>
    <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.pinyougou.sellergoods.service.BrandService" ref="brandServiceImpl" timeout="600000"/> <dubbo:service interface="com.pinyougou.sellergoods.service.SpecificationService" ref="specificationServiceImpl" timeout="600000"/>

    【impl实现类中使用@Service注解】

      这里的@Service来自org.springframework.stereotype.Service;包下

    1.2 web层

    【springmvc.xml】

      在springmvc.xml中也需要配置基本的包扫描,且使用哪个服务就引用哪个服务

    <context:component-scan base-package="com.pinyougou.manager.controller" />
    <!-- 引用dubbo 服务 -->
    <dubbo:application name="pinyougou-manager-web" />
    <dubbo:registry address="zookeeper://192.168.25.148:2181"/>
    <dubbo:reference interface="com.pinyougou.sellergoods.service.BrandService" id="brandService" />
    <dubbo:reference interface="com.pinyougou.sellergoods.service.SpecificationService" id="specificationService" />

    【在controller中引用service服务时使用@Resource注解】

    @Resource
    private BrandService brandService ;

      @Resource来自于javax.annotation.Resource包下

    二、使用dubbo注解方式(推荐)

      说明 : 使用此种方式不需要暴露服务接口,也不需要引用服务接口,直接使用dubbo注解即可

    2.1 service服务层

    【applicationContext-service.xml配置文件】

    <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>  
    <dubbo:application name="pinyougou-sellergoods-service"/>  
    <dubbo:registry address="zookeeper://192.168.25.148:2181"/>
    <dubbo:annotation package="com.pinyougou.sellergoods.service.impl" />

    【impl实现类使用@service注解】

      注意,@Service来自com.alibaba.dubbo.config.annotation.Service;包下

    2.2 web层

    【springmvc.xml】

    <!-- 引用dubbo 服务 -->
    <dubbo:application name="pinyougou-manager-web" />
    <dubbo:registry address="zookeeper://192.168.25.148:2181"/>
    <dubbo:annotation package="com.pinyougou.manager.controller" />

    【在controller中使用@Reference引用服务】

    @Reference
    private BrandService brandService ;

      @Reference来自com.alibaba.dubbo.config.annotation.Reference;包下

    转自:https://blog.csdn.net/A_jungle/article/details/83045507

  • 相关阅读:
    图表处理ZedGraph
    12月16日
    Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly
    (转)EXCEL2007存储格式xlsx
    树型数据结构设计处理
    取得表名以h_开头的表,要使用left,不要使用like
    北京挪动推出神州行5元卡套餐
    手机出口爆炸性增长 品牌输出比例大幅上升
    中电信将进驻国美电器统统门店 启动一体化营销
    联通业务厅开进国美门店
  • 原文地址:https://www.cnblogs.com/yft-javaNotes/p/10938926.html
Copyright © 2020-2023  润新知