Dubbo Zookeeper与Spring或者struts2整合和使用

现在网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的。现在核心业务抽取出来,作为独立的服务,使前端应用能更快速和稳定的响应。Zookeeper作为Dubbo服务的注册中心,Dubbo原先基于数据库的注册中心,没采用Zookeeper,Zookeeper一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心,Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求。

1.下载dubbo-admin-2.5.4.war部署在tomcat中启动

2.下载zookeeper-3.4.8.tar.gz(我的是window7),执行zkServer.cmd

3.下载相关jar包

<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.4.6</version>
</dependency>
<dependency>
        <groupId>com.github.sgroschupf</groupId>
        <artifactId>zkclient</artifactId>
	<version>0.1</version>
</dependency>

4.编写接口,还有实现类

TestRegistryService:

package com.yingze;

/**  
 * @ClassName: TestRegistryService
 * @Description: TODO()
 * @author liang
 * @date 2016年6月23日 上午9:10:35
 *
 */
public interface TestRegistryService {

	public String sayHello(String name);
	
}
TestRegistryServiceImpl:

package com.yingze;

import org.springframework.stereotype.Service;

import com.yingze.TestRegistryService;

/**  
 * @ClassName: TestRegistryServiceImpl
 * @Description: TODO()
 * @author liang
 * @date 2016年6月23日 上午9:20:01
 *
 */
@Service()
public class TestRegistryServiceImpl implements TestRegistryService {

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

}
5.暴露服务,编写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管理页面比较清晰是哪个应用暴露出来的 --> 
    <dubbo:application name="dubbo_provider" />  
  
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register="" />  

    <!-- 要暴露的服务接口 -->    
    <dubbo:service interface="com.yingze.TestRegistryService" ref="testRegistryService" executes="10" />  
   
    <bean id="testRegistryService" class="com.yingze.TestRegistryServiceImpl" /> 

</beans>
测试main方法 TestRegistryServiceProviderMain:

package com.yingze;

import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**  
 * @ClassName: TestRegistryServiceProviderMain
 * @Description: TODO()
 * @author liang
 * @date 2016年6月23日 上午9:36:52
 *
 */
public class TestRegistryServiceProviderMain {

	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
                new String[]{"applicationProvider.xml"});    
        context.start();   
        System.out.println(" TestRegistryServiceProviderMain is statr... ");
        System.in.read();
	}

}
6.引用服务,编写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管理页面比较清晰是哪个应用暴露出来的 --> 
    <dubbo:application name="dubbo_consumer" />  
  
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" />  

     <!-- 要引用服务的接口 -->   
    <dubbo:reference id="testRegistryServiceReference" interface="com.yingze.TestRegistryService"/>
 
</beans> 
测试main方法 TestRegistryServiceConsumerMain:

package com.yingze;

import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yingze.TestRegistryService;

/**  
 * @ClassName: TestRegistryServiceConsumerMain
 * @Description: TODO()
 * @author liang
 * @date 2016年6月23日 上午9:50:13
 *
 */
public class TestRegistryServiceConsumerMain implements Runnable{

	public static void main(String[] args) throws IOException {
		new Thread(new TestRegistryServiceConsumerMain()).start();
		System.in.read();
	}

	public void run() {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
                new String[]{"applicationConsumer.xml"});    
        context.start(); 
        System.out.println(" TestRegistryServiceConsumerMain is statr... ");
        TestRegistryService is= (TestRegistryService) context.getBean("testRegistryServiceReference");
        String name=is.sayHello("何先生");
        System.out.println(Thread.currentThread().getName() + " "+name); 
	}

}
7.通过登录tomcat中的dubbo-admin-2.5.4可以管理配置暴露的接口和消费的接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值