duboo注解



一、背景

随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行。

当越来越的的接口与实现类的增加后,dubooxml配置会越来越多,为了防止几百几千行的代码,减少开发人员配置xml的工作量,使用duboo的注解模式,减少配置多出问题多的可能性!

二、阅读要求

由于IAF团队是一个技术框架研发团队,团队成员必须具备一定的技术底蕴。此文档提供最基本的测试用例,更深的原理,需要读者自行学习。

Duboo注解

接口类项目:DubboServiceInterface

仅仅是一个接口类项目!接口是普通接口!

注意:将接口类项目打包成jar分别放入服务端项目跟客户端项目!

服务端项目:DubboServiceProvider

实现类fooserviceImpl.java

package com.alibaba.dubbo.demo.imp;

 

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

import com.alibaba.dubbo.demo.DemoService;

@Service(version="1.0")

public classFooServiceImpl implements DemoService {

   @Override

   public String sayHello(Stringname) {

        return "Hello " + name;

   }

}

web.xml 配置扫描内容

<?xml version="1.0"encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

 <display-name>DubboServiceProvider</display-name>

   <servlet>

      <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

      <load-on-startup>1</load-on-startup>

   </servlet>

   <servlet-mapping>

      <servlet-name>spring</servlet-name>

      <url-pattern>*</url-pattern>

   </servlet-mapping>

  <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   </listener>

   <context-param>

      <param-name>contextConfigLocation</param-name>   <param-value>classpath*:applicationContext.xml</param-value>

   </context-param>

</web-app>

applicationContext.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.properties配置 -->

  <dubbo:application name="test" />

<!--     链接zookeeper -->

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

  <dubbo:consumer timeout="5000"/>

  <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->

       <dubbo:annotation package="com.unj.dubbotest.serviceImp"/>

 

   <!-- xml配置 声明需要暴露的服务接口 -->

<!--      <dubbo:serviceinterface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/> -->

     <!-- xml配置 :和本地bean一样实现服务-->

<!--     <bean id="demoService" class="com.unj.dubbotest.serviceImp.FooServiceImpl"/> -->

</beans>

测试类Provider

package com.alibaba.dubbo.test;

 

import java.io.IOException;

import org.junit.Before;

import org.junit.Test;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

   @Before

   publicvoid setUp() throws Exception {

   }

   @Test

   publicvoid testMain() throws IOException {

      ClassPathXmlApplicationContextcontext = new ClassPathXmlApplicationContext(

           newString[] { "applicationContext.xml" });

      context.start();

      System.in.read();//按任意键退出

   }

}

lib下的jar

客户端项目:DubboServiceConsumer

web.xml 配置扫描内容

applicationContext.xml文件

<?xml version="1.0"encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 

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

    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.properties配置 -->

  <dubbo:application name="xx" />

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

  <dubbo:consumer timeout="5000"/>

 <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->

   <dubbo:annotation package="com.unj.dubbotest.action" />

</beans>

测试类:Consumer

package com.unj.dubbotest.action;

import java.io.IOException;

import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

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

import com.alibaba.dubbo.demo.DemoService;

public class Consumer{

   @Reference(version= "1.0")

   privateDemoService demoService;

 

   @Test

    public void mainTest() throws IOException {

         ClassPathXmlApplicationContextcontext =new ClassPathXmlApplicationContext(

               newString[] {"applicationContext.xml"});

           context.start();

            demoService =(DemoService)context.getBean("demoService"); // 获取远程服务代理

           String hello = demoService.sayHello("world");         // 执行远程方法

           System.out.println( hello );                     // 显示调用结果

       }

}

lib下的jar

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值