dubbo学习1:Dubbo的入门例子HelloWorld



 

 

 

  
 

今天看了位大神的Dubbo学习笔记终于能把HelloWorld写出来了

该例子用maven来构建,客户端和服务端之间采用组播技术进行通信,并未使用zookeeper。

服务端:

服务端代码结构:dubbo学习1:Dubbo的入门例子HelloWorld
dubbo学习1:Dubbo的入门例子HelloWorld
 

 

 

dubbo学习1:Dubbo的入门例子HelloWorld

POM.XML

<?xml version="1.0" ?>
<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>cn.zto.dubbo-Service</groupId>
	<artifactId>dubbo-Service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>dubbo-Service</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.18.1-GA</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
			<exclusions>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
				<exclusion>
					<artifactId>jms</artifactId>
					<groupId>javax.jms</groupId>
				</exclusion>
				<exclusion>
					<artifactId>mail</artifactId>
					<groupId>javax.mail</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.6.SEC03</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.6</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.5</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.4</version>
		</dependency>
	</dependencies>
</project>

applicationProvider.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="hello-world" />

	<!-- 注册地址 -->
	<dubbo:registry address="multicast://224.5.6.7:1234" />

	<!-- Service interface Concurrent Control -->
	<dubbo:service interface="cn.zto.service.IProcessData"
		ref="demoService" executes="10" />


	<!-- designate implementation -->
	<bean id="demoService" class="cn.zto.service.impl.ProcessDataImpl" />

</beans>

代码:

package cn.zto.service;

public interface IProcessData {
	public String hello(String name); 
}
package cn.zto.service.impl;

import cn.zto.service.IProcessData;

public class ProcessDataImpl implements IProcessData {

	public String hello(String name) {
		System.out.println(name);
		return "hello : " + name;
	}

}
package cn.zto.app;


import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "applicationProvider.xml" });
		
		context.start();
		System.out.println("按任意键退出");
		System.in.read();

	}

}

客户端:

客户端结构:dubbo学习1:Dubbo的入门例子HelloWorld
dubbo学习1:Dubbo的入门例子HelloWorld
 

dubbo学习1:Dubbo的入门例子HelloWorld

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>cn.zto.consumer</groupId>
	<artifactId>dubbo-Consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<name>dubbo-Consumer</name>
	<url>http://maven.apache.org</url>


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.18.1-GA</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
			<exclusions>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
				<exclusion>
					<artifactId>jms</artifactId>
					<groupId>javax.jms</groupId>
				</exclusion>
				<exclusion>
					<artifactId>mail</artifactId>
					<groupId>javax.mail</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.6.SEC03</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.6</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>com.github.adyliu</groupId>
			<artifactId>zkclient</artifactId>
			<version>2.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.5</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>cn.zto.dubbo-Service</groupId>
			<artifactId>dubbo-Service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>

</project>

applicationConsumer.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
        ">

	<!-- consumer application name -->
	<dubbo:application name="consumer-of-helloworld-app" />

	<!-- registry address, used for consumer to discover services -->
	<dubbo:registry address="multicast://224.5.6.7:1234" />
	<dubbo:consumer timeout="5000" />
	<!-- which service to consume? -->
	<dubbo:reference id="demoService" interface="cn.zto.service.IProcessData"/>
</beans>

代码:

package cn.zto.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.zto.service.IProcessData;



public class ConsumerThd{

	public void sayHello(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "applicationConsumer.xml" });
		
		context.start();

		IProcessData demoService = (IProcessData) context.getBean("demoService");

		System.out.println(demoService.hello("world"));
	}
}
package cn.zto.test;

import cn.zto.consumer.ConsumerThd;

public class AppTest {
	public static void main(String[] args){
		ConsumerThd thd = new ConsumerThd();
		thd.sayHello();
	}
}

运行的时候可能会遇到解析不了XML文件的错误SAXException:

在maven的本地仓库下找到maven->com->alibaba->dubbo->2.5.3->dubbo-2.5.3.jar (具体的看自己的本地仓库) 解压它获得dubbo.xsd文件

然后打开Eclipse:1. Window->Preferences->XML->XML Catalog->User Specified Entries窗口中,选择Add 按纽

                              2.dubbo学习1:Dubbo的入门例子HelloWorlddubbo学习1:Dubbo的入门例子HelloWorld
dubbo学习1:Dubbo的入门例子HelloWorld
 

 

先运行服务端的Main方法  然后在启动 客户端的 AppTest 方法  OK 

相关推荐