准备工作
1.这个实例需要maven的项目依赖,如果有需要的博友,请查看我前面的文章:maven项目依赖详细步骤。
2.dubbo下载
项目代码
三个项目的目录结构,生产者,接口,消费者
其中接口只是提供了接口的方法,无需其他配置。而生产者和消费者需要dubbo.xml配置文件来配置。
各个项目的目录结构
接口inter
SayHelloWorldInter.java
package com.dao.chu.inter;
/**
*
* 第一个项目的接口
*
* @version [版本号, 2017年1月24日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public interface SayHelloWorldInter
{
/**
*
* sayHelloWorld方法
* @see [类、类#方法、类#成员]
*/
public void sayHelloWorld();
}
提供者product
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.dubbo</groupId>
<artifactId>product</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>product</name>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>my.dubbo</groupId>
<artifactId>inter</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
dubbo.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
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/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false">
<bean id="springservice" class="com.dao.chu.product.SayHelloWorldImpl"></bean>
<!-- 提供方应用名称-->
<dubbo:application name="dubbo_product"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"
check="false" subscribe="false" register=""></dubbo:registry>
<!-- 要暴露的服务接口 -->
<dubbo:service interface="com.dao.chu.inter.SayHelloWorldInter" ref="springservice" />
<dubbo:protocol accesslog="true" name="dubbo" port="20880" />
</beans>
SayHelloWorldImpl.java
package com.dao.chu.product;
import org.springframework.stereotype.Service;
import com.dao.chu.inter.SayHelloWorldInter;
/**
*
* 接口实现类
*
* @version [版本号, 2017年1月25日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@Service("springservice")
public class SayHelloWorldImpl implements SayHelloWorldInter
{
public void sayHelloWorld()
{
System.out.println("say hello world");
}
}
BeginProduct.java
package com.dao.chu.product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* 提供者测试类
*
* @version [版本号, 2017年1月25日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class BeginProduct
{
@SuppressWarnings({"unused", "resource"})
public static void main(String[] args)
{
//加载dubbo.xml
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath*:dubbo.xml" });
//执行不退出程序的操作
while(true);
}
}
消费者customer
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.dubbo</groupId>
<artifactId>customer</artifactId>
<packaging>war</packaging>
<version>2.0</version>
<name>customer Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>my.dubbo</groupId>
<artifactId>inter</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<finalName>customer</finalName>
</build>
</project>
dubbo.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
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/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false">
<dubbo:application name="dubbo_customer"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"
check="false"></dubbo:registry>
<!-- 要引用的服务 -->
<dubbo:reference interface="com.dao.chu.inter.SayHelloWorldInter"
id="springservice" ></dubbo:reference>
</beans>
BeginCustomer.java
package com.dao.chu.customer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dao.chu.inter.SayHelloWorldInter;
/**
*
* 消费者测试类
*
* @version [版本号, 2017年1月25日]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class BeginCustomer
{
@SuppressWarnings("resource")
public static void main(String[] args)
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "classpath*:dubbo.xml" });
SayHelloWorldInter sayHelloWorldInter = (SayHelloWorldInter) applicationContext.getBean("springservice");
//使用接口中的方法
sayHelloWorldInter.sayHelloWorld();
while(true);
}
}
三个项目的jar包
这里只需要spring,dubbo,zookeeper的jar包。值得注意的是在自己倒入jar包的时候,不要忘记zkclient.jar。
提供者product
接口inter
消费者customer
运行步骤
运行zookeeper
zookeeper-3.4.6\bin\zkServer.cmd
运行dubbo
tomcat\bin\startup.bat
打开dubbo注册中心
其实和运行其他javaweb项目一样 :locallhost:8080/ 加项目名称即可访问。
locallhost:8080/dubbo-admin/或127.0.0.1:8080/dubbo-admin
运行生产者product测试类
右键BeginProduct.java->run as ->java application
运行消费者customer测试类
右键BeginCustomer.java->run as ->java application
查看注册中心的应用共有一个提供者,一个消费者,名称和dubbo.xml的配置一样
各自查看提供者和消费者
提供者
消费者
本次实例源码下载