dubbo註冊zookeeper集羣配置,dubbo控制檯查看提供者和消費者

本文開始前已搭好以下環境:

1.zookeeper集羣:http://blog.csdn.net/lishirong/article/details/52880946

2.dubbo控制檯管理工具

3.用IntellijIdea2016 搭建的基本dubbo項目框架

本實例中dubbo服務提供者以項目啓動中在applicationContext.xml中進行注入,其中web.xml配置如下:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <!-- WEB應用名稱 -->
    <display-name>demo.provider</display-name>
    <!-- WEB應用說明 -->
    <description>此WEB應用用於展示DUBBO的應用結構</description>
    <!-- 配置Spring配置文件路徑 -->
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        classpath:applicationContext.xml
      </param-value>
    </context-param>
    <!-- 配置Spring上下文監聽器 -->
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置Spring字符編碼過濾器 -->
    <filter>
      <filter-name>encodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
      </init-param>
      <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置log4j配置文件路徑、檢測日誌配置文件變化 -->
    <context-param>
      <param-name>log4jConfigLocation</param-name>
      <param-value>classpath:log4j.properties</param-value>
      <param-name>log4jRefreshInterval</param-name>
      <param-value>30000</param-value>
    </context-param>
    <!-- 配置Log4j監聽器 -->
    <listener>
      <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!-- 首頁 -->
    <welcome-file-list>
      <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 錯誤頁 -->
    <error-page>
      <error-code>404</error-code>
      <location>/error/404.jsp</location>
    </error-page>
    <error-page>
      <error-code>500</error-code>
      <location>/error/500.jsp</location>
    </error-page>
  </web-app>

applicationContext.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
        ">
    <!-- 具體的實現bean -->
    <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />

    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="demo_provider" />

    <!-- 使用multicast廣播註冊中心暴露服務地址 <dubbo:registry address="multicast://224.5.6.7:1234"
        /> -->

    <!-- 使用zookeeper註冊中心暴露服務地址 -->
    <dubbo:registry address="zookeeper://192.168.0.104:2181?backup=192.168.0.105:2181,192.168.0.111:2181" />

    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 聲明需要暴露的服務接口 -->
    <dubbo:service interface="com.unj.dubbotest.provider.DemoService"
                   ref="demoService" />

</beans>

主要是看zookeeper中心的集羣註冊,其它的服務實現類和實現方法往上也有很多,此處不做探究。項目啓動以後,在dubbo管理控制檯查看到如下效果:


我本機IP是192.168.0.125,我選擇在20880端口暴露dubbo服務,同樣的,服務關閉以後,此處就不能查到125的服務了。

下面描述一下消費者的配置,先看一下消費者的applicationContext.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="demo_consumer" />


    <!-- 使用zookeeper註冊中心暴露服務地址 -->
    <dubbo:registry address="zookeeper://192.168.0.104:2181?backup=192.168.0.105:2181,192.168.0.111:2181"  />

    <!-- 生成遠程服務代理,可以像使用本地bean一樣使用demoService -->
    <dubbo:reference id="demoService"
                     interface="com.unj.dubbotest.provider.DemoService" />

</beans>

消費者示例類Consumer.java

package com.alibaba.dubbo.demo.pp;

import com.unj.dubbotest.provider.DemoService;
import com.unj.dubbotest.provider.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Consumer {

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "applicationContext.xml" });
		context.start();

		DemoService demoService = (DemoService) context.getBean("demoService");
		String hello = demoService.sayHello("tom");
		System.out.println(hello);

		List<User> list = demoService.getUsers();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i).getName());
			}
		}System.in.read(); // 爲保證服務一直開着,利用輸入流的阻塞來模擬
	}

}
運行主函數以後,在控制檯可以看到:

至此,可以看到,我的zookeeper集羣,dubbo服務提供者,dubbo服務消費者,dubbo服務管理控制檯監控中心,均工作正常,dubbo服務的簡單架構運行較好。

個人感覺,dubbo的這種通過zookeeper發現服務,並自動負載均衡的調用服務的方式,還是蠻先進的,有很多容災方面的考慮,效率也很高,安全性較好,可以繼續研究。後面再加上緩存和數據庫方面的性能優化,總體架構性能應該會相當之高,後續再研究吧!


感謝:感謝兄弟們網上提供的那麼多研究成果和代碼,我的也是基於網上的一些研究成果代碼進行改進,也希望能幫助到一些人。

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