dubbo-簡單Demo的實現(轉+原創)
阿新 • • 發佈:2019-02-11
專案結構搭建
- 新建maven聚合專案,DubboDemo,是pom型別。
- 在DubboDemo專案右鍵新建一個module,dubbo-api,是war型別。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
新建完,專案報錯。
點選專案,右鍵,Properties,找到Project Facets,並把Dynamic Web Module的勾去掉,並Apply應用。
然後再選擇Dynamic Web Module版本為2.5並勾選上,底下會出現,Further configuration available,點選進入。
在dubbo-api底下輸入框輸入/src/main/webapp
,並點選OK,然後再次點選OK,這樣就自動生成了web-inf目錄和web.xml檔案。
(還有一種簡單的方法就是在
/src/main/webapp
目錄下手動新建web-inf目錄和web.xml檔案)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 在點選DubboDemo專案右鍵再新建一個module,dubbo-provider,是jar型別。
- 同理在DubboDemo新建一個module,叫dubbo-consumer,也是jar型別
- 專案目錄新建完畢,如下圖所示。
建立檔案
- 開啟DubboDemo專案的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my</groupId>
<artifactId>DubboDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging >
<modules>
<module>dubbo-api</module>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- spring版本號 -->
<spring.version>4.2.5.RELEASE</spring.version>
<!-- log4j日誌包版本號 -->
<slf4j.version>1.7.18</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- 新增spring核心依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring 結束 -->
<!-- 新增日誌相關jar包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- zeekeeper註冊中心 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!-- dubbo -->
<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>
</dependencies>
</project>
- 在dubbo-api專案下,新建目錄
com.alibaba.dubbo.demo
,並建立一個介面檔案DemoService,程式碼如下:
package com.alibaba.dubbo.demo;
import java.util.List;
public interface DemoService {
List<String> getPermissions(Long id);
}
- 打包dubbo-api,專案右鍵,run as =>maven install。
- 在pom.xml檔案中為dubbo-provider專案新增dubbo-api的依賴,程式碼如下:
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.my</groupId>
<artifactId>DubboDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.my</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 在dubbo-provider專案下,新建目錄
com.alibaba.dubbo.demo.impl
,並建立class檔名為DemoServiceImpl,實現DemoService介面,程式碼如下:
package com.alibaba.dubbo.demo.impl;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
public List<String> getPermissions(Long id) {
List<String> demo = new ArrayList<String>();
demo.add(String.format("Permission_%d", id - 1));
demo.add(String.format("Permission_%d", id));
demo.add(String.format("Permission_%d", id + 1));
return demo;
}
}
- 再建立一個class檔案provider,程式碼如下:
package com.alibaba.dubbo.demo.impl;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
System.err.println(context.getDisplayName() + ": here");
context.start();
System.err.println("服務已經啟動...");
System.in.read();
}
}
- 在resource目錄下建立provider.xml檔案和日誌配置檔案log4j.xml,程式碼如下:
provider.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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--定義了提供方應用資訊,用於計算依賴關係;在 dubbo-admin 或 dubbo-monitor 會顯示這個名字,方便辨識-->
<dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
<!--使用 zookeeper 註冊中心暴露服務,注意要先開啟 zookeeper-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo協議在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--使用 dubbo 協議實現定義好的 api.PermissionService 介面-->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" />
<!--具體實現該介面的 bean-->
<bean id="demoService" class="com.alibaba.dubbo.demo.impl.DemoServiceImpl"/>
</beans>
log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</root>
</log4j:configuration>
- dubbo-provider的目錄結構如下:
- 在dubbo-consumer的pom.xml檔案中新增dubbo-api的依賴,程式碼如下:
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.my</groupId>
<artifactId>DubboDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.my</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 在dubbo-consumer專案下新建目錄
com.alibaba.dubbo.consumer
,並建立class檔案Consumer,程式碼如下:
package com.alibaba.dubbo.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.demo.DemoService;
public class Consumer {
public static void main(String[] args) {
//測試常規服務
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("consumer.xml");
context.start();
System.err.println("consumer start");
DemoService demoService = context.getBean(DemoService.class);
System.err.println("consumer");
System.out.println(demoService.getPermissions(1L));
}
}
- 在dubbo-consumer專案的resource目錄下,建立consumer.xml檔案和log4j.xml檔案,程式碼如下:
consumer.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.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/>
<!--向 zookeeper 訂閱 provider 的地址,由 zookeeper 定時推送-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用 dubbo 協議呼叫定義好的 api.PermissionService 介面-->
<dubbo:reference id="permissionService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</root>
</log4j:configuration>
- dubbo-consmuer的目錄結構如下:
- 專案搭建完畢
- 為專案新增dubbo.xsd檔案,Window=》Preferances
- 選擇之前下載的dubbo.xsd檔案,點OK完成。
- 最後統一下各個專案的jre版本和編譯的版本。
測試
- 開啟下載解壓的zookeeper目錄下的bin,並雙擊zkServer.cmd。
- 回到eclipse中的dubbo-provider專案,開啟provider.java檔案,右鍵run as =》java application
- 如果見到如下列印結果,表示成功。
- 然後開啟dubbo-consumer專案下的consumer.java檔案,右鍵run as =》java application
- 如果見到如下列印結果,表示成功。
在zkServer.cmd視窗能見到provider檔案和consumer檔案執行之後的一些連結資訊
如果在eclipse裡關閉執行的程序,在zkServer.cmd視窗也能看到關閉資訊。