Dubbo+Zookeeper+SpringMVC:基于注解的配置方法

来源:互联网 发布:淘宝广场舞服装 编辑:程序博客网 时间:2024/06/07 19:18

一.环境说明

  • Windows 10 1709
  • JDK 1.8.0_144
  • IDEA 2017.3
  • Dubbo 2.5.7
  • Zookeeper 3.4.10
  • SpringMVC 4.3.12.RELEASE
  • Maven 3.5.0

二.问题说明

由于我的IDEA在使用xml配置式配置dubbo远程服务的时候,总是会报个红线,提示这个dubbo的服务bean不存在.虽然不影响运行与实际效果,但本着追求极致的宗旨,总得做点什么消除红线.今天得空,研究了下dubbo的基于注解的配置,也参考了大量前辈的博客,特整理出一份本人亲测没问题的方法,给遇到同样问题的或者想要使用基于注解的方法的同行们提供便利.

三.那就开始吧

dubbo服务的提供方(Provider)

import com.alibaba.dubbo.config.annotation.Service;import org.springframework.stereotype.Component;...//最好将dubbo的service放在spring的上面,不然好像会出问题@Service@Componentpublic class ContentCategoryServiceImpl implements ContentCategoryService {...}

服务方的spring配置文件

<?xml version="1.0" encoding="UTF-8"?><!--Spring的约束文件 使用那些模块就标记那些模块的约束文档 --><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"       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://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <!--扫描service -->    <!--先让spring扫描,再让dubbo扫描 -->    <context:component-scan base-package="xin.csqsx.content.service.impl"/>    <!--使用dubbo发布服务 -->    <!--扫描dubbo的service注解 -->    <dubbo:annotation package="xin.csqsx.content.service.impl" />    <!--提供方应用信息,用于计算依赖关系 -->    <dubbo:application name="ddshop_content_service"/>    <dubbo:registry protocol="zookeeper" address="zookeeper服务所在服务器的ip地址:2181" />    <!--用dubbo协议暴露20880端口 -->    <dubbo:protocol name="dubbo" port="20880" />    <!--设置超时时间 -->    <dubbo:provider timeout="600000" />    <!--声明需要暴露的服务接口 -->    <dubbo:service interface="xin.csqsx.content.service.ContentCategoryService" ref="contentCategoryServiceImpl" />    <dubbo:service interface="xin.csqsx.content.service.ContentService" ref="contentServiceImpl" /></beans>

dubbo服务的消费者(customer)

springMVC的配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"       xmlns:context="http://www.springframework.org/schema/context"       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://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <mvc:annotation-driven />    <!--放行静态资源 -->    <mvc:default-servlet-handler/>    <!--先让dubbo扫描controller层,可以避免出现NullPoint空指针问题 -->    <dubbo:annotation package="xin.csqsx.manager.controller" />    <!--配置扫描包 -->    <context:component-scan base-package="xin.csqsx.manager.controller"/>    <!--引用dubbo服务 -->    <dubbo:application name="ddshop_manager_web" />    <!--设置超时时间 -->    <dubbo:consumer timeout="600000" />    <dubbo:registry protocol="zookeeper" address="zookeeper所在服务器的ip地址:2181" />    <!--视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/html/"/>        <property name="suffix" value=".html"/>    </bean></beans>

dubbo消费者端controller层

import com.alibaba.dubbo.config.annotation.Reference;import org.springframework.stereotype.Controller;@Controller@RequestMapping("/advent")public class AdventController {    //使用reference注解注入dubbo服务    @Reference    private ContentCategoryService contentCategoryService;

这样应该就能完成了,如果有问题,可以发送邮件到mark4043425@gmail.com.我们一起探讨探讨.


2017/11/22
Lucifer

原创粉丝点击