使用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就能看到輸出結果。




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