环境准备:
1.zookeeper:zookeeper-3.4.6版本
2.maven:apache-maven-3.3.9版本
3.dubbo监控工具:dubbo-admin-2.5.4-SNAPSHOT 一个war包,用户查看注册中心的提供者和消费者
项目结构:
dubbo-provider-test
|--src/main/java 程序目录
|--com.wengen.dubbo_provider_test.service 提供者接口目录
|--DomeService.java
|--com.wengen.dubbo_provider_test.service.impl 提供者接口实现目录
|--DomeServiceImpl.java
|--src/test/java 测试目录
|--com.wengen.dubbo_provider_test
|--DomeServiceTest.java 提供者启动类
|--DomeServiceConsumerTest.java 消费者启动类
|--src/main/resources
|--spring.xml spring 配置文件
|--config
|--provider.xml 提供者配置文件
|--consumer.xml 消费者配置文件
|--pom.xml maven的pom文件
源码:
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.wengen</groupId>
<artifactId>dubbo-provider-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>dubbo-provider-test</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<druid.version>1.0.18</druid.version>
<jaxen.version>1.1.1</jaxen.version>
<lombok.version>1.14.4</lombok.version>
<slf4j.version>1.7.5</slf4j.version>
<dom4j.version>1.6.1</dom4j.version>
<spring.version>4.2.5.RELEASE</spring.version>
<commons-httpclient.version>3.0.1</commons-httpclient.version>
<dubbo.version>2.5.2</dubbo.version>
<zkclient.version>0.1</zkclient.version>
<project.junit.version>4.8.2</project.junit.version>
<spring.amqp.version>1.2.0.RELEASE</spring.amqp.version>
<spring.version>4.2.5.RELEASE</spring.version>
<spring-core.version>4.2.5.RELEASE</spring-core.version>
<aspectj.version>1.7.0</aspectj.version>
<aopalliance.version>1.0</aopalliance.version>
<asm.version>3.1</asm.version>
<mybatis.version>3.2.8</mybatis.version>
</properties> <dependencies> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>${spring.amqp.version}</version>
</dependency> </dependencies>
</project>
spring.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <!-- 自动扫描bean,把作了注解的类转换为bean -->
<context:component-scan base-package="com.wengen.dubbo_provider_test.service">
<context:include-filter type="annotation"
expression="com.alibaba.dubbo.config.annotation.Service" />
</context:component-scan> <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<!-- 强制使用CGLIB代理 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<task:annotation-driven />
<context:annotation-config />
<import resource="classpath:/config/provider.xml" />
</beans>
提供者配置文件: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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-provider-test" /> <!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20881" /> <!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.wengen.dubbo_provider_test.service.DomeService" ref="domeservice"/> <!-- 和本地bean一样实现服务 -->
<bean id="domeservice" class="com.wengen.dubbo_provider_test.service.impl.DomeServiceImpl"/>
</beans>
消费者配置文件: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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo-consumer-test" /> <!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.wengen.dubbo_provider_test.service.DomeService" /> </beans>
提供者接口:DomeService.java
package com.wengen.dubbo_provider_test.service; public interface DomeService {
String sayHello(String name);
}
提供者接口实现:DomeServiceImpl.java
package com.wengen.dubbo_provider_test.service.impl; import com.wengen.dubbo_provider_test.service.DomeService; public class DomeServiceImpl implements DomeService { @Override
public String sayHello(String name) { return "Helle" + name;
} }
提供者启动类:DomeServiceTest.java
package com.wengen.dubbo_provider_test; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DomeServiceTest { public static void main(String[] args)throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});
context.start();
//任意键退出
System.in.read();
} }
消费者启动类:DomeServiceConsumerTest.java
package com.wengen.dubbo_provider_test; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wengen.dubbo_provider_test.service.DomeService; public class DomeServiceConsumerTest {
public static void main(String []args) throws IOException{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String []{"classpath:/config/consumer.xml"});
context.start();
DomeService dome = (DomeService) context.getBean("demoService");
String hello = dome.sayHello(" ADSADS");
System.out.println(hello);
}
}
dubbo监控工具安装请参考:http://www.cnblogs.com/coreyjk/p/6560085.html