前言
一、安装zookeeper
1、zookeeper简介
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户
而dubbo就是依赖zookeeper的一个分布式框架,当然二次开发的dubbox肯定也会依赖于zookeeper,所以我们需要先安装zookeeper
2、下载zookeeper
3、安装zookeeper
因为我是在Windows环境配置的,所以就简单说一下windows下面的配置吧,首先解压压缩包,然后进入conf文件夹,复制一下zoo_sample.cfg创建副本,然后重命名为zoo.cfg,因为zookeeper只识别zoo.cfg,而默认是没有这个文件的,zoo_sample.cfg是默认的配置文件,但是因为文件名的原因,所以zookeeper无法识别,当然直接重命名zoo_sample.cfg也是可以的,只是看自己喜欢咯
4、启动zookeeper
Windows环境下直接运行bin目录下的zkServer.cmd就可以,如果是Linux环境,则在bin目录下运行
./zkServer.sh start
命令,就可以启动zookeeper
5、添加dubbox依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
6、添加服务提供者的接口
接口类:
package wang.raye.dubbo.interfaces;
public interface DubboInterface {
public String hello(String name);
}
接口实现类:
package wang.raye.dubbodemo1;
import org.springframework.stereotype.Service;
import wang.raye.dubbo.DubboInterface;
@Service
public class DubboImpl implements DubboInterface {
public String hello(String name) {
return "hello "+name+" this is dubbodemo1";
}
}
7、添加dubbo配置xml
xml文件放在sources文件夹,名字可以随便命名,我这里是dubbo.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
default-lazy-init="false" >
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="dubbo-provider1"></dubbo:application>
<!--使用注解方式暴露接口
<dubbo:annotation package="wang.raye.dubbodemo1" /> -->
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.1.126:2181" check="false" subscribe="false" register=""></dubbo:registry>
<!-- 要暴露的服务接口-->
<dubbo:service interface="wang.raye.dubbo.interfaces.DubboInterface" ref="dubboImpl" />
</beans>
这里每个节点都有解释了,相信不过过多解释
8、配置消费者
类中引用远程提供者
package wang.raye.dubbodemo3.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import wang.raye.dubbo.DubboInterface;
@Controller
public class DubboControll {
@Autowired
private DubboInterface interface1;
@RequestMapping("/hello")
@ResponseBody
public String hello(String name){
return interface1.hello(name);
}
}
消费者的xml配置
<beans
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo-custom-dsp"/>
<dubbo:registry address="zookeeper://192.168.1.126:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="dubboService" interface="wang.raye.dubbo.interfaces.DubboInterface" />
</beans>
这里也是每个接点都有注释,应该不用多说,而且比较本文不是讲究dubbo配置的,主要是讲spring boot集成dubbox
9、引用dubbo配置xml
在Spring boot的Application类添加注解
@ImportResource({"classpath:dubbo.xml"})
因为我的xml名字是dubbo.xml,所以当你用的时候需要换成自己的xml名字,==注意:消费者项目和服务提供者的项目的Application类都需要配置此注解==
结尾
至此,spring boot集成duubox就说完了,当然这样说肯定很空洞,所以我吧我测试的项目上传到了github,大家可以参考看看,当然要测试的话需要修改dubbo.xml的dubbo:registry 节点中的zookeeper地址。