1. 程式人生 > >Dubbo教程(三)----一個簡單的Dubbo示例

Dubbo教程(三)----一個簡單的Dubbo示例

一、建立Maven多模組專案

專案結構如下


模組介紹:

dubbo-api            ----API介面

dubbo-consumer ----消費者

dubbo-provider    ----生產者

dubbo-api  Service 提供的介面,provider 實現了 api 介面,並對外開放http埠,consumer依賴 api 工程,但是不會直接依賴provider工程。 這種可以讓消費者僅僅依賴api工程,而不需要對provider工程的依賴,也是鬆耦合的經典。

(1)建立root專案

首先建立root專案,把一些通用的依賴全部放置到這裡,步驟如下:




建立成功之後如下


把上面的src目錄全部刪除掉,只留下pom.xml


然後修改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.fendo</groupId>
  <artifactId>Dubbo_demos</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>dubbo</name>
  <description>dubbo示例</description>
  
  
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</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>
   
   
   
   <dependencyManagement>
      <dependencies>
	    
	    <!-- 新增junit4依賴 -->
	    <dependency>
	      <groupId>junit</groupId>
	      <artifactId>junit</artifactId>
	      <version>4.11</version>
	    </dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>

   
	    <!-- 新增日誌相關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>
    
	     <dependency>
	         <groupId>commons-logging</groupId>
	         <artifactId>commons-logging</artifactId>
	         <version>1.2</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>
    
    	<!-- Zookeeper zkclient-->
		<dependency>
		    <groupId>com.github.sgroschupf</groupId>
		    <artifactId>zkclient</artifactId>
		    <version>0.1</version>
		</dependency>
    
   </dependencies>
  </dependencyManagement>
  
  
</project>

(2)建立api專案

新建一個Module


輸入module名,選擇型別為java專案



點選"Finish",建立完成如下


他會自動在root目錄下把dubbo-api這麼module加入進來。而dubbo-api會自動繼承root


(3)建立provider(生產者)





點選"Finish"生成如下


修改POM.XML為如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  
  <parent>
    <artifactId>Dubbo_demos</artifactId>
    <groupId>com.fendo</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <groupId>com.fendo</groupId>
  <artifactId>dubbo-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>dubbo-provider</name>
  <url>http://maven.apache.org</url>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  
  <dependencies>
  
  		<dependency>
	    	<groupId>com.fendo</groupId>
	    	<artifactId>dubbo-api</artifactId>
	    	<version>0.0.1-SNAPSHOT</version>
        </dependency>
  		
  
		<dependency>
	      <groupId>junit</groupId>
	      <artifactId>junit</artifactId>
	    </dependency>

    	<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		
		
		<!-- 新增日誌相關jar包 -->
	    <dependency>
	      <groupId>log4j</groupId>
	      <artifactId>log4j</artifactId>
	    </dependency>
	    <dependency>
	      <groupId>org.slf4j</groupId>
	      <artifactId>slf4j-api</artifactId>
	    </dependency>
	    <dependency>
	      <groupId>org.slf4j</groupId>
	      <artifactId>slf4j-log4j12</artifactId>
	    </dependency>
    
	     <!-- dubbo -->
	    <dependency>
	      <groupId>com.alibaba</groupId>
	      <artifactId>dubbo</artifactId>
	      <exclusions>
	        <exclusion>
	          <groupId>org.springframework</groupId>
	          <artifactId>spring</artifactId>
	        </exclusion>
	      </exclusions>
	    </dependency>
    
    	<!-- Zookeeper zkclient-->
		<dependency>
		    <groupId>com.github.sgroschupf</groupId>
		    <artifactId>zkclient</artifactId>
		</dependency>
  </dependencies>
  
</project>




(4)建立consumer(消費者)




建立成如下


修改POM.XML為如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  
  
  <parent>
    <artifactId>Dubbo_demos</artifactId>
    <groupId>com.fendo</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <groupId>com.fendo</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>dubbo-consumer</name>
  <url>http://maven.apache.org</url>
  
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  
  <dependencies>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    
     <dependency>
    	<groupId>com.fendo</groupId>
    	<artifactId>dubbo-api</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
     </dependency>
    
    <dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
    </dependency>

	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
	</dependency>

    <!-- dubbo -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
   
   	<!-- Zookeeper zkclient-->
	<dependency>
	    <groupId>com.github.sgroschupf</groupId>
	    <artifactId>zkclient</artifactId>
	</dependency>
    
  </dependencies>
</project>


建立完成後,整體結構如下


這種結構看不起不是很美觀,把它修改成如下顯示,一個專案下包含多個子模組


二、定義介面API、bo物件、公共類及方法,這些是要提供方和呼叫方都需要的。

在dubbo-api模組中建立一個介面對外開發


三、定義Dubbo服務service,實現dubbo-api中的api介面,書寫業務邏輯及DB操作。通過spring暴露服務。

建立DemoService實現類DemoServiceImpl

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;
    }

   
}

以及消費者啟動類Provider
public class Provider {

    public static void main(String[] args) throws IOException{
    	
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
        System.out.println(context.getDisplayName() + ": here");
        context.start();
        System.out.println("服務已經啟動...");
        System.in.read();
    }
}


然後建立resources目錄


在下新建兩個xml檔案


provider.xml是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: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.fendo.dubbo.DemoService" ref="demoService" protocol="dubbo" />
    <!--具體實現該介面的 bean-->
    <bean id="demoService" class="org.dubbo.provider.impl.DemoServiceImpl"/>
</beans>

如果發現eclipse提示錯誤


解決方案,就是下載一個dubbo.xsd檔案然後

windows->preferrence->xml->xmlcatalog 



add->catalog entry  ->file system 選擇剛剛下載的檔案路徑



修改key值和配置檔案的http://code.alibabatech.com/schema/dubbo/dubbo.xsd 相同,點選OK之後就不會報錯了。。


四、定義呼叫端也叫消費端,實現對應的控制層。通過spring獲取服務。

和上面差不多


建立消費者執行類:

public class Consumer 
{
    public static void main(String[] args) {
        //測試常規服務
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        System.out.println("consumer start");
        DemoService demoService = (DemoService) context.getBean(DemoService.class);
        System.out.println("consumer");
        System.out.println(demoService.getPermissions(1L));
    }
}

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="org.dubbo.api.DemoService"/>
    
</beans>


五、使用zookeeper註冊中心。

執行zookeeper/bin目錄下的zkServer.cmd


啟動zookeeper

六、啟動Dubbo的service端,即provider。

啟動zookeeper之後,然後執行dubbo-provider中的Provider類


啟動成功之後,如上所示,然後在dubbo-admin控制檯會多出一個生產者


顯示還沒有消費者。。


七、啟動portal端,即所謂的consumer。

然後啟動dubbo-consumer中的Consumer類


會列印如上資訊,表示消費成功!!!