使用dubbo+zookeeper创建一个小应用

    dubbo和zookeeper都是分布式服务框架,其中dubbo是阿里巴巴研发的开源框架,dubbo主要分为服务提供方,服务消费方,注册中心,监控中心,服务运行容器。下图是dubbo官网的一个结构图:


这里zookeeper在这里主要是用来作为dubbo的注册中心,dubbo还可以使用Multicast注册中心Zookeeper注册中心Redis注册中心Simple注册中心。下面来看实例:

创建一个Java Project作为dubbo的 provider。命名为DubboProvider

1、引入一下jar包:


2、新建一个IhelloService接口

package com.service;
/**
 * @description
 * @date:(2016-3-29 下午10:21:03)
 * @author Administrator
 * @version v1.0
 * @since v1.0
 *
 * Modified history
 *
 *    Modified date:  
 *    Modifier user:     
 *    description: 
 *
 * */
public interface IHelloService {
	
	String sayHello(String name);

}

3、新建一个IhelloService的实现类HelloServiceImpl.java

package com.service.impl;

import com.service.IHelloService;

/**
 * @description
 * @date:(2016-3-29 下午10:21:26)
 * @author Administrator
 * @version v1.0
 * @since v1.0
 *
 * Modified history
 *
 *    Modified date:  
 *    Modifier user:     
 *    description: 
 *
 * */
public class HelloServiceImpl implements IHelloService{

	@Override
	public String sayHello(String name) {
		System.out.println("生产者中的方法执行...");
		return "hello "+name;
	}

}
3、新建一个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: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="hello-world-app"  />
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.service.IHelloService" ref="helloService" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="helloService" class="com.service.impl.HelloServiceImpl" />


</beans>
4、新建一个服务发布者启动类

package com.test;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @description
 * @date:(2016-3-29 下午10:41:24)
 * @author Administrator
 * @version v1.0
 * @since v1.0
 *
 * Modified history
 *
 *    Modified date:  
 *    Modifier user:     
 *    description: 
 *
 * */
public class TestProvider {
	
	public static void main(String[] args) throws IOException {
		
		ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("provider.xml");
		applicationContext.start();
		 System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟 

	}

}

然后在新建一个Java Project项目DubboConsumer,并引入一下jar包:


5、在DubboConsumer中新建一个接口IhelloService.java

package com.service;
/**
 * @description
 * @date:(2016-3-29 下午10:21:03)
 * @author Administrator
 * @version v1.0
 * @since v1.0
 *
 * Modified history
 *
 *    Modified date:  
 *    Modifier user:     
 *    description: 
 *
 * */
public interface IHelloService {
	
	String sayHello(String name);

}

6、新建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: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="consumer-of-helloworld-app"  />
 
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <!-- <dubbo:protocol name="dubbo" port="20880" /> -->
    
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="helloService" interface="com.service.IHelloService" />


</beans>

7、新建一个测试类TestConsumer

package com.test;

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

import com.service.IHelloService;

/**
 * @description
 * @date:(2016-3-29 下午10:41:03)
 * @author Administrator
 * @version v1.0
 * @since v1.0
 *
 * Modified history
 *
 *    Modified date:  
 *    Modifier user:     
 *    description: 
 *
 * */
public class TestConsumer {
	
	public static void main(String[] args) {
		
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("consumer.xml");
		IHelloService helloService=(IHelloService)applicationContext.getBean("helloService");
		String result=helloService.sayHello("Tom"); 
		System.out.println(result);

	}

}
8、下载zookeeper

http://www.apache.org/dist/zookeeper/

9、先启动zookeeper注册中心

/zookeeper-3.4.8\bin目录下的zkServer.cmd(window环境)或者zkServer.sh(Linux环境)。然后在运行TestProvider.java在运行TestConsumer就能看到输出结果。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章