1、环境

     在机器192.168.0.4机器上安装了zookeeper,用于dubbo的服务注册,安装教程在另外一篇博客

http://www.cnblogs.com/520playboy/p/6235415.html

2、生产端

2.1、项目结构

2.2、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mujourney</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version> <name>dubbo-provider</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>com.mujourney</groupId>
<artifactId>dubbo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build> </project>

2.3、UserServiceImpl.java

package com.mujourney.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.alibaba.dubbo.config.annotation.Service; @Service(interfaceName="com.mujourney.service.UserService",timeout=10000)
public class UserServiceImpl implements UserService { public Map<String, Object> findById(int id) {
return get(id);
} public List<Map<String, Object>> queryList() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 1; i <= 10; i++) {
list.add(get(i));
}
return list;
} private Map<String, Object> get(int id) {
Map<String, Object> res = new HashMap<String, Object>();
res.put("id", id);
res.put("name", "1");
res.put("type", "1");
res.put("date", "1");
return res;
} }

2.4、StartServer.java

package com.mujourney.service;

import java.util.concurrent.TimeUnit;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StartServer {

    public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "application-provider.xml" });
context.start();
System.out.println("=========dubbo生产者启动完毕========");
try {
TimeUnit.HOURS.sleep(1l);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

2.5、application-provider.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-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 指定dubbo的服务名称 -->
<dubbo:application name="my_provider" /> <!-- 使用multicast广播注册中心暴露服务地址 -->
<!-- <dubbo:registry protocol="zookeeper" address="zookeeper://192.168.18.25:2181" /> -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.0.4:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" /> <!-- 具体的实现bean -->
<!-- <bean id="userService" class="com.mujourney.service.UserServiceImpl" /> --> <!-- 声明需要暴露的服务接口 -->
<!-- <dubbo:service interface="com.mujourney.service.UserService" ref="userService" /> --> <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示当前ApplicationContext中所有的类 -->
<dubbo:annotation package="com.mujourney.service"></dubbo:annotation>
</beans>

因为使用注解来创建服务,所以就把生成者的配置文件创建UserServiceImpl类和指定接口给注释了

3、消费端 

3.1 、项目结构

3.2、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mujourney</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version> <name>dubbo-consumer</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.4.RELEASE</spring.version>
<junit.version>4.8.2</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 关联dubbo-service工程源码 -->
<dependency>
<groupId>com.mujourney</groupId>
<artifactId>dubbo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
<type>jar</type>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.0.5.RELEASE</version>
<optional>false</optional>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
</project>

3.3、application-consumer.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-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 指定生成者 -->
<dubbo:application name="my_provider" /> <!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.0.4:2181" /> <!-- 扫描注解包路径,多个包用逗号分隔,不填package表示扫描当前ApplicationContext中的所有类 -->
<dubbo:annotation package="com.mujourney.service"></dubbo:annotation>
</beans>

3.4、ClientTest.java

package com.mujourney.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.alibaba.dubbo.config.annotation.Reference; @Component
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/application-consumer.xml"})
public class ClientTest { @Reference(interfaceName="com.mujourney.service.UserService")
private UserService uservice; @Test
public void pors() {
System.out.println(uservice.findById(1));
}
}

4、服务的接口端

4.1、项目结构

4.2 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mujourney</groupId>
<artifactId>dubbo-service</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<verbose>true</verbose>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin> </plugins>
</build>
</project>

4.3、UserService.java

package com.mujourney.service;

import java.util.List;
import java.util.Map; public interface UserService { public Map<String, Object> findById(int id); public List<Map<String, Object>> queryList();
}

之所以有一个服务的接口端,因为生产者和消费者都要用到接口,把接口单独作为maven的java项目可以打jar包放到私库给生产端和消费端同时使用。

到这里注解版教程就介绍了

dubbo2.5.3注解版的更多相关文章

  1. springMVC(注解版笔记)

    springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...

  2. 整合初步--------->SSH(注解版)

    上面的一篇博客已经介绍了 Spring和Hibernate之间的整合,没看过的童鞋可以去看看,这篇博客讲解Spring+Hibernate+Struts2注解版......... 个人觉得使用注解可能 ...

  3. struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)

    为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补: 1:File--->New--->Other--->Maven--->M ...

  4. SpringMVC环境搭建---xml版及注解版

    一.建立 JavaWeb 项目(基于Intellij 14.0.3搭建) 1.建立一个 Java 项目,在项目下新建一个文件夹 webapp ,然后在该文件夹下新建一个 WEB-INF 文件夹: 2. ...

  5. SSM-Spring-17:Spring中aspectJ注解版

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP 语法,能够在编译期提供 ...

  6. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  7. SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]

    SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...

  8. spring mvc注解版01

    spring mvc是基于servlet实现的在spring mvc xml版中已经说过了,注解版相较于xml版更加简洁灵活. web项目的jar包: commons-logging-1.1.3.ja ...

  9. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

随机推荐

  1. iOS-仿京东6位密码支付输入框

    概述 用于安全支付的密码支付输入框. 详细 代码下载:http://www.demodashi.com/demo/10709.html 开发需求中有时候我们需要用于安全支付的功能, 需要设置APP钱包 ...

  2. StringBuilder.AppendFormat(String, Object, Object) 方法

    将通过处理复合格式字符串(包含零个或零个以上格式项)返回的字符串追加到此实例. 每个格式项都替换为这两个参数中任意一个参数的字符串表示形式. 说明: public StringBuilder Appe ...

  3. Android之SurfaceView使用总结

    1.概念SurfaceView是View类的子类,可以直接从内存或者DMA等硬件接口取得图像数据,是个非常重要的绘图视图.它的特性是:可以在主线程之外的线程中向屏幕绘图上.这样可以避免画图任务繁重的时 ...

  4. linux服务器的Gzip文件压缩方法[转]

    一.gzip介绍 gzip是GNU zip的缩写,它是一个GNU自由软件的文件压缩程序,也经常用来表示gzip这种文件格式.软件的作者是Jean-loup Gailly和Mark Adler.1992 ...

  5. JAVA RMI远程方法调用简单实例[转]

    RMI的概念 RMI(Remote Method Invocation)远程方法调用是一种计算机之间利用远程对象互相调用实现双方通讯的一种通讯机制.使用这种机制,某一台计算机上的对象可以调用另外 一台 ...

  6. HDFS配额管理指南

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_quota_admin_guide.html Hadoop分布式文件系统(HDFS)允许管理员为每个 ...

  7. WebDav的java客户端开发包:sardine

    最近需要对WebDav服务器进行操作,查找了一下,基于java的开发包主要有这几个: slide Jackrabbit sardine webdavclient4j 其中slide是apache的一个 ...

  8. [android] 网络断开的监听

    标签: android 网络监听 杂谈 分类: Android [转自]  http://blog.sina.com.cn/s/blog_5d5996d001012o07.html 其实手机在网络方面 ...

  9. SIPp常用脚本之三:UAC

    UAC是作为SIP消息的发起端,可以控制消息速率什么的,方便极了. 一.uac.xml <?xml version="1.0" encoding="ISO-8859 ...

  10. js 正则表达式 exec 和 match的使用

    match body.match(/\d\.\d\.\d\.\d:\d/g); // 推荐使用exec可以拿到多个数组 exec var a = [ 'PHPSESSID=sglvjui97o18bg ...