【ZooKeeper + Dubbo】 實戰快速搭建ZooKeeper + Dubbo + Spring項目框架

最近在項目開發中,用到了ZooKeeper + Dubbo的微服務框架,項目是Maven Module Project結構。我不想做過多的文字敘述,網絡資源很多,主要是講述下我是怎麼搭建的。

首先我們要下載zookeeper,我用到的版本是zookeeper-3.4.10,目錄如下:


進入到conf目錄,我們需要copy一份zoo_sample.cfg文件,修改文件名爲zoo.cfg,裏面內容爲:


然後我們再進入bin目錄如下:


我用的是window本,等項目配置好之後,我們首先要啓動ZooKeeper,執行文件zkServer.cmd

現在來說下maven項目,我maven項目用到的是我自己的兩個工程,Maven Module 大家自己去搭建,網上資源很多。先展示下我的兩個項目:

生產者項目:                                                        消費者項目:

    


首先在兩個項目中的pom.xml文件中添加如下依賴:

<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.9</version>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>

然後看下生產者的service代碼:

package com.framework.pt.service;

public interface DubboService {

	public String testDubboMsg(String s);
}
package com.framework.pt.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.framework.pt.service.DubboService;

@Service
public class DubboServiceImpl implements DubboService {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public String testDubboMsg(String s) {
		logger.info("----------調用成功----------");
		return s;
	}

}

然後看下applicationContext-dubbo.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd" >

	<bean id ="dubboService" class="com.framework.pt.service.impl.DubboServiceImpl" />

	<dubbo:application name="dubbo_prividor" logger="slf4j" />

    <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181"  check="false" subscribe="false"/>

    <!-- <dubbo:protocol accesslog="true" name="dubbo" port="20880" /> -->

    <dubbo:service interface="com.framework.pt.service.DubboService" ref="dubboService"/>

</beans>

關於spring框架的搭建我的其他文章多次提到,這裏不再詳細敘述,這樣我們的生產者項目就搭建完畢了。

接着我們看下消費者項目的部分代碼內容:


service接口其實和生產者項目是一樣的,包路徑保持一致

package com.framework.pt.service;

public interface DubboService {

	public String testDubboMsg(String s);
}

我寫了一個controller來前臺調用service,代碼如下:

package com.framework.fund.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.framework.fund.service.HelloWorldService;
import com.framework.pt.service.DubboService;

@Controller
@RequestMapping(value = "/helloWorld", method = RequestMethod.GET)
public class HelloWorldController {

	@Autowired
	private HelloWorldService helloWorldService;
	
	@Autowired
	private DubboService dubboService;

	@RequestMapping(value ="/print",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String print(HttpServletRequest request, HttpServletResponse response) {
		return dubboService.testDubboMsg("測試Dubbo服務");
	}
}

現在看下消費者的applicationContext-dubbo.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd" >

	<dubbo:application name="dubbo_consumer"  logger="slf4j"  />

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

    <dubbo:reference interface="com.framework.pt.service.DubboService" id="dubboService" />

</beans>

我們的項目工程配置好了,現在開始啓動服務,首先先啓動ZooKeeper,執行剛纔的文件,啓動後的界面如下:


然後啓動生產者,項目用jetty啓動的,啓動完如下:


再次啓動消費者項目工程,也是jetty起的,啓動界面如下:



我們現在就開始用瀏覽器調用接口,界面如下:



我們的項目搭建成功了,我們只是簡單的搭建,實戰用還會用到其他的技術,我們慢慢的摸索,一起加油努力~

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