dubbo+zookeeper+spring整合demo

这里先说一下dubbo的安装和部署(windows环境),dubbo控制台克监控到zookeeper提供者和消费者相关情况;

一:dubbo控制台安装和部署

1.下载dubbo war ,地址:http://download.csdn.net/detail/liweifengwf/7784901;

2.修改WEB-INF目录下dubbo.properties文件,修改dubbo.registry.address的值,服务器地址;

3.部署,下载完成后解压 ,将解压后的文件放到tomcat的 webapps/ROOT 目录下,如果ROOT有你之前部署的项目,可在webapps下新建dubbo文件,然后将解压后的文件放到其中,最后启动tomcat;这是一种方案,另一种部署方案可将下载后的文件引入到myEclipse或eclipse中发布到tomcat中,大同小异;

4.访问:tomcat启动成功后,在浏览器地址栏输入127.0.1.1(直接放在ROOT下),若是新建dubbo,则地址是127.0.0.1/dubbo回车,此时会弹出用户名和密码框,这里用户名和密码都是root,输入即可看到dubbo控制台主页面,如下图:


通过上面最后一张图可知,提供者和消费者数量都为0,因为我们还没建立提供者和消费者,这就是我们接下来要说的事了;


二:服务提供者

1.新建一个java项目,并引入相关jar包,主要有spring、dubbo、zookeeper、javassist、log4j、netty、commons-logging包,我最初只导入了前三者,后果就是启动服务失败,然后就根据提示的异常信息依次导入其他相关包;

2.编写服务接口并配置,我的项目结构及想代码如下:



简单的介绍下目录:
service:服务提供者提供的服务接口

test:主要是启动服务以及调用服务接口的代码

application_context_demo_custom.xm:服务提供者配置

application_context_demo_service.xml:消费者相关配置

lib:顾名思义相关jar包

主要代码如下:

接口:

package com.awu.service;
/**
 * remark
 * dwzhou@atman.com
 * 2016年12月19日下午3:27:06
 */
public interface MyService {

	public void say(String hello);
}

接口实现:

package com.awu.service.impl;
import org.springframework.stereotype.Service;
import com.awu.service.MyService;

/**
 * remark
 * dwzhou@atman.com
 * 2016年12月19日下午3:27:57
 */
@Service
public class MyServiceImpl implements MyService{

	@Override
	public void say(String hello) {
		System.out.println("come on:"+hello);
	}
}

服务提供者application_context_demo_service.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="awu_provider"  />
 
    <!-- 使用multicast广播注册中心暴露服务地址 ?backup=192.168.255.153:2189-->
    <dubbo:registry group="paopao" address="zookeeper://127.0.0.1:2181" />
 
 	<!-- 监控中心配置,从注册中心发现监控中心地址 -->  
<!--  	<dubbo:monitor protocol="registry"/>   -->
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    
    <!-- 和本地bean一样实现服务 -->
    <bean id="myService" class="com.awu.service.impl.MyServiceImpl" />
 
 	<!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.awu.service.MyService" ref="myService" timeout="300000"/>
</beans>


服务提供者启动服务:
package com.awu.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * remark
 * dwzhou@atman.com
 * 2016年12月19日下午3:55:56
 */
public class Provider {

	public static void main(String[] args) throws IOException {
		@SuppressWarnings("resource")
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
	                new String[] { "application_context_demo_service.xml" });  
	        context.start();  
	        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟  
	}
}
到这里服务提供者已经构建完成,启动服务提供者服务时需先启动zookeeper服务,二者服务都启动后,这时到zookeeper bin目录下执行zkCil.cmd命令后再输入可看到服务提供者暴露的服务接口,如下图:

之后我们再去dubbo控制台看看提供者的信息,结果去怎么刷新都没有,网上找了很久后发现是我们再配置服务器提供者者的时候配置了分组信息,找到/WEB-INF/dubbo.properties文件打开发现,并没有设置分组信息,所以加上分组信息:

修改前:

修改后:

当然这里加入了变量参数,需要有地方用才行,否则不和没加一样,于是去修改最开始发布到tomcat下dubbo,找到WEB-INF\classes\META-INF\spring下的dubbo-admin.xml文件做相关修改

修改前:

修改后:

以上二步修改完成后,重启tomcat,再访问dubbo控制台,查看服务治理下的提供者克看到提供者相关信息,如下图:

完成以上操作,服务提供者已经创建完成;下面看消费者:

为了方便,我这里直接在刚才创建的项目也就是服务提供者项目下直接增加了消费者的配置文件,修改相关配置,具体如下:

application_context_demo_custom.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    	 
     	 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="awu_consumer"  />
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry group="paopao" address="zookeeper://127.0.0.1:2181" check="false" />
    
    <!-- 和本地bean一样实现服务 -->
    <dubbo:reference id="myService" interface="com.awu.service.MyService" timeout="10000" connections="100"/>
    
    <!-- 监控中心配置,从注册中心发现监控中心地址 -->  
<!--     <dubbo:monitor protocol="registry" />   -->
</beans>

启动

package com.awu.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.awu.service.MyService;

/**
 * remark
 * dwzhou@atman.com
 * 2016年12月19日下午4:02:59
 */
public class Custom {

	public static void main(String[] args) throws IOException {
		@SuppressWarnings("resource")
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
	                new String[] { "application_context_demo_custom.xml" });  
	    context.start();  
	    
	    MyService service = (MyService) context.getBean("myService");
	    service.say("Nice to meet you");
	    
	    System.in.read(); 
	}
}

控制台输出:

到dubbo控制台服务治理下消费者查看消费者相关信息,如下图:


好了,以上就是dubbo+zookeeper+spring 整合了,希望能对大家有所帮助!






  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值