|
1
2
3
4
5
6
7
8
9
10
11
12
|
# 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.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
|
- tickTime:這個時間是作為 Zookeeper 服務器之間或客戶端與服務器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
- dataDir:顧名思義就是 Zookeeper 保存數據的目錄,默認情況下,Zookeeper 將寫數據的日志文件也保存在這個目錄里。
- clientPort:這個端口就是客戶端連接 Zookeeper 服務器的端口,Zookeeper 會監聽這個端口,接受客戶端的訪問請求。
|
1
2
3
|
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
|
address:zookeeper 的ip地址 后面是端口號 ,和zookeeper中配置的端口號一樣
修改完成后需要一個tomcat 打開tomcat \webapps\ROOT 目錄 ,此目錄放置的是tomcat的首頁,刪除所有的文件,將解壓后修改好的所有的dubbo-admin 里的文件復制到 \webapps\ROOT中,
此時我們已經配置好了。現在可以啟動tomcat了 (主意:在啟動tomcat之前要先啟動zookeeper ,啟動zookeeper前面有介紹)。 啟動tomcat之后 在瀏覽器輸入 http://localhost:8080 (我的是8083) 進入dubbo管控台的主頁
(備注:使用jdk1.8 tomcat啟動可能會出錯 有關 URI。。。。 的錯誤。我最后換成1.7了)

用戶名和密碼就是dubbo.properties 中配置的 默認的是 用戶名 :root, 密碼:root
輸入用戶名和密碼之后 進入首頁

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package
com.cl.user.serviceImpl;
import
org.springframework.stereotype.Service;
import
com.cl.user.servicei.UserService;
@Service
(
"userService"
)
public
class
UserServiceImpl
implements
UserService{
@Override
public
String sayHello() {
System.out.println(
"hello world----------------------------"
);
return
"hello world"
;
}
}
|
|
1
2
3
4
5
6
|
package
com.cl.user.servicei;
public
interface
UserService {
public
String sayHello();
}
|
當然 項目 1 要 依賴 項目 2
所以在1 的 pom.xml 中 有
1 <dependency> 2 <groupId>test-web</groupId> 3 <artifactId>test-pubilc-interface</artifactId> 加入項目2依賴 4 <version>0.0.1-SNAPSHOT</version> 5 <scope>test</scope> 6 </dependency>
那么 1 中的 UserServiceImpl 就可以實現 UserService接口
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>dubbo</artifactId> 4 <version>2.5.3</version> 5 </dependency>
還要有 zookeeper的 所以在1 的 pom.xml 中 有
1 <dependency> 2 <groupId>org.apache.zookeeper</groupId> 3 <artifactId>zookeeper</artifactId> 4 <version>3.3.3</version> 5 </dependency>
下面介紹一下項目1 中 的 ApplicationContent-dubbo.xml 和 ApplicationContent.xml
1 <!-- 啟動組件掃描,排除@Controller組件,該組件由SpringMVC配置文件掃描 --> 2 <context:component-scan base-package="com.cl.user.serviceImpl"/>
ApplicationContent-dubbo.xml dubbo服務的配置
1 <!-- 提供方應用信息,用於計算依賴關系 --> 2 <dubbo:application name="hehe_provider" /> 3 <!-- 使用zookeeper注冊中心暴露服務地址 端口是zookeeper 中配置的2181--> 4 <dubbo:registry address="zookeeper://127.0.0.1:2181"/> 5 <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> 6 <!-- 用dubbo協議在20880端口暴露服務 --> 7 <dubbo:protocol name="dubbo" port="20880" /> 8 <!-- 具體的實現bean --> 9 <bean id="userService" class="com.cl.user.serviceImpl.UserServiceImpl" /> 10 <!-- 聲明需要暴露的服務接口 --> 11 <dubbo:service interface="com.cl.user.servicei.UserService" ref="userService" />
主意:有可能你的配置文件中不識別 <dubbo:> 去網上找解決的辦法
到這里我們的 “提供者” 即 服務 已經開發完了,那么如何啟動服務呢? 在項目1中有個start.java

1 package com.test;
2
3 import org.apache.log4j.PropertyConfigurator;
4
5 public class start {
6 static{
7 PropertyConfigurator.configure("src/main/resources/log4j.properties");
8 }
9 public static void main(String[] args) {
10
11 com.alibaba.dubbo.container.Main.main(args);
12 }
13 }
主意里面有main方法,可以直接運行啟動了。別急!!! 之前還有個問題就是為什 ApplicationContent-dubbo.xml 和 ApplicationContent.xml 這兩個配置文件 必須要放到 /META-INF/spring/下面
因為 com.alibaba.dubbo.container.Main.main(args) 默認就會去加載 /META-INF/spring/ 下的配置文件
我們來看一下源碼

@Controller
public class UserController {
@Resource(name="userService")
private UserService userService;
@RequestMapping("/hello/test/world")
public void sayHello(){
System.out.println(userService.sayHello()+"**************************");
}
}
ApplicationContext-mvc.xml 都是springmvc的配置 如下不解釋了
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com" />
<!-- 對靜態資源文件的訪問 restful-->
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/lib/**" location="/lib/" />
<mvc:resources mapping="/plugins/**" location="/plugins/" />
<mvc:resources mapping="/uploadFiles/**" location="/uploadFiles/" />
<mvc:resources mapping="/WEB-INF/html/**" location="/WEB-INF/html/" />
<!-- 配置SpringMVC的視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/html"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 上傳攔截,如最大上傳值及最小上傳值 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
</bean>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 -->
<dubbo:application name="hehe_consumer" />
<!-- 使用zookeeper注冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 組播注冊 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 生成遠程服務代理,可以像使用本地bean一樣使用userService -->
<dubbo:reference id="userService" interface="com.cl.user.servicei.UserService" />
</beans>
開發完消費者后 ,啟動消費者,可以在管控台看到


<!-- 組播注冊 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->

