在本篇文章中我們將通過集體的示例來對(duì)Dubbo的提供和消費(fèi)進(jìn)行代碼層面的認(rèn)識(shí).這里所介紹的是基本的提供者和消費(fèi)者通過Spring容器來進(jìn)行相關(guān)的提供和消費(fèi)的服務(wù).首先看整個(gè)示例的項(xiàng)目結(jié)構(gòu)如下:
我們通過Maven的方式來進(jìn)行示例,其中dubbo-demo-api為提供者所定義的接口,項(xiàng)目結(jié)構(gòu)如下:
此項(xiàng)目里的內(nèi)容很簡單,為服務(wù)的一個(gè)接口DemoService:
/dubbo-demo/dubbo-demo-api/src/main/java/org/pactera/dubbo/DemoService.java
packageorg.pactera.dubbo;
/**
*
* @author Qing
*
*/
publicinterface DemoService {
public String sayHello(String str);
}
Dubbo-demo-provider為服務(wù)提供者的示例項(xiàng)目,結(jié)構(gòu)如下圖:
Dubbo-demo-consumer為服務(wù)消費(fèi)者的示例項(xiàng)目,結(jié)構(gòu)如下圖:
以上是整個(gè)示例程序的整體框架.
接下來我們將從提供者的項(xiàng)目入手開始.在提供者的項(xiàng)目里先要對(duì)相關(guān)提供的服務(wù)進(jìn)行配置.
1 在Dubbo-demo-provider的項(xiàng)目里我們需要用到Dubbo,Zookeeper,以及dubbo-demo-api的相關(guān)java包,因此需要在pom.xml對(duì)其進(jìn)行引用,如下:
<dependencies>
<dependency>
<groupId>org.pactera.dubbo</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
2 接下來為了能夠讓Dubbo的提供者通過Spring方式加載,我們需要對(duì)其進(jìn)行相關(guān)配置,如下文件內(nèi)容.
/dubbo-demo/dubbo-demo-provider/src/main/resources/dubbo-demo-provider.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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
">
<!-- 提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系 -->
<dubbo:applicationname="hello-world-app" />
<!-- 使用zookeeper廣播注冊(cè)中心暴露服務(wù)地址 -->
<dubbo:registryprotocol="zookeeper" address="127.0.0.1:2181" />
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol name="dubbo"port="20881" />
<dubbo:monitorprotocol="registry"></dubbo:monitor>
<!-- 聲明需要暴露的服務(wù)接口 -->
<dubbo:serviceinterface="org.pactera.dubbo.DemoService" ref="demoService"/>
<!-- 和本地bean一樣實(shí)現(xiàn)服務(wù) -->
<bean id="demoService"class="org.pactera.dubbo.provider.DemoServiceImpl" />
</beans>
3 需要將dubbo提供的服務(wù)接口進(jìn)行實(shí)現(xiàn).
/dubbo-demo/dubbo-demo-provider/src/main/java/org/pactera/dubbo/provider/DemoServiceImpl.java
packageorg.pactera.dubbo.provider;
importcom.alibaba.dubbo.rpc.RpcContext;
importlombok.extern.slf4j.Slf4j;
importorg.pactera.dubbo.DemoService;
@Slf4j
publicclass DemoServiceImpl implements DemoService {
public String sayHello(String str) {
System.out.println("reqInfo: "+ str + ", request form consumer: " +RpcContext.getContext().getRemoteHost());
return "Hello " + str;
}
}
4 接下來我們需要再寫一個(gè)提供者啟動(dòng)服務(wù)的程序,主要是用來啟動(dòng)Spring容器,并注入相關(guān)的服務(wù),同時(shí)將提供者的內(nèi)容注冊(cè)到Zookeeper中.
啟動(dòng)程序如下:
/dubbo-demo/dubbo-demo-provider/src/main/java/org/pactera/dubbo/Bootstrap.java
packageorg.pactera.dubbo;
importcom.google.common.util.concurrent.AbstractIdleService;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
@Slf4j
publicclass Bootstrap extends AbstractIdleService {
private ClassPathXmlApplicationContextcontext;
public static void main(String[] args) {
Bootstrap bootstrap = new Bootstrap();
bootstrap.startAsync();
try {
Object lock = new Object();
synchronized (lock) {
while (true) {
lock.wait();
}
}
} catch (InterruptedException ex) {
System.err.println("ignoreinterruption");
}
}
/**
* Start the service.
*/
@Override
protected void startUp() throws Exception {
context = newClassPathXmlApplicationContext("dubbo-demo-provider.xml");
context.start();
context.registerShutdownHook();
System.out.println("----------------provider service startedsuccessfully------------");
}
/**
* Stop the service.
*/
@Override
protected void shutDown() throws Exception{
context.stop();
System.out.println("-------------service stoppedsuccessfully-------------");
}
}
5 提供者服務(wù)啟動(dòng)日志:
至此,服務(wù)者Provider的示例基本上已經(jīng)結(jié)束,如果讀者配置監(jiān)控中心的話,可以打開監(jiān)控中心搜索demoService,你便可以在監(jiān)控中心看到此提供者服務(wù)的具體信息.限于篇幅的原因,關(guān)于消費(fèi)者的相關(guān)示例將在接下來的文章進(jìn)行闡述.
聯(lián)系客服