學習目錄在上一篇中有了,這篇我們來寫一個本地的單服務的demo,下面我們看看需要什么!?。?/p>
1、zookeeper的安裝:
(1)、zookeeper 的安裝很簡單,下載架包解壓縮,解壓縮后的目錄下有個conf目錄也就是zookeeper的配置文件在該目錄下,該目錄下有一個zoo_sample.cfg 文件,復制一個修改為zoo.cfg,像我們這里是單服務的所以這里的配置只需要修改dataDir的目錄就可以了其他的可以不用修改,dataDir的目錄可以自己隨便定義?。?!
2、服務提供者
創(chuàng)建一個maven 工程,配置spring ,dubbo,zookeeper等相關的jar包,下面來具體看看!
(1)、目錄結構
(2)、我們配置pom.xml下載我們需要的jar包,pom.xml配置文件如下:
[html]view plaincopyprint?
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.test
dubboser
0.0.1
jar
dubboserver
http://maven.apache.org
UTF-8
3.1.4.RELEASE
1.6.6
junit
junit
3.8.1
test
org.springframework
spring-aop
${spring.version}
org.springframework
spring-asm
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-expression
${spring.version}
log4j
log4j
1.2.16
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
com.alibaba
dubbo
2.5.3
com.github.sgroschupf
zkclient
0.1
org.apache.zookeeper
zookeeper
3.4.5
dubbo-demo
org.apache.maven.plugins
maven-compiler-plugin
2.1
1.5
1.5
UTF-8
false
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.test
dubboser
0.0.1
jar
dubboserver
http://maven.apache.org
UTF-8
3.1.4.RELEASE
1.6.6
junit
junit
3.8.1
test
org.springframework
spring-aop
${spring.version}
org.springframework
spring-asm
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-expression
${spring.version}
log4j
log4j
1.2.16
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
com.alibaba
dubbo
2.5.3
com.github.sgroschupf
zkclient
0.1
org.apache.zookeeper
zookeeper
3.4.5
dubbo-demo
org.apache.maven.plugins
maven-compiler-plugin
2.1
1.5
1.5
UTF-8
false
(3)、寫服務接口,服務接口的實現(xiàn)
[java]view plaincopyprint?
packagecom.test.dubboser;
publicinterfaceServiceDemo?{
publicString?say(String?str);
}
package com.test.dubboser;
public interface ServiceDemo {
public String say(String str);
}
[java]view plaincopyprint?
packagecom.test.dubboser;
publicclassServiceImpimplementsServiceDemo{
publicString?say(String?str)?{
System.err.println("server:?"+str);
return"hello:?"+str;
}
}
package com.test.dubboser;
public class ServiceImp implements ServiceDemo{
public String say(String str) {
System.err.println("server: "+str);
return "hello: "+str;
}
}
(4)、向注冊中心注冊服務(將接口暴露出去),這里的注冊中心是zookeeper
下面我們來看看,服務的配置也就是applicationProvider.xml這個配置文件中配置……
[html]view plaincopyprint?
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
">
ref="demoService"/>
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
">
ref="demoService" />
(5)、所有的準備都好了,下面我們啟動服務提供者(提前啟動zookeeper)
[java]view plaincopyprint?
packagecom.test.dubboser;
importjava.io.IOException;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassMain?{
publicstaticvoidmain(String[]?args)throwsIOException?{
ClassPathXmlApplicationContext?context?=newClassPathXmlApplicationContext(newString[]?{"applicationProvider.xml"});
context.start();
System.out.println("按任意鍵退出");
System.in.read();
}
}
package com.test.dubboser;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
context.start();
System.out.println("按任意鍵退出");
System.in.read();
}
}
ok這樣我們的服務提供者就好了,是不是很亂,我感覺也有點,所以之后會有個自己的小總結……
我們的服務提供者好了,我們來看看我們的服務消費者,也就是調用我們這里的服務的程序……
3、服務消費者開發(fā)
服務消費者的編寫還是按照服務提供者一樣的順序來
(1)、目錄結構
(2)、maven的pom.xml配置文件獲取需要的架包
[html]view plaincopyprint?
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.test
dubbocli
0.0.1-SNAPSHOT
jar
dubboclient
http://maven.apache.org
UTF-8
3.1.4.RELEASE
1.6.6
junit
junit
3.8.1
test
org.springframework
spring-aop
${spring.version}
org.springframework
spring-asm
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-expression
${spring.version}
log4j
log4j
1.2.16
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
com.alibaba
dubbo
2.5.3
com.github.sgroschupf
zkclient
0.1
org.apache.zookeeper
zookeeper
3.4.5
dubbo-demo
org.apache.maven.plugins
maven-compiler-plugin
2.1
1.5
1.5
UTF-8
false
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.test
dubbocli
0.0.1-SNAPSHOT
jar
dubboclient
http://maven.apache.org
UTF-8
3.1.4.RELEASE
1.6.6
junit
junit
3.8.1
test
org.springframework
spring-aop
${spring.version}
org.springframework
spring-asm
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-expression
${spring.version}
log4j
log4j
1.2.16
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
com.alibaba
dubbo
2.5.3
com.github.sgroschupf
zkclient
0.1
org.apache.zookeeper
zookeeper
3.4.5
dubbo-demo
org.apache.maven.plugins
maven-compiler-plugin
2.1
1.5
1.5
UTF-8
false
注意:服務提供者 ,服務消費者以及安裝的zookeeper保持相同的版本號,這樣能省去很多麻煩,而不同版本的錯誤在前中有記錄過可以看看……
(3)、服務消費者的配置applicationConsumer.xml
[html]view plaincopyprint?
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
">
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
">
(4)、消費端,也就是調用服務端的方法
[java]view plaincopyprint?
packagecom.test.dubbocli;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcom.test.dubboser.ServiceDemo;
publicclassMain?{
publicstaticvoidmain(String[]?args)throwsInterruptedException?{
inti=0;
while(i++<5){
run();
Thread.sleep(3000);
}
}
publicstaticvoidrun(){
ClassPathXmlApplicationContext?context?=newClassPathXmlApplicationContext(newString[]?{"applicationConsumer.xml"});
context.start();
ServiceDemo?demoServer?=?(ServiceDemo)?context.getBean("demoServicemy");
String?str=demoServer.say("java?---->>>");
System.err.println("res:?"+str);
}
}
package com.test.dubbocli;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.dubboser.ServiceDemo;
public class Main {
public static void main(String[] args) throws InterruptedException {
int i=0;
while(i++<5){
run();
Thread.sleep(3000);
}
}
public static void run(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
context.start();
ServiceDemo demoServer = (ServiceDemo) context.getBean("demoServicemy");
String str=demoServer.say("java ---->>>");
System.err.println("res: "+str);
}
}
注意:這里我們引入了一個類 com.test.dubboser.ServiceDemo 類,這個類是服務端的接口,所以在啟動消費端的時候我們要把服務端生成jar包導入到消費端……
我們看到了消費端調用服務端的方法就和調用本地的方法一樣方便,ok 例子就到這里……
4、整個開發(fā)流程:
(1)、安裝zookeeper,修改dataDir
(2)、開發(fā)服務端即服務提供者,當然前提示已經有了zookeeper dubbo spring 等jar包,這里使用了maven也就是pom.xml 配置
(3)、將服務端注冊到注冊中心暴露服務地址配置添加
(4)、使用dubbo協(xié)議將指定的服務端口暴露配置添加
(5)、聲明需要暴露的服務接口
(6)、實現(xiàn)服務接口
(7)、啟動服務端
這樣 整個服務提供者就ok了
(1)、服務消費者開發(fā),同樣需要zookeeper dubbo spring 等相關的jar包,這里使用的是maven所以配置pom.xml
(2)、使用zookeeper廣播注冊中心發(fā)現(xiàn)暴露的服務地址的配置添加
(3)、生成 遠程代理的配置添加
(4)、將服務端生成jar包引入
(5)、調用服務端服務
5、注意點
(1)、三個地方提到了zookeeper 、zookeeper的安裝、服務端注冊、客戶端注冊 這三個地方的zookeeper使用同一版本,這樣能省去一些不必要的錯誤?。?!
(2)、記得在服務消費者引入服務提供者的jar,也就是要把服務端打成jar包引入到服務消費者?。?!
6、使用dubbo、zookeeper到底做了什么
我們在網上能查到dubbo、zookeeper是做什么的,這里不說的那么具體還詳細,第一你能看到官網的解釋,第二我也剛開始學習不能誤人子弟,下面就是我個人的簡單理解:
zookeeper 是注冊中心,我們寫的服務注冊到注冊到zookeeper,zookeeper將服務端的相關配置存儲并管理更新,而服務消費者鏈接到zookeeper注冊中心通過zookeeper這個注冊中心獲取服務端的相關信息,生成遠程代理服務就調用服務端提供的方法了!?。∵@是我簡單的一個理解,當然zookeeper還有軟負載等功能,但是具體的怎么實現(xiàn)的也不太清,先知道的自己可以查看資料學習?。?!
7、demo源碼下載
(1)、zookeeper 安裝包下載:http://download.csdn.net/detail/qh_java/9655026
(2)、服務端服、服務消費者以及服務端的jar包(maven工程)下載:http://download.csdn.net/download/qh_java/9655491
ok到此我們簡單的小demo學習就ok了 下片我們看看本地偽集群的配置
愿意了解或者源碼的朋友直接求求交流分享技術:2042849237
更多詳細源碼參考來源:http://minglisoft.cn/technology