您的位置:首页 > 其它

Dubbo基于注解方式的配置

2017-11-18 18:15 369 查看
提供者(也就是服务端)的配置,先上配置文件代码:

[html] view
plain copy

<?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: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/context   

        http://www.springframework.org/schema/context/spring-context-2.5.xsd  

        http://code.alibabatech.com/schema/dubbo   

        http://code.alibabatech.com/schema/dubbo/dubbo.xsd   

        ">   

      

    <!-- 提供方应用信息,用于计算依赖关系 -->  

    <dubbo:application name="dubbo_provider" />  

  

    <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234"   

        /> -->  

  

    <!-- 使用zookeeper注册中心暴露服务地址 -->  

    <dubbo:registry address="zookeeper://127.0.0.1:2181" />  

  

    <!-- 用dubbo协议在20880端口暴露服务 -->  

    <dubbo:protocol name="dubbo" port="20880" />  

  

    <!-- 具体的实现bean  

    <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />  

    -->  

    <!-- 声明需要暴露的服务接口   

    <dubbo:service interface="com.unj.dubbotest.provider.DemoService" ref="demoService" />  

    -->  

      

    <!-- 使用注解方式暴露接口 -->     

    <dubbo:annotation package="com.dubbo.provide" />  

      

    <!-- 加入spring注解扫描 -->  

    <context:component-scan base-package="com.dubbo."/>  

</beans>   

下面这两句就是开启注解扫描的配置:

[html] view
plain copy

<!-- 使用注解方式暴露接口 -->     

    <dubbo:annotation package="com.dubbo.provide" />  

      

    <!-- 加入spring注解扫描 -->  

    <context:component-scan base-package="com.dubbo."/>  

package和base-package是要扫描的位置,这些配置的意思大部分都跟spring的配置差不多,这里就不多说了。

接下来来看下我们Service中代码注解的使用:

[html] view
plain copy

package com.dubbo.provide.user.service.impl;  

  

import org.springframework.stereotype.Component;  

  

import com.alibaba.dubbo.config.annotation.Service;  

import com.dubbo.provide.user.service.IUserService;  

  

  

/**  

 *   

 * @author LiZhiXian  

 * @version 1.0  

 * @date 2015-9-12 下午4:44:21  

 */  

@Component  

@Service  

public class UserService implements IUserService {  

  

    @Override  

    public String getUser() {  

        return "dubbo接口调用成功......";  

    }  

  

}  

其中@Component就是spring bean的注解,而@Service就是dubbo本身的注解,这两个注解合起来的意思就跟我们配置文件中先声明一个bean然后再声明接口暴露的意思是一样的。

接下来看下消费者(也就是客户端)的配置:

我这边使用的是springmvc框架,注意:这边接口的名称以及包路径,必须和提供者暴露的接口一致,可以直接将服务端的接口导出成jar,然后在客户端引入使用。



平时加配置文件的习惯都是按不同的功能来配置的,但是刚试了一下将dubbo单独配置,发现在注解扫描的时候扫描不到,有可能是跟spring的注解扫描发生了冲突,

最后我将dubbo的配置集成到了springMvc-servlet.xml配置中问题就解决了。

[html] view
plain copy

<?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:util="http://www.springframework.org/schema/util"  

    xmlns:mvc="http://www.springframework.org/schema/mvc"   

    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  

    xsi:schemaLocation="http://www.springframework.org/schema/beans     

    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  

    http://www.springframework.org/schema/context  

    http://www.springframework.org/schema/context/spring-context-3.2.xsd  

    http://www.springframework.org/schema/util   

    http://www.springframework.org/schema/util/spring-util-3.1.xsd  

    http://www.springframework.org/schema/mvc  

    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  

    http://code.alibabatech.com/schema/dubbo   

    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    

       

    <!-- <util:properties id="APP_PROPERTIES" location="classpath:sys.properties" local-override="true"/> -->  

      

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  

    <dubbo:application name="basic_dubbo_consumer" />  

  

    <!-- 使用zookeeper注册中心暴露服务地址 -->  

    <dubbo:registry address="zookeeper://127.0.0.1:2181" />  

      

    <!-- 启用spring mvc 注解-->  

    <context:annotation-config/>  

      

    <!-- MVC转换 -->   

    <mvc:annotation-driven/>  

    <mvc:default-servlet-handler/>  

      

    <dubbo:annotation package="com.frame." />  

      

    <!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->    

    <context:component-scan base-package="com.frame." />   

      

    <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->    

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  

  

    <!-- 转换成json对象配置 -->  

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"    

        p:ignoreDefaultModelOnRedirect="true" >    

            <property name="messageConverters">    

                <list>    

                    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>   

                </list>    

            </property>    

    </bean>   

      

    <!--  ③:对模型视图名称的解析,即在模型视图名称添加前后缀 -->    

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >    

         <property name="prefix" value="/WEB-INF/pages/"></property>    

         <property name="suffix" value=".jsp"></property>    

    </bean>   

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">     

         <!-- 该属性用来配置可上传文件的最大 byte 数 1G -->     

         <property name="maxUploadSize"><value>10737418240</value></property>   

    </bean>   

  

</beans>    

dubbo的配置我是加在了springMvc配置的最前面



接下来看下我在controller中的运用

[java] view
plain copy

package com.frame.controller;  

  

import org.springframework.stereotype.Controller;  

import org.springframework.web.bind.annotation.RequestMapping;  

import org.springframework.web.bind.annotation.ResponseBody;  

import com.alibaba.dubbo.config.annotation.Reference;  

import com.dubbo.provide.user.service.IUserService;  

  

/** 

 *  

 * @author LiZhiXian 

 * @version 1.0 

 * @date 2015-9-16 上午8:54:55 

 */  

@Controller  

@RequestMapping(value="/user/*")  

public class UserController {  

  

    @Reference  

    IUserService userService;//调用Dubbo暴露的接口  

  

    @RequestMapping(value="getUser.htm")  

    public @ResponseBody String getUser(){  

        return userService.getUser();  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: