dubbo zookeeper spring整合之helloworld

dubbo是目前很是流行的分佈式服務技術,不少公司都在用。空閒之餘,搭了個helloworld,分享給你們。
本文demo下載
1.下載 zookeeper
zookeeper是服務的註冊中心,下載後進入到安裝目錄G:bakCenterzookeeper-3.3.6bin。
雙擊zkServer.cmd便可啓動註冊中心服務。
zkServer.sh【Linux】或zkServer.cmd【Windows】
Zookeeper的配置文件在 conf 目錄下,這個目錄下有 zoo_sample.cfg 和 log4j.properties,須要將zoo_sample.cfg 更名爲 zoo.cfg,Zookeeper在啓動時會找這個文件做爲默認配置文件html

# 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.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181

配置說明能夠參考這裏
2.服務提供者
定義服務接口:java

package com.hy.service;
public interface DemoService {
    String sayHello(String name);
}

實現服務接口:git

package com.hy.service.impl;

import com.hy.service.DemoService;
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        System.out.println("init : " + name);
        return "hello " + name;
    }
}

在Spring配置中註冊服務。github

<?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-demo" />
    <!-- zookeeper註冊中心 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <dubbo:protocol name="dubbo" port="20880" />
    
    <!-- 和本地bean同樣實現服務 --> 
    <bean id="demoService" class="com.hy.service.impl.DemoServiceImpl" />

    <!-- 向註冊中心註冊暴漏服務地址,註冊服務 -->
    <dubbo:service interface="com.hy.service.DemoService"
        ref="demoService" executes="10" />

</beans>

執行服務提供方主方法啓用服務:web

package main;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderMain {

    public static void main(String[] args) throws IOException {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" });
        context.start();

        System.out.println("服務註冊成功..");
        System.in.read();
        context.close();
    }
}

3.dubbo監控中心查看服務下載地址
下載後,將dubbo-admin-2.5.4-SNAPSHOT.war包放置tomcat服務器的webapps目錄,啓動tomcat服務器,打開下面連接便可查看可用的服務及其狀態 http://localhost:8080/dubbo-a...圖片描述
用戶名和密碼配置在C:Program FilesApache Software FoundationTomcat 7.0webappsdubbo-admin-2.5.4-SNAPSHOTWEB-INFdubbo.properties文件中spring

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

4.服務消費者
經過Spring配置訂閱服務提供方暴露的服務apache

<?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-dubbo-demo" />

    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 向註冊中心訂閱服務 -->
    <dubbo:reference id="demoService" interface="com.hy.service.DemoService" />
</beans>

執行服務消費方主方法啓用服務:tomcat

package main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hy.service.DemoService;

public class ConsumerMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationConsumer.xml" });
        context.start();
        DemoService service = (DemoService) context.getBean("demoService");
        System.out.println(service.sayHello("world"));
        context.close();
    }
}

在控制檯便可見服務提供方方法的調用結果。
圖片描述服務器

相關文章
相關標籤/搜索