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

二、Dubbo使用案例
Duboo注解
接口类项目:DubboServiceInterface

dubbo注解使用详解_spring



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

dubbo注解使用详解_java_02



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

dubbo注解使用详解_spring_03



实现类fooserviceImpl.java

dubbo注解使用详解_xml_04



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 配置扫描内容

dubbo注解使用详解_spring_05




<?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 配置

dubbo注解使用详解_dubbo_06





<?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

dubbo注解使用详解_dubbo_07



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包

dubbo注解使用详解_xml_08



客户端项目:DubboServiceConsumer

dubbo注解使用详解_spring_09



web.xml 配置扫描内容

dubbo注解使用详解_spring_10



applicationContext.xml文件

dubbo注解使用详解_dubbo_11



<?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

dubbo注解使用详解_spring_12





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包

dubbo注解使用详解_spring_13




获取海量视频

dubbo注解使用详解_java_14