在Spring项目中集成使用dubbo实现分布式服务

来源:互联网 发布:北京供销大数据集团 编辑:程序博客网 时间:2024/06/05 15:27

一、配置启动Zookeeper

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

下载

http://www-eu.apache.org/dist/zookeeper/

配置

Zookeeper安装目录的conf文件夹下的zoo.cfg文件

# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just # example sakes.dataDir=C:\zookeeper# the port at which the clients will connectclientPort=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

启动

cd C:\zookeeper-3.4.9\binzkServer.cmd

二、服务提供端

pom.xml

添加依赖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.5</version></dependency><dependency>  <groupId>com.101tec</groupId>  <artifactId>zkclient</artifactId>  <version>0.4</version></dependency>

java接口和实现类

  • 接口
public interface Test {    String haha(String name);}
  • 实现类
public class TestImpl implements Test {    @Override    public String haha(String name) {        return "sucesss!"+name;    }}

spring配置文件

  • dubbo-provider.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:application name="dubbo-service" />    <!-- 使用multicast广播注册中心暴露服务地址 -->    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->    <!-- 使用zookeeper注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" />    <!-- 用dubbo协议在20880端口暴露服务 -->    <dubbo:protocol name="dubbo" port="20880" />    <!-- 声明需要暴露的服务接口 -->    <dubbo:service interface="com.xyh.service.Test"                   ref="demoService" />    <!-- 和本地bean一样实现服务 -->    <bean id="demoService" class="com.xyh.service.impl.TestImpl" /></beans>

web.xml

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:dubbo-provider.xml</param-value></context-param><listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

三、客户端

pom.xml

添加依赖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.5</version></dependency><dependency>  <groupId>com.101tec</groupId>  <artifactId>zkclient</artifactId>  <version>0.4</version></dependency>

spring配置文件

<?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="dubbo-service-consumer" />    <!-- 使用multicast广播注册中心暴露服务地址 -->    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->    <!-- 使用zookeeper注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" />    <!-- 声明需要暴露的服务接口 -->    <dubbo:reference id="demoService" interface="com.xyh.service.Test"/></beans>

java调用服务

public class Main {    public static void main(String[] args) {        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("dubbo-consumer.xml");        Test test = (Test) applicationContext.getBean("demoService");        System.out.println(test.haha("xyh"));    }}

四、服务提供端实现分布式

spring配置文件(服务端1)

<!-- 用dubbo协议在20880端口暴露服务 -->    <dubbo:protocol name="dubbo" port="20880" />
  • 实现类
public class TestImpl implements Test {    @Override    public String haha(String name) {        return "sucesss1!"+name;    }}

spring配置文件(服务端2)

<!-- 用dubbo协议在20881端口暴露服务 -->    <dubbo:protocol name="dubbo" port="20881" />
  • 实现类
public class TestImpl implements Test {    @Override    public String haha(String name) {        return "sucesss2!"+name;    }}

spring配置文件(服务端3)

<!-- 用dubbo协议在20882端口暴露服务 -->    <dubbo:protocol name="dubbo" port="20882" />
  • 实现类
public class TestImpl implements Test {    @Override    public String haha(String name) {        return "sucesss3!"+name;    }}

客户端多次调用会输出sucesss1、sucesss2、sucesss3等不一样的结果,这里能看出来zookeeper服务为我们实现了负载均衡,相比hession实现分布式需要自配负载均衡服务器,这是一大优点,省事。