zoukankan      html  css  js  c++  java
  • duboo注解使用详解

    一、背景 
    随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行。 
    当越来越的的接口与实现类的增加后,duboo的xml配置会越来越多,为了防止几百几千行的代码,减少开发人员配置xml的工作量,使用duboo的注解模式,减少配置多出问题多的可能性! 

    二、Dubbo使用案例 
    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 class FooServiceImpl implements DemoService { 
    @Override 
    public String sayHello(String name) { 
            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/javaee http://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:service interface="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; 
    import org.springframework.context.support.ClassPathXmlApplicationContext; 
    public class Provider { 
    @Before 
    public void setUp() throws Exception { 

    @Test 
    public void testMain() throws IOException { 
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 
    new String[] { "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") 
    private DemoService demoService; 

    @Test 
    public  void mainTest() throws IOException { 
    ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext( 
    new String[] {"applicationContext.xml"}); 
            context.start(); 
             demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理 
            String hello = demoService.sayHello("world"); // 执行远程方法 
            System.out.println( hello ); // 显示调用结果 
        } 

    lib下的jar包 

     


    获取海量视频 

  • 相关阅读:
    LeetCode算法训练
    重新整理自己的博客
    VS2019制作安装包与ClickOnce部署
    2020年系统架构设计师考试通过总结
    合并两个有序数组为一个新的有序数组
    Inno Setup 出现 the drive or unc share you selected does not exist or is not accessible 解决记录
    60秒定位问题,十倍程序员的Debug日常
    这几个神秘参数,教你TDengine集群的正确使用方式
    存储成本仅为OpenTSDB的1/10,TDengine的最大杀手锏是什么?
    基于TDengine进行睿信物联网平台的迁移改造
  • 原文地址:https://www.cnblogs.com/happyhuangjinjin/p/8459973.html
Copyright © 2011-2022 走看看