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. write()和prinln()的区别?

    输出数字不同: write()输出数字转换为字符,println原样输出. 输出null不同: write()输出引用类型的时候调用的toString转换为String数据,因此如果对象为null那么 ...

  2. Swift3 获取版本号,比较版本大小

    Swift获取应用版本号:version 1.获取本地版本号 /// 获取本地版本号 func getLocalVersion() -> String { var localVersion:St ...

  3. 禁用root登录以及使用sudo分配权限

    前言 最近一段时间服务器有受到一些挖比特币的人的攻击,这里就简要说明一下提升服务器安全度的做法 禁用root登录 root权限可以说是linux服务器的最高权限,如果我们平常都是直接使用root用户来 ...

  4. Foundations of Machine Learning: Boosting

    Foundations of Machine Learning: Boosting Boosting是属于自适应基函数(Adaptive basis-function Model(ABM))中的一种模 ...

  5. 关于Xcode上的Other linker flags

    Targets选项下有Other linker flags的设置,用来填写XCode的链接器参数,如:-ObjC -all_load -force_load等.还记得我们在学习C程序的时候,从C代码到 ...

  6. 自动化测试尝试 动态Linq表达式生成 ftp上传

    自动化测试尝试   1. Selenium IDE Selenium IDE is a Chrome and Firefox plugin which records and plays back u ...

  7. Python学习笔记011——内置函数eval()

    1 描述 eval()  函数用来执行一个字符串表达式,并返回表达式的值 2 语法 原文 eval(expression[, globals=None[, locals=None]]) express ...

  8. selenium + python 怎样才能滚到页面的底部?

    可以用 execute_script方法来处理这个. 调用原生javascript的API,这样你想滚到哪里就能滚到哪里. 下面的代码演示了如何滚到页面的最下面:   driver.execute_s ...

  9. 转 web前端性能分析--原理篇

    转自http://blog.csdn.net/five3/article/details/7686715 web前端性能: 即是web用户在访问一个页面时所要花费的时间总和.即一个完全意义上的用户响应 ...

  10. ssh/scp 远程连接ssh非默认端口方法

    ssh -p port <username>@<IP>