dubbo注解使用详解

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

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

[img]http://dl2.iteye.com/upload/attachment/0128/9505/93982900-3e98-3755-b4a7-166ff16a15f6.png[/img]


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

[img]http://dl2.iteye.com/upload/attachment/0128/9507/cebd3cab-b262-3c2d-9a03-d8dc2ea17904.png[/img]


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

[img]http://dl2.iteye.com/upload/attachment/0128/9509/990826a4-f88f-3868-912d-d1d720f76ca1.png[/img]


实现类fooserviceImpl.java


[img]http://dl2.iteye.com/upload/attachment/0128/9511/64fc8ef7-7c5d-334d-9746-e03602453df2.png[/img]

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


[img]http://dl2.iteye.com/upload/attachment/0128/9513/d59a2b7d-5d8e-3fe1-bb76-0d7d07a3428c.png[/img]

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



[img]http://dl2.iteye.com/upload/attachment/0128/9515/671bf567-f414-3416-9768-adda145042d6.png[/img]


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

[img]http://dl2.iteye.com/upload/attachment/0128/9517/ab6899f6-b18b-3d7d-a564-7afa7e618c76.png[/img]

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包

[img]http://dl2.iteye.com/upload/attachment/0128/9519/bca7e170-3460-350c-a1da-b0bc47810a92.png[/img]


客户端项目:DubboServiceConsumer

[img]http://dl2.iteye.com/upload/attachment/0128/9521/e5f75cce-8681-392d-89b7-4ba3136c1e0c.png[/img]


web.xml 配置扫描内容


[img]http://dl2.iteye.com/upload/attachment/0128/9523/4baaf0b0-7c6f-3f33-8e5b-b95f91e276a1.png[/img]

applicationContext.xml文件

[img]http://dl2.iteye.com/upload/attachment/0128/9525/47d5b46e-e7e8-38d7-936d-8fede02bf214.png[/img]


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


[img]http://dl2.iteye.com/upload/attachment/0128/9527/f7e0cfba-f3d3-32e7-9fb1-eb670fd24c22.png[/img]


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包

[img]http://dl2.iteye.com/upload/attachment/0128/9529/f15f50b8-1da9-393b-9932-f853b29af0a8.png[/img]


获取海量视频

[img]http://dl2.iteye.com/upload/attachment/0128/9531/be862cef-3448-370e-990a-0ddff1ec5465.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值