1. 程式人生 > >Dubbo新手入門例項HelloWorld(zookeeper)

Dubbo新手入門例項HelloWorld(zookeeper)

最近剛接觸dubbo,新手入門遇到好多麻煩,網上搜來的入門demo也是各種問題,百般周折自己終於倒騰出來了,與大家共享~

1.建立服務方專案dubbo-server,在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.xbz</groupId>
	<artifactId>dubbo-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>server</name>
	<url>http://maven.apache.org</url>

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

	<dependencies>
		<!-- spring begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<!-- spring end -->

		<!-- dubbo begin -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<!-- dubbo end -->

		<!-- 註冊中心zookeeper begin -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.6</version>
		</dependency>
		<!-- 註冊中心zookeeper end -->

		<!-- log begin -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</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>
		<!-- log end -->
		
		<!-- other begin -->
		<dependency>
			<groupId>org.jboss.netty</groupId>
			<artifactId>netty</artifactId>
			<version>3.2.0.Final</version>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.8</version>
		</dependency>
		<!-- other end -->
	</dependencies>
</project>
2.測試介面DemoService和實現類DemoServiceImpl
package com.xbz.service;

public interface DemoService {

	String sayHello(String name);

}
package com.xbz.service.impl;

import com.xbz.service.DemoService;

public class DemoServiceImpl implements DemoService {

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

}

3.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="dubbo-demo" />
	<!-- zookeeper註冊中心 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<dubbo:protocol name="dubbo" port="20880" />
	
	<!-- 和本地bean一樣實現服務 --> 
	<bean id="demoService" class="com.xbz.service.impl.DemoServiceImpl" />

	<!-- 向註冊中心註冊暴漏服務地址,註冊服務 -->
	<dubbo:service interface="com.xbz.service.DemoService"
		ref="demoService" executes="10" />

</beans>

4.服務方主方法ServerMain
package main;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ServerMain {

	public static void main(String[] args) throws IOException {

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" });
		context.start();

		System.out.println("輸入任意按鍵退出 ~ ");
		System.in.read();
		context.close();
	}
}
至此,服務方的配置就已經完成了~下面是服務方專案結構:


接下來我們建立客戶端消費方dubbo-client專案
1.建立dubbo-client,在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.xbz</groupId>
	<artifactId>dubbo-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>client</name>
	<url>http://maven.apache.org</url>

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

	<dependencies>
		<!-- spring begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<!-- spring end -->

		<!-- dubbo begin -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<!-- dubbo end -->

		<!-- 註冊中心zookeeper begin -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.6</version>
		</dependency>
		<!-- 註冊中心zookeeper end -->

		<!-- log begin -->
		<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>
		<!-- log end -->

		<!-- other begin -->
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.8</version>
		</dependency>
		<dependency>
			<groupId>com.xbz</groupId>
			<artifactId>dubbo-server</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<!-- other end -->
	</dependencies>
</project>

2.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">
	<dubbo:application name="consumer-of-dubbo-demo" />

	<dubbo:registry address="zookeeper://127.0.0.1:2181" />

	<!-- 向註冊中心訂閱服務 -->
	<dubbo:reference id="demoService" interface="com.xbz.service.DemoService" />
</beans> 

3.客戶端消費方主方法ClientMain
package main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xbz.service.DemoService;

public class ClientMain {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "applicationConsumer.xml" });
		context.start();
		DemoService service = (DemoService) context.getBean("demoService");
		System.out.println(service.sayHello("world"));
		context.close();
	}
}

客戶端消費方專案結構如下


至此,一個基本的dubbo專案就已經構建完成了.在本機上啟動zookeeper註冊中心(直接解壓到英文路徑下直接執行根路徑下bin/zkServer.cmd啟動)


啟動服務方


啟動成功,執行緒阻塞~

執行客戶端消費方


輸出hello world,呼叫成功 !

此時服務端控制檯輸出如下