配置好jdk1.7、Zookeeper和Maven環境之后,開始嘗試第一次搭建簡單的dubbo生產者和消費者。
dubbo服務的原理,引用經典的官方圖(cr.Dubbo官網):

關於Dubbo的原理和機制,在此不做贅述,具體可以查詢官方文檔:http://dubbo.apache.org/#!/?lang=zh-cn。
接下來開始搭建生產者和消費者。
1.生產者(Provider)
創建一個maven項目,



代碼如下:
(1)接口ProviderService.java
package com.mohrss.service;
public interface ProviderService {
public void sayHello();
}
(2)實現類ProviderServiceImpl.java
package com.mohrss.service.impl;
import com.mohrss.service.ProviderService;
import org.springframework.stereotype.Service;
@Service("providerService")
public class ProviderServiceImpl implements ProviderService {
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Hello I am Provider");
}
}
(3)程序入口TestProviderService.java
package com.mohrss.service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestProviderService {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"});
context.start();
System.out.println("生產者服務已經注冊成功!");
try {
System.in.read();//讓此程序一直跑,表示一直提供服務
} catch (Exception e) {
e.printStackTrace();
}
}
}
(4)dubbo配置文件dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<!--用注解方式實現bean-->
<context:component-scan base-package="com.mohrss.service">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!-- 提供方應用信息,用於計算依賴關系 -->
<dubbo:application name="provider" />
<!-- 使用zookeeper注冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="29014" />
<!-- 聲明需要暴露的服務接口 -->
<dubbo:service interface="com.mohrss.service.ProviderService" ref="providerService" />
</beans>
(5)依賴文件配置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.mohrss</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>4.3.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
2.消費者(Consumer)
消費者搭建和生產者相同的maven項目

(1)TestConsumerService.java
package com.mohrss.consumer;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mohrss.service.ProviderService;
public class TestConsumerService {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer.xml"});
context.start();
ProviderService testService = (ProviderService) context.getBean("testProviderService");
testService.sayHello();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2)dubbo-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="consumer" />
<!-- 使用multicast廣播注冊中心暴露發現服務地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
<dubbo:reference id="testProviderService" interface="com.mohrss.service.ProviderService" />
</beans>
3.啟動zookeeper,右鍵啟動TestProviderService.java,將看到生產者成功注冊:

然后右鍵TestConsumerService.java, Run as Application,將看到:

注意:消費者調用服務方接口,應當將provider編譯成jar包放到消費者中進行引用,此處為了方便,直接引用了生產者的接口文件ProviderService.java
注:原創博客,轉載請注明。
