配置 dubbo 的Hello World

按以下步骤进行:

1.新建一个maven工程du(com.lzm du)。

2.编辑dom.xml加入依赖

项目依赖的三方库与Dubbo所依赖的版本冲突怎么办

比如,项目使用的spring与dubbo冲突,
dubbo使用的是spring2.5,
而项目中其它模块依赖的是spring3.2.10.RELEASE。

在Maven中,使用项目根pom.xml中的dependencyManagement版本仲裁解决:

<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.lzm</groupId>
	<artifactId>du</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<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.5.0-alpha</version>
			<type>pom</type>
		</dependency>

		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.2.10.RELEASE</version>
		</dependency>

	</dependencies>


	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>com.alibaba.external</groupId>
				<artifactId>sourceforge.spring</artifactId>
				<version>3.2.10.RELEASE</version>
			</dependency>
		</dependencies>

	</dependencyManagement>
</project>

其中zkclient是zookeeper的客户端。

3.新建IHello接口

package com.lzm.du;

public interface IHello {

    
    public String sayHello(String name);
    
}

工程结构图:

 
配置 dubbo 的Hello World
 

4.实现IHello接口

package com.lzm.du;

public class Hello implements IHello {

    public String sayHello(String name) {
        // TODO Auto-generated method stub
        return "Hello " + name;
    }

}

5.在src/main/resources/里新增provider.xml文件,配置provider。

<?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">
 
    <!-- Application name -->
    <dubbo:application name="hello-world-app"  />
 
    <!-- registry address, used for service to register itself -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- expose this service through dubbo protocol, through port 20880 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- which service interface do we expose? -->
    <dubbo:service interface="com.lzm.du.IHello" ref="helloService" />
 
    <!-- designate implementation -->
    <bean id="helloService" class="com.lzm.du.Hello" />
 
</beans>

6.新建Provider

package com.lzm.du;


import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Provider {
 
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        context.start();
 
        System.out.println("Press any key to exit.");
        System.in.read();
    }
 
}

7.在src/main/resources/里新增consumer.xml文件,配置consumer。

<?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="zookeeper://127.0.0.1:2181" />
 
    <!-- which service to consume? -->
    <dubbo:reference id="helloService" interface="com.lzm.du.IHello" />
 
</beans>

8.新建Consumer

package com.lzm.du;


import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Consumer {
 
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
        context.start();
 
        IHello helloService = (IHello)context.getBean("helloService"); // get service invocation proxy
        String result = helloService.sayHello("world"); // do invoke!
 
        System.out.println(result); // cool, how are you~
    }
 
}

9.在src/main/resources/里新增dubbo.properties

dubbo.application.name=foo
dubbo.application.owner=bar
dubbo.registry.address=zookeeper://127.0.0.1:2181

该文件是全局配置文件,因为现在provider.xml, consumer.xml没引用它定义的属性,所以暂时没用上。

10.下载zookeeper

http://apache.fayea.com/apache-mirror/zookeeper/zookeeper-3.5.0-alpha/zookeeper-3.5.0-alpha.tar.gz

11.启动zookeeper服务器。

解压zookeeper-3.5.0-alpha.tar.gz,将zookeeper-3.5.0-alpha\conf\zoo_sample.cfg改名为zoo.cfg。双击

zookeeper-3.5.0-alpha\bin\zkServer.cmd。

12.运行Provider

13.运行Consumer

以上就可以跑了。

 =========================================================

接下来配置monitor

14.下载monitor

http://download.csdn.net/download/nrs12345/7412165

解压后,将dubbo文件夹复制到tomcat的webapps下面,修改sever.xml,在<Host 节点增加:

<Context path="" debug="0" docBase="dubbo" reloadable="true"/> 

这样就可以通过 http://localhost:8180/  访问,而不是http://locahost:8180/dubbo。

使用前面的url访问正常,使用后面的url访问时,会出现很多找不到页面的错误。

当然,把dubbo文件夹里面的文件复制到ROOT下面也可以。

15.修改WEB-INF/dubbo.properties,

dubbo.registry.address=zookeeper://127.0.0.1:2181

dubbo.admin.root.password=root

dubbo.admin.guest.password=guest

第1个是zookeeper的地址,后面两个是访问monitor时要求登录的账号,root/root, guest/guest。

16.启动tomat,访问http://localhost:8180/ 。

在https://github.com/alibaba/dubbo,点击右下角的‘Download ZIP',下载后解压,然后进入dubbo-master,执行 mvn clean install -Dmaven.test.skip进行install,然后进入dubbo-simple\dubbo-monitor-simple\target\,解压dubbo-monitor-simple-2.5.4-SNAPSHOT-assembly.tar.gz得到dubbo-monitor-simple-2.5.4-SNAPSHOT-assembly。进入dubbo-simple\dubbo-monitor-simple\target\dubbo-monitor-simple-2.5.4-SNAPSHOT-assembly\dubbo-monitor-simple-2.5.4-SNAPSHOT\conf,修改dubbo.properties,将dubbo.registry.address=multicast://224.5.6.7:1234 改成dubbo.registry.address=zookeeper://127.0.0.1:2181。

进入上级的bin,执行start.bat,打开http://127.0.0.1:8080,就能看到monitor的页面了。

长恨东风2014-09-04 13:01:58我试了 。要加配置。provider加一句
<dubbo:monitor protocol="registry" />

相关推荐