`
somefuture
  • 浏览: 1079847 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

windows下本地运行和消费dubbo服务

阅读更多

最简单的dubbo应用包括三部分:服务提供者provider、服务消费者consumer、注册中心。

 

前两个都是我们自己编写的,第三个注册中心推荐使用稳定性更好的zookeeper。

 

所以我们需要先下载zookeeper: http://apache.fayea.com/zookeeper/current/

下载后解压到某个目录,进入里面的conf目录。将zoo_sample.cfg复制一份,改名为zoo.cfg。

修改其内容为

tickTime=2000

initLimit=10

syncLimit=5

dataDir=D:\\data\\zookeeper

clientPort=2181

 其中的dataDir可以自由修改。

进入zookeeper的bin目录,运行zkServer.cmd

 { 要测试是否运行良好的话可以运行zkCli.cmd }

 

 

接下来打开eclipse,新建maven项目,arctype使用默认的quickstart就可以。

修改pom.xml文件:

<dependencies>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.0.13</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.6</version>
			<exclusions>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
	</dependencies>

 

在Java源文件夹下新建接口,这个接口要给provider和consumer都使用

public interface DemoService {
	public void sayHello();
	
	public String returnHello();
	
	public MsgInfo returnMsgInfo(MsgInfo info);
}

 新建其实现类

public class DemoServiceImpl implements DemoService{

	public void sayHello() {
		System.err.println("Hello world.");
	}

	public String returnHello() {
		return "hello world";
	}

	public MsgInfo returnMsgInfo(MsgInfo info) {
		info.getMsgs().add("done!");
		return info;
	}

}

 新建一个测试实体

public class MsgInfo implements Serializable {

	int id;
	String name;
	List<String> msgs;

//getters setters

}

 

在资源文件夹下新建目录spring(虽然不是必要的,不过我们是和spring集成的)。

在里面新建xml文件dubbo-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="hello-world-app" />

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

	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 声明需要暴露的服务接口 -->
	<dubbo:service interface="com.abc.qkdb.DemoService" ref="demoService" />

	<!-- 和本地bean一样实现服务 -->
	<bean id="demoService" class="com.abc.qkdb.DemoServiceImpl" />

</beans>

 其中的<dubbo:registry>就是注册中心这里使用的是zookeeper,其他选项可以参考http://dubbo.io/Administrator+Guide-zh.htm#AdministratorGuide-zh-Redis%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83%E5%AE%89%E8%A3%85

相同目录下新建dubbo-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="consumer-of-helloworld-app" />
         <!--zookeeper注册中心 -->
         <dubbo:registry  protocol="zookeeper" address="127.0.0.1:2181" /> 

          <!-- 生成远程服务代理,可以和本地bean一样使用demoService-->
         <dubbo:reference id="demoService" interface="com.abc.qkdb.DemoService" />
</beans>

 注意里面也包含<dubbo:registry>结点。

 

 

回到Java文件夹,新建服务类

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LuncherProvider {
	public static void main(String[] args) throws InterruptedException, IOException {
		LuncherProvider provider = new LuncherProvider();
		provider.start();
		System.in.read();
	}

	void start() {
		String configLocation = "spring/dubbo-provider.xml";
		ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
		String[] names = context.getBeanDefinitionNames();
		for (String name : names) {
			System.err.println(name);
		}
	}
}

 新建消费者类

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LuncherConsumer {
	public static void main(String[] args) {
		LuncherConsumer provider = new LuncherConsumer();
		provider.start();
	}

	void start() {
		String configLocation = "spring/dubbo-consumer.xml";
		ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
		DemoService ds = (DemoService) context.getBean("demoService");
		String[] names = context.getBeanDefinitionNames();
		System.out.print("Beans:");
		for (String string : names) {
			System.out.println(string);
		}
		
		MsgInfo info = new MsgInfo();
		info.setId(1);
		info.setName("ruisheh");
		List<String> msgs = new ArrayList<String>();
		msgs.add("I");
		msgs.add("am");
		msgs.add("test");
		info.setMsgs(msgs);

		System.out.println(ds.returnMsgInfo(info).getMsgs());
		System.err.println(ds.returnHello());
	}
}

 

编码完毕。

先运行LuncherProvider类,没有错误输出后(会输出context中的bean)运行LuncherConsumer,输出

[I, am, test, done!]

hello world

即为正常。

0
5
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics