IDEA Dubbo+ZooKeeper+SpringMVC 多模組小程式
本文主要用了Dubbo+ZooKeeper+SpringMVC做的一個小程式,閱讀請確保您的dubbo和zookperr的服務已經搭建好
開始:新建一個空的MAVEN專案
點選完成,建立最外層工程,該工程並無實際意義,只是為了裝載其他工程
新建Controller工程,同樣也是Maven專案,這個選擇模板
新建Service工程,這裡的MAVEN不用選擇模板,空建立就好
同理,建立Interfenct工程,MAVEN也不用選擇模板,空建立就好,命名自己看著合適就行,下面給出工程暫時結構
Controller 裡面在Main裡面建個JAVA資料夾,設定成SourcesRoot,不然不能夠建立包和類,然後在資原始檔夾下建立xml檔案
建立介面MAVEN工程
建立服務MAVEN工程,同Controller
介面的一個小方法就不給程式碼了
新增Service專案裡面的MAVEN依賴項,這裡先是依賴一下介面工程,因為要實現介面工程裡面方法
然後在Service裡面實現介面工程裡面的方法
然後在Service的mavan配置檔案新增dubbo和zookeeper的jar包,給出依賴的程式碼
?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parentDemo</artifactId>
<groupId>com.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion >4.0.0</modelVersion>
<artifactId>MyService</artifactId>
<dependencies>
<dependency>
<groupId>com.parent</groupId>
<artifactId>MyInterfence</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
Conntroller工程maven配置檔案的依賴
<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">
<parent>
<artifactId>parentDemo</artifactId>
<groupId>com.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>MyController</artifactId>
<packaging>war</packaging>
<name>MyController 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>
<dependency>
<groupId>com.parent</groupId>
<artifactId>MyInterfence</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<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>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<finalName>MyController</finalName>
</build>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
在服務工程新建一類,主要用來載入配置檔案,開啟提供者服務,這裡zookeeper的服務要開啟
註冊提供者,在配置檔案中進行註冊
<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.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-3.1.xsd"
default-lazy-init="false" >
<bean id="myInterfence" class="com.start.service.MyInterfenceImpl"></bean>
<dubbo:application name="dubbo_provider"></dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" ></dubbo:registry>
<dubbo:service interface="com.start.interfence.IMyInterfence" ref="myInterfence" />
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
登入dubbo檢視提供者,可以看到已經配好了,這個頁面即之前下載過的dubbo.war放到Tomcat webAPP目錄下開啟的頁面
在Controller的xml配置檔案中,配置SpringMVC註解,檢視解析器,dubbo
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:component-scan base-package="com.start.controller"/>
<!-- 配置註解驅動 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!-- 檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<dubbo:application name="dubbo_consumer"></dubbo:application>
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"></dubbo:registry>
<!-- 要引用的服務 -->
<dubbo:reference interface="com.start.interfence.IMyInterfence" id="myInterfence"></dubbo:reference>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
在Controller工程裡的web.xml新增配置資訊,幾個xml檔案不要弄混了,MAVAWEN的,dubbo的,這還一個Controller工程的web.xml的
然後在Controller開始我們的編碼
package com.start.controller;
import com.start.interfence.IMyInterfence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by 123 on 2017/3/14.
*/
@Controller
public class MyController {
@Autowired
private IMyInterfence myInterfence;
@RequestMapping("/test.do")
public String getTest() {
System.out.print("11111111111");
String str = myInterfence.helloWolrd();
System.out.print(str);
return "welcome";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
配置Tomcat,然後啟動
這裡報錯了,因為我在後臺開了一個Tomcat了,埠號為8080,開著的目的,主要是開啟dubbo的admin頁面,檢視提供者和消費者的
把埠號換一下,重新執行
開啟介面,輸入配置的路徑,然後輸入請求路徑,成功
控制檯也打印出相關資訊
登入到dubbo的admin頁面,發現消費者也出來了
至此一個簡單的小專案就完成了。
注意,在寫提供者那一塊,Zookeeper的服務就要開啟,放那不用管它,Tomcat也可以本地先啟動,為的是能夠開啟dubbo的admin,當然,你在Tomcat本地上先要把dubbo.war包解壓後放到webapp目錄下,也可以解壓替換root資料夾,這樣一開啟tomcat首頁就是dubbo-admin頁面,注意後來釋出專案的時候,Tomcat的埠號不要再用8080了,容易衝突。
謝謝觀賞!