上一篇介紹了Dubbo簡介和實(shí)現(xiàn)原理。 Dubbo 簡介
本文主要介紹采用Dubbo與Zookeeper、Spring框架的整合。
整合流程:
- 安裝Zookeeper,啟動(dòng);
- 創(chuàng)建MAVEN項(xiàng)目,構(gòu)建Dubbo+Zookeeper+Spring實(shí)現(xiàn)的簡單demo;
- 安裝Dubbo-admin,實(shí)現(xiàn)監(jiān)控。
一、Zookeeper安裝
本demo中的Dubbo注冊中心采用的是Zookeeper.
具體的安裝方法,可參考文章:Windows環(huán)境下Zookeeper安裝和使用
二、新建Spring Maven工程
項(xiàng)目所需環(huán)境:
- jdk1.8
- apache-tomcat-8.5.31
- 開發(fā)工具(IntelliJ IDEA 2016.3)
-
apache maven 3.3.9
項(xiàng)目結(jié)構(gòu):
dubbo-api : 存放公共接口;
dubbo-consumer、dubbo-consumer2 : 調(diào)用遠(yuǎn)程服務(wù);
dubbo-provider : 提供遠(yuǎn)程服務(wù)。
image.png
代碼構(gòu)建過程:
- 構(gòu)建MAVEN項(xiàng)目,導(dǎo)入所需要的jar包依賴(spring, dubbo, zookeeper等jar)。
-
創(chuàng)建dubbo-api的MAVEN項(xiàng)目(有獨(dú)立的pom.xml,用來打包供提供者消費(fèi)者使用)。
在項(xiàng)目中定義服務(wù)接口:該接口需單獨(dú)打包,在服務(wù)提供方和消費(fèi)方共享。
image.png
接口:DemoService
package com.alibaba.dubbo.demo;
import java.util.List;
public interface DemoService {
List<String> getPermissions(Long id);
}
pom.xml無需修改。
3)創(chuàng)建dubbo-provider的MAVEN項(xiàng)目(有獨(dú)立的pom.xml,用來打包供消費(fèi)者使用)。 實(shí)現(xiàn)公共接口,此實(shí)現(xiàn)對消費(fèi)者隱藏
DemoServiceImpl實(shí)現(xiàn)類:
package com.alibaba.dubbo.demo.impl;
import com.alibaba.dubbo.demo.DemoService;
import java.util.ArrayList;
import java.util.List;
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;
}
}
pom.xml中需加入公共接口所在的依賴 :
<dependency>
<groupId>DubboDemo</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
在main/rescourse下新增 provider.xml配置聲明暴露服務(wù)
<?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">
<!--定義了提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系;在 dubbo-admin 或 dubbo-monitor 會(huì)顯示這個(gè)名字,方便辨識(shí)-->
<dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
<!--使用 zookeeper 注冊中心暴露服務(wù),注意要先開啟 zookeeper-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol name="dubbo" port="20880" />
<!--使用 dubbo 協(xié)議實(shí)現(xiàn)定義好的 api.PermissionService 接口-->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" />
<!--具體實(shí)現(xiàn)該接口的 bean-->
<bean id="demoService" class="com.alibaba.dubbo.demo.impl.DemoServiceImpl"/>
</beans>
在impl包下,新增啟動(dòng)服務(wù)的main方法
package com.alibaba.dubbo.demo.impl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
System.out.println(context.getDisplayName() + ": here");
context.start();
System.out.println("服務(wù)已經(jīng)啟動(dòng)...");
System.in.read();
}
}
-
創(chuàng)建dubbo-consumer的MAVEN項(xiàng)目(可以有多個(gè)consumer,但是需要配置好)。 調(diào)用所需要的遠(yuǎn)程服務(wù)。
image.png
在main/rescourse下新增 consumer .xml配置引用遠(yuǎn)程服務(wù)
<?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 定時(shí)推送-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用 dubbo 協(xié)議調(diào)用定義好的 api.PermissionService 接口-->
<dubbo:reference id="permissionService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
在impl包下,新增調(diào)用遠(yuǎn)程服務(wù)的main方法
package com.alibaba.dubbo.consumer;
import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) {
//測試常規(guī)服務(wù)
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("consumer.xml");
context.start();
System.out.println("consumer start");
DemoService demoService = context.getBean(DemoService.class);
System.out.println("consumer");
System.out.println(demoService.getPermissions(1L));
}
}
5)運(yùn)行項(xiàng)目
先確保Zookeeper已成功啟動(dòng)
進(jìn)入dubbo-provider工程下,右鍵運(yùn)行Provider.class,啟動(dòng)provider服務(wù)。
再進(jìn)入dubbo-consumer工程下,右鍵運(yùn)行Consumer.class,啟動(dòng)consumer模塊。
三、部署dubbo-admin管理控制臺(tái)
請參考文章:dubbo-admin 監(jiān)控搭建