在Spring項目中集成使用dubbo實現分佈式服務

一、配置啓動Zookeeper

ZooKeeper是一個分佈式的,開放源碼的分佈式應用程序協調服務,是Google的Chubby一個開源的實現,是Hadoop和Hbase的重要組件。它是一個爲分佈式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名服務、分佈式同步、組服務等。

下載

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

配置

Zookeeper安裝目錄的conf文件夾下的zoo.cfg文件

# 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=C:\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

啓動

cd C:\zookeeper-3.4.9\bin
zkServer.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實現分佈式需要自配負載均衡服務器,這是一大優點,省事。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章