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包

获取海量视频

duboo注解使用详解的更多相关文章
- 转:springmvc常用注解标签详解
Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...
- Java 注解用法详解——@SuppressWarnings
转自: https://www.cnblogs.com/fsjohnhuang/p/4040785.html Java魔法堂:注解用法详解——@SuppressWarnings 一.前言 编码时我 ...
- @SuppressWarnings注解用法详解
@SuppressWarnings注解用法详解 今天来谈谈@SuppressWarnings注解的作用. J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条 ...
- Spring 注解@Value详解
一.spring(基础10) 注解@Value详解[1] 一 配置方式 @value需要参数,这里参数可以是两种形式: [html] view plain copy @Value("#{co ...
- Java注解(Annotation)详解
转: Java注解(Annotation)详解 幻海流心 2018.05.23 15:20 字数 1775 阅读 380评论 0喜欢 1 Java注解(Annotation)详解 1.Annotati ...
- springmvc常用注解标签详解
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...
- android注解使用详解(图文)
在使用Java的SSH框架的时候,一直在感叹注解真是方便啊,关于注解的原理,大家可以参考我的另一片文章Java注解详解.最近有时间研究了android注解的使用,今天与大家分享一下. android中 ...
- springmvc常用注解标签详解【转】
转载自:http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Disp ...
- spring security 注解@EnableGlobalMethodSecurity详解
1.Spring Security默认是禁用注解的,要想开启注解,需要在继承WebSecurityConfigurerAdapter的类上加@EnableGlobalMethodSecurity注解 ...
随机推荐
- 工具类--MD5Utils
public class MD5Utils { private static final String[] HEX_DIGITS = { "0", "1", & ...
- adb devices unauthorized的解决办法
Hi, trying to launch adb but get: daemon not running. starting it now on port * daemon started s ...
- BZOJ 2460 & 洛谷 P4570 [BJWC2011]元素 (线性基 贪心)
题目链接: 洛谷 BZOJ 题意 给定 \(n\) 个矿石,每个矿石有编号和魔力值两种属性,选择一些矿石,使得魔力值最大且编号的异或和不为 0. 思路 线性基 贪心 根据矿石的魔力值从大到小排序. 线 ...
- org.apache.ibatis.builder.IncompleteElementException: Could not find result map com.abc.beans.Minister
使用mybatis进行一对多嵌套查询时出错,错误原因: <select id="findMinisterById" resultMap="com.abc.beans ...
- python读取文件报错:pandas.errors.ParserError: iterator should return strings, not bytes (did you open the file in text mode?)
python 读取csv文件报错问题 import csv with open('E:/Selenium2script/DDT模块/test.csv','rb') as f: readers = cs ...
- 关于memset赋值无穷大无穷小
memset(a,,sizeof(a)); 即得到无穷大. memset(a,,sizeof(a)); 即得到无穷小,与上述的值互为相反数. memset(a,,sizeof(a)); 即近似为第一个 ...
- The J2EE Architecture
- 一个服务io占满,服务器无响应
(1).服务器io占满,服务无响应, sar -q -f /var/log/sa/sa28 上图显示plist-sz 增加了一倍 plist-sz 说明:进程列表中的进程(processes)和线程 ...
- 深度探索C++对象模型读书笔记-第六章执行期语意学
在函数中,编译器会帮助将析构函数(Destructor) 安插在相应的位置.对于函数中的局部对象,会将析构函数安插在对象的每一个离开点. 例如: 1: void Function(int a) { 2 ...
- 报错 DOMDocument not found
php -m 查看有没有dom扩展 没有安装扩展 yum install php-dom php 常用扩展有 yum install php-solr php-opcache php-seasLog ...