四個步驟
1 搭建zookeeper消息中心
2 搭建dubbo服務端代碼
3 寫dubbo用戶端代碼
4 dubbo提供的dubbo-admin-2.5.4.war 管理控制台
一,搭建zookeeper
1 下載下傳zookeeper安裝檔案
zookeeper-3.3.6.tar.gz
這裡提供一個百度網盤的下載下傳位址http://pan.baidu.com/s/1gexxfvP
2 執行指令 tar zxvf zookeeper-3.4.6.tar.gz 解壓,
3 到 zookeeper-3.3.6/conf 目錄下
把zoo_sample.cfg 複制一份為zoo.cfg,作為預設配置檔案

4 配置說明
tickTime:這個時間是作為 Zookeeper 伺服器之間或用戶端與伺服器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
dataDir:顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 将寫資料的日志檔案也儲存在這個目錄裡。
clientPort:這個端口就是用戶端連接配接 Zookeeper 伺服器的端口,Zookeeper 會監聽這個端口,接受用戶端的通路請求。
我的配置: 端口2181是預設配置
5 在zookper-3.3.6/bin目錄下執行
./zkServer.sh start 啟動zookeeper
6 驗證zookeeper是否啟動
使用netstat -lpn | grep 2181
如果有端口号, 說明已經啟動
1,pom.xml
Xml代碼
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhang.dubboServer</groupId>
<artifactId>dubboServer</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>dubboServer Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<version>4.1.6.RELEASE</version>
<artifactId>spring-webmvc</artifactId>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<groupId>dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.15.0-GA</version>
<classifier>GA</classifier>
<groupId>netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<groupId>zkclient</groupId>
<artifactId>zkclient</artifactId>
<version>0.4</version>
<groupId>zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.1</version>
</dependencies>
<build>
<finalName>dubboServer</finalName>
</build>
</project>
2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="false">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/springmvc-config.xml;classpath*:dubbo/*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</filter>
<filter-mapping>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3,spring和dubbo配置檔案
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven ></annotation-driven>
<!--spring 自動掃描com.zhang下邊的所有類 -->
<context:component-scan base-package="com.zhang" />
<context:property-placeholder location="classpath:config.properties" />
<!-- 自定義配置Service -->
<beans:bean id="sayHelloService" class="com.zhang.service.demoImpl"/>
</beans:beans>
<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="DubboDemo" />
<!-- 注冊中心 -->
<dubbo:registry protocol="zookeeper" address="${able_zookeeper}" />
<!-- 是否納入調用統計報表(可選) -->
<dubbo:monitor protocol="registry"/>
<!-- 協定 -->
<dubbo:protocol name="dubbo" port="31581" />
<!-- 服務者與消費者的預設配置 -->
<!-- 延遲到Spring初始化完成後,再暴露服務,服務調用逾時設定為6秒,逾時不重試 -->
<dubbo:provider delay="-1" timeout="6000" retries="0"/>
<dubbo:consumer timeout="6000" retries="0"/>
</beans>
<dubbo:service interface="com.zhang.service.demo" ref="sayHelloService" timeout="3000" protocol="dubbo"/>
config.properties
Java代碼
#able_zookeeper
able_zookeeper=192.168.50.42:2181
4,java代碼 一個接口一個實作類
package com.zhang.service;
public interface demo {
public String sayHello();
}
public class demoImpl implements demo {
@Override
public String sayHello() {
return "hello";
}
1,pom.xml和web.xml,config.properties配置檔案和DubboServer配置一樣
2,增加一個消費者配置檔案applicationContext-dubbo-consumer.xml,注:這裡需要把服務提供者的jar包打入到用戶端,打接口就可以
<strong><?xml version="1.0" encoding="UTF-8"?>
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:reference id="demo" interface="com.zhang.service.demo" check="false" protocol="dubbo"/>
</beans> </strong>
3,使用
<strong>package com.zhang.contr;
import org.springframework.beans.factory.annotation.Autowired;
public class test {
@Autowired
private com.zhang.service.demo demo;
public String testD(){
return demo.sayHello();
</strong>
war包下載下傳位址:http://pan.baidu.com/s/1i4xZamD
把這個war包放到tomcat的webapps目錄下
1 修改tomcat的端口号不要和其他服務沖突.
2 修改 WEB-INF 下的dubbo.properties檔案
3 啟動tomcat,通路.
原文位址http://www.bieryun.com/2022.html
想看源碼的提供一個github的通路位址
https://github.com/zqh1989/DubboDemo