Dubbox 环境搭建

第一章:Dubbox简介

 

Dubbox是一个开源的RPC(Remote ProcedureCall Protocol)远程调用框架,是由dangdang对阿里的Dubbo的升级,可以被视为Dubbo的增强版,基本架构没有变动,升级spring2.x到spring3.x,支持restful风格的调用调试方式,丰富了序列化的方式,提高了序列化的性能。

 

Dubbox的Github官网:

https://github.com/dangdangdotcom/dubbox



第二章 环境搭建(基于注册中心是Zookeeper的搭建)

 

2.1 dubbo-admin控制台的安装

 

2.1.1打开dubbox的官网https://github.com/dangdangdotcom/dubbox

点击Clone or download 我们选择最原始的Download ZIP,当前的stable版本是2.8.4

2.1.2下载到某个文件夹下,解压文件夹:

2.1.3 解压dubbox-master.zip

2.1.4按住键盘shift 鼠标右击,选择命令窗口,进入maven编译 :

mvn install -Dmaven.test.skip=true

编译安装大概需要4~5分钟,编译安装成功之后显示如下:

好了,到此为止,dubbox2.8.4就算是编译成功了,接下来我们要先搭建dubbox的控制台,因为可视化是我们最喜欢的模式了

 

2.1.5进入刚才的文件夹D:\dubboxstudy\dubbox-master\dubbo-admin\target

找到dubbo-admin-2.8.4.war文件,因为这是一个war文件,所以我们就使用tomcat启动,下载apache-tomcat-7.0.40-windows-x64.zip到我们dubbox study目录

解压apache-tomcat.zip,然后将刚才的dubbo-admin-2.8.4.war复制到D:\dubboxstudy\apache-tomcat-7.0.40\webapps文件夹下

因为dubbo的注册中心和管理控制台是依赖zookeeper,所以我们在测试环境下,需要启动一个zookeeper的实例,关于zookeeperwindow环境下的搭建就不赘述了,详细参考:

http://blog.csdn.net/morning99/article/details/40426133


2.1.6启动dubbo-admin控制台之前,先启动一个zookeeper实例(因为只是测试,所以就启动zookeeper集群了):


2.1.7启动tomcat,进入D:\dubbox study\apache-tomcat-7.0.40\bin,双击startup.bat

上图表示启动成功,启动成功之后,我们会发现D:\dubbox study\apache-tomcat-7.0.40\webapps下多了一个文件夹dubbo-admin-2.8.4,进入

D:\dubboxstudy\apache-tomcat-7.0.40\webapps\dubbo-admin-2.8.4\WEB-INF,打开dubbo.properties:


可以看到dubbo默认的注册机制是zookeeper,地址也是本地地址:127.0.0.1:2181,假如你此时zookeeper的实例的地址不是127.0.0.1:2181,或者注册机制是Redis的话,需要修改dubbo.properties的配置,此处就不做修改了

 

 

2.1.8打开浏览器。输入http://localhost:8080/dubbo-admin-2.8.4/,账户密码是root/root.就可以看到页面了

2.2 dubbo的Provider/Consumer 消费者和提供者的Demo代码编写

 

2.2.1环境准备JDK1.7 +Eclipse(STS) + Maven3.x

 

 

2.2.2新建WorkingSet

点击finish:

2.2.3新建maven项目

建好之后的目录结构:

2.2.4在bazinga-provider和bazinga-consumer的pom.xml中引入dubbox的依赖(暂时使用2.8.3的依赖,相对简单一点):

<properties>
		<dubbox.version>2.8.3</dubbox.version>
		<slf4j.version>1.7.5</slf4j.version>
		<zookeeper.version>3.4.6</zookeeper.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>${dubbox.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>${zookeeper.version}</version>
			<exclusions>
				<exclusion>
					<groupId>io.netty</groupId>
					<artifactId>netty</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-api</artifactId>
				</exclusion>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>jline</groupId>
					<artifactId>jline</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.2</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.0.13</version>
		</dependency>
	</dependencies>

2.2.5在bazinga-provider编写调用接口IDemoService:

package org.bazinga.service;

public interface IDemoService {
	
	public String sayHello();

}
具体的实现:

package org.bazinga.service.impl;

import org.bazinga.service.IDemoService;

public class IDemoServiceImpl implements IDemoService {

	public String sayHello() {
		return "hello dubbox";
	}

}

2.2.6在src/main/resources下配置dubbo基于Spring的配置文件spring-dubbo-provider.xml,在这里需要配置注册中心的地址,通信的协议方式,服务提供者的应用名,最后就是最关键的需要暴露的服务,我们这里就是

<?xml version="1.1" 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 owner="lyncc" name="bazinga-app" />
    <!--zookeeper注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/> 
    
    <dubbo:protocol name ="dubbo" port="20880" />
    <!-- 发布这个服务 -->
    <dubbo:service  protocol="dubbo"  timeout="4000" connections="100" interface ="org.bazinga.service.IDemoService" ref="demoService" />       
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="org.bazinga.service.impl.IDemoServiceImpl" />
    
</beans>

细心的你会发现左侧有报错的提示:

这个报错是因为不识别dubbo的命名空间,所以需要导入xsd文件,dubbo.xsd文件源码中有,网上也可以下载到,下载好或者在源码中找到之后,选择Windw->Perferences->XML->XMLCatelog->User Specifed Entries

点击add,点击FileSystem 选择你下载好的dubbo.xsd,输入key值,key值要与schema值一样,点击OK:

一路保存刚才的设置,重新打开spring-dubbo-provider.xml文件,可以发现报错消失。到此为止,dubbox的服务提供者端的代码已经编写完毕,我们写个测试类测试一下:

package org.bazinga.service.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DubboxProviderDemoService {

	public static void main(String[] args) throws InterruptedException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"spring-dubbo-provider.xml");
		context.start();
		Thread.sleep(2000000l);
	}

}

启动main函数之后,会发现dubbo-admin的控制台中提供者发现了该服务:



2.2.7服务提供者模块的编写,服务消费者只要有服务的接口就可以了,把服务提供者的接口复制到bazinga-consumer项目的同一个package下,注意必须放在同一个package下,也就是说服务消费者和服务提供者的接口的路径必须完全相同,因为这是服务的唯一标识,是一一对应的:



2.2.8服务消费端dubbo的配置文件的编写spring-dubbo-consumer.xml,因为dubbo具有服务自动发现的功能,所以我们这边只需要配置注册中心,服务消费者的名字,和需要订阅的服务接口信息,如下:

<?xml version="1.1" 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 owner="lyncc" name="bazinga-consumer" />
    <!--zookeeper注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/> 
    
    <dubbo:reference id="demoService" interface="org.bazinga.service.IDemoService"/> 
    
</beans> 

2.2.9编写测试类DubboConsumerDemoService:

package org.bazinga.service.test;

import org.bazinga.service.IDemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DubboConsumerDemoService {

	public static void main(String[] args) throws InterruptedException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"spring-dubbo-consumer.xml");
		context.start();
		IDemoService demoService = (IDemoService)context.getBean("demoService");
		System.out.println(demoService.sayHello());
		Thread.sleep(2000000l);
	}

}

先运行DubboxProviderDemoService的情况下,启动DubboConsumerDemoService的main函数,运行结果:



同时控制台admin页面也会显示消费者的信息:


好了,到此为止,最简单的Dubbo的Helloworld搭建完毕

 

 

2.3 本章小结

 

  本章简单的搭建了一个Dubbo的Demo,配置了dubbo-admin控制页面平台,编写了一个简单的Hello World,服务提供者向zookeeper注册中心注册服务,服务消费者从注册中心订阅服务,发现服务的暴露地址,完成远程调用,下一个章节,我们稍微深入体验一下dubbo给我们带来的丰富的RPC的一些特性


  • 9
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值