開發工具html |
MyEclipse 10.7git |
JDKgithub |
1.7web |
容器spring |
Tomcat 8(運行dubbo)apache |
zookeeper版本windows |
zookeeper-3.4.6tomcat |
dubboapp |
dubbo-admin-2.5.3webapp |
dubbo-admin-2.5.3下載地址:http://pan.baidu.com/s/1bozCMzP
zookeeper下載地址:http://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.6/
1.dubbo-admin-2.5.3下載完成後。放在webapps文件夾下面。先把默認的tomcat的ROOT項目備份後移除。將dubbo-admin-2.5.3.war改爲ROOT.war 備用
2.zookeeper下載後解壓安裝便可。windows安裝完成後以下圖
進入到conf裏面,會看到zoo_sample.cfg文件。複製一個修改成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=/tmp/zookeeper
# the port at which the clients will connect
#這塊是設置zookeeper的端口。默認2181
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
到bin文件夾下面。啓動zookeeper,windows 點擊zkServer.cmd便可
3.能夠啓動tomcat了。這樣直接訪問 就能看到dubbo的頁面。要否則dubbo啓動會報錯。第一次入門的話建議就用默認的端口配置便可。默認用戶 root 密碼 root
登陸成功之後看到以下頁面
以上就是dubbo+zookeeper的配置和啓動。下面開始Java代碼的編寫
1.建立myDubbo項目爲主項目,結構圖以下 myC P S後續才建立,剛開始是沒有的。
POM.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</groupId>
<artifactId>mydubbo</artifactId>
注意這裏。打包爲POM,分佈式系統的第一步
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mydubbo</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo註冊中心-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--zookeeper客戶端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>mydubbo</finalName>
</build>
建立子項目後自動會加載。剛開始是沒有的
<modules>
<module>myService</module>
<module>myProvider</module>
<module>myConsumer</module>
</modules>
</project>
2.建立服務提供商子項目,MyEclipse操做以下圖示意 對mydubbo項目鼠標右鍵new或者maven插件直接建立maven module項目,填寫子項目名稱。有的若是不選種Create a simple project 自定義建立會報錯。選擇則不會。根據實際狀況來肯定是否選擇
3.建立3個子項目分別爲 myService(服務商(接口),模塊提供方) myProvider(供應者,給dubbo zookeeper註冊暴露接口) myConsumer
</project>
13. package com.xiaoshuai;
15. public interface HelloService {
}
10. 引用接口服務項目 子項目maven install 爲jar
</project>
18. 實現接口服務者
19. package com.xiaoshuai.impl;
22. import com.xiaoshuai.HelloService;
23. public class HelloServiceImpl implements HelloService{
29. }
31. <?xml version="1.0" encoding="UTF-8"?>
32. <beans xmlns="http://www.springframework.org/schema/beans"
53. </beans>
55. package com.xiaoshuai.impl;
56. import org.springframework.context.support.ClassPathXmlApplicationContext;
58. public class ProviderServer {
68. }
</project>
18. <?xml version="1.0" encoding="UTF-8"?>
19. <beans xmlns="http://www.springframework.org/schema/beans"
29. </beans>
31. package com.xiaoshuai;
33. import org.springframework.context.support.ClassPathXmlApplicationContext;
35. public class ConsumerClient {
42. }