一步步教你搭dubbo+zopper+spring整合使用负载均衡容错服务

一直奔波于生活和工作中,突然发现博客近半年没用碰过了,更重要的发现是2015年只剩下几个月了……,时间真的太快太快!

先说说题外话:
这篇文章是朋友需要就帮忙捣腾了下。笔者借鉴了网上不下100篇文章才整理了如下文章,但是发现他们写的文章不是Copy别人的就是一上来就开始写过程,在这个过程中他们的文章80%一模一样的文字总感觉缺少了什么所以现在就让笔者带你进入这个过程中来..

Dubbo是什么?

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,Dubbo是阿里巴巴SOA服务化治理方案的核心框架在这种情况下诞生的。

Dubbo的背景介绍?

我这就省略…了可以百度他们写的文章都Copy过我就不重复了。

Dubbo能做什么?

透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

Ox1 第一步

什么是Zookeeper及作用?

在此笔者附上链接解释:http://www.aboutyun.com/thread-6628-1-1.html

在Windows下安装或者在Linux环境下安装zookeeper
去官网下载zookeeper-3.4.6然后安装笔者这给出官网的下载地址:http://www.apache.org/dist/zookeeper/zookeeper-3.4.6/
然后在对应的zookeeper-3.4.6/conf 下有一个文件zoo_sample.cfg的这个文件里面配置了监听客户端连接的端口等一些信息,Zookeeper 在启动时会找zoo.cfg这个文件作为默认配置文件,所以我们复制一个名称为zoo.cfg的文件,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

参数意思解释
tickTime=2000 # Zookeeper服务器心跳时间,单位毫秒
dataDir=/tmp/zookeeper # 数据持久化路径, 笔者使用的默认路径你们可以自行修改
clientPort=2181 # 连接端口
initLimit=5 # 投票选举新leader的初始化时间。
syncLimit=2 # Leader与Follower之间的最大响应时间单位,响应超过syncLimit*tickTime,Leader认为Follwer死掉,从服务器列表中删除Follwer
dataLogDir=/home/hadoop/zookeeper/logs # 日志保存路径 这个要自己新建

然后下载dubbo-admin-2.5.4.war 在Windwos或者Linux的tomcat中部署,先把dubbo-admin-2.5.4放在tomcat的webapps/ROOT下(先删除自带ROOT目录里面的内容或者备份里面的内容替换成dubbo2.5.4的内容到ROOT目录里面)。
然后进行解压:
Windows下解压:直接解压到root里面。
Linux下解压命令: #jar -xvf dubbo-admin-2.4.1.war
然后到webapps/ROOT/WEB-INF下,有一个dubbo.properties文件,里面指向Zookeeper ,使用的是Zookeeper 的注册中心,如图所示:

0x2 第二步

sping代码部分与dubbo的整合

首先做过spring项目的大家都知道先新建一个工程笔者这是spring3.1的jar包。
新建一个znn-service-provider工程

一、提供者部分
新建一个DemoService.java类,提供者和消费者都要有这个Service,

在新建一个服务提供方的实现类接口DemoServiceImpl.java,对服务消费方隐藏实现

用Spring配置声明暴露服务:
新建一个dubbo-provider.xml文件对dubbo进行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

<?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:application name="hello-world-app" />

<!-- 使用multicast广播注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->

<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://182.92.228.183:2181" />

<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.znn.provider.DemoService" ref="demoService" />

<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.znn.provider.DemoServiceImpl" />
</beans>

注:有两种暴露地址的方法,广播的那个在测试消费者的时候没有成功,就自己搭了一个zookeeper,使用zookeeper来管理。

二、消费者部分
另外新建一个工程:znn-service-consumer
导入第三方包,和提供者一样。
新建一个dubbo-consumer.xml,对dubbo进行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

<?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:application name="consumer-of-helloworld-app"/>
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://182.92.228.183:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.znn.provider.DemoService" />
</beans>

新建一个测试类实际项目中用的时候会放到特定的执行环节中去运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

package com.znn.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.znn.provider.DemoService;

public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:C:/Users/Dell/Desktop/dubbo/DubboDemo-master/znn-service-consumer/WebRoot/WEB-INF/dubbo-consumer.xml");
context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理C:\Users\Dell\Desktop\dubbo\DubboDemo-master\
String hello = demoService.sayHello("world"); // 执行远程方法
System.out.println(hello);
}
}

服务器在tocmat6-7中跑起来后运行Consumer消费者测试出现以下表示服务配置正常。

然后访问dubbo_admin管理控制台页面可以看到服务器提供者上线了,用户名和密码:root,并访问服务,显示登陆页面,说明dubbo-admin2.5.4部署成功!

就先搭到这里项目中用到了的话,下一版总结过程就是在项目实际中用到遇到的问题方面的内容了!