Dubbo + Zookeeper入门初探
生活随笔
收集整理的這篇文章主要介紹了
Dubbo + Zookeeper入门初探
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2018年2月15日,阿里巴巴的dubbo進入了Apache孵化器,社區的加入,希望dubbo能變得更好…Registry:注冊中心,相當于房產中介,服務提供者和使用者都需要在這里注冊/使用服務,
我使用 zookeeper 實現。Monitor:監控中心,相當于房產局,它可以統計服務提供者和服務使用者的一些信息,及他們之間的關系,
我使用 dubbo admin 實現。Provider:服務提供者,相當于房東,提供服務。Consumer:服務消費者,想當于租戶,使用服務。通俗的解釋下 dubbo 的整個流程,將服務比喻成房子:
start:dubbo 一啟動,房東想好自己準備要租出去的房子
register:房東將房子拿到房產中介那邊進行登記,并留下自己的聯系方式
subscribe:租戶告訴房產中介自己想租一個什么樣的房子
notify:房產中介回復給租戶符合條件的房子的房東的聯系方式
invoke:租戶拿著聯系方式去找房東租房子
count:房產局全程監控著房東和租戶之間的交易start、register、subscribe 在 dubbo 服務一啟動就完成了
notify、count 是異步執行的
invoke 是同步執行的四、配置項目
<properties><dubbo.version>2.6.1</dubbo.version><zookeeper.version>3.5.2-alpha</zookeeper.version><curator.version>4.0.1</curator.version>
</properties><!-- dubbo包 -->
<dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><!-- 排除dubbo自帶的spring和netty,使用項目的,如果本身項目沒有,無需排除 --><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion><exclusion><groupId>org.jboss.netty</groupId><artifactId>netty</artifactId></exclusion></exclusions>
</dependency>
<!-- zookeeper包 -->
<dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><type>pom</type>
</dependency>
<!-- curator(zookeeper的客戶端)包 -->
<dependency><groupId>org.apache.curator</groupId><artifactId>curator-client</artifactId>
</dependency>
<dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId>
</dependency>還需要在相關配置文件加上 dubbo 的 bean 的頭部約束,將下面的添加到 bean 頭部即可:
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd4.1 服務提供方代碼
import org.springframework.stereotype.Service;
@Service
public class TbItemServiceImpl extends ServiceImpl<TbItemMapper, TbItem> implements TbItemService {}需要修改 spring 關于 service 的配置文件,加入 dubbo 的配置信息:
<?xml version="1.0" encoding="UTF-8"?>
<beanshttp://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd><!-- 掃描service層注解 --><context:component-scan base-package="jit.wxs.service"/><!-- dubbo發布服務 --><!-- 提供方應用信息,用于計算依賴關系 --><dubbo:application name="e3-manager" /><!-- 配置zookeeper的地址,集群地址用逗號隔開 --><dubbo:registry protocol="zookeeper" address="192.168.30.145:2181" /><!-- 用dubbo協議在20880端口暴露服務 --><dubbo:protocol name="dubbo" port="20880" /><!-- 聲明需要暴露的服務接口ref:為注入的對應接口的beantimneout:超時時間,單位ms,開發模式可以設長一點方便debug--><dubbo:service interface="jit.wxs.service.TbItemService"
ref="tbItemServiceImpl" timeout="600000"/>
</beans>dubbo:application:提供方的應用名
dubbo:registry:注冊中心的類型和地址
dubbo:protocol:這個服務要暴露在哪個端口上(使用方根據這個端口使用服務)
dubbo:service:設置暴露的服務的接口,ref 為該接口的 bean,timeout 為超時時間4.2 服務使用方代碼
服務使用方,我使用 Spring MVC 來實現,修改 Spring MVC 的配置文件,加入 dubbo 的配置:
<?xml version="1.0" encoding="UTF-8"?><beans http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
><!-- 掃描組件 --><context:component-scan base-package="jit.wxs.web"/><!-- 注解驅動 --><mvc:annotation-driven /><!-- 全局異常類 --><!--<bean class="cn.edu.jit.exception.GlobalExceptionResolver"/>--><!-- 視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 引用dubbo服務 --><!-- 使用方應用信息,用于計算依賴關系 --><dubbo:application name="e3-manager-web"/><!-- 指定zookeeper的地址,集群用逗號分隔 --><dubbo:registry protocol="zookeeper" address="192.168.30.145:2181"/><!-- 申明要訪問的接口,并創建代理對象,注入bean,名為id的值 --><dubbo:reference interface="jit.wxs.service.TbItemService" id="tbItemService" />
</beans>
dubbo:application: 使用方的應用名
dubbo:registry:注冊中心的類型和地址
dubbo:reference:要使用的服務的接口,并將返回的注入 bean,名稱為id設的值如果配置沒有問題的話,現在使用方已經能夠使用提供方提供的服務了,直接將 tbItemService
注入進來即可:@RestController
@RequestMapping("/items")
public class TbItemController {@Autowiredprivate TbItemService tbItemService;@GetMapping("/{id}")public TbItem getItemById(@PathVariable Long id) {TbItem item = null;if(id != null) {item = tbItemService.selectById(id);}return item;}
}
dubbo和zookeeper項目中使用
服務的提供者將服務注冊到注冊中心,服務的消費者從注冊中心獲取服務,monitor監控服務的調用。 框架無非就是配置文件+java代碼,所以dubbo也同理: (1)首先看下B服務的提供者的配置文件和代碼: 搭建b系統: 建立一個maven的war工程,導入依賴<dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.3.3</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version> </dependency> <dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version><exclusions><exclusion><!-- 排除傳遞spring依賴 --><artifactId>spring</artifactId><groupId>org.springframework</groupId></exclusion></exclusions> </dependency>第二步:創建user對象:注意這里一定要實現序列化,并獲取序列化的序號: // 使用dubbo要求傳輸的對象必須實現序列化接口 public class User implements java.io.Serializable創建服務接口和服務接口的實現類:接口為了暴露服務的: public interface UserService { /*** 查詢所有的用戶數據** @return*/public List<User> queryAll(); }5.3.6. 創建UserServiceImpl實現類 public class UserServiceImpl implements UserService {/*** 實現查詢,這里做模擬實現,不做具體的數據庫查詢*/public List<User> queryAll() {List<User> list = new ArrayList<User>();for (int i = 0; i < 10; i++) {User user = new User();user.setAge(10 + i);user.setId(Long.valueOf(i + 1));user.setPassword("123456");user.setUsername("username_" + i);list.add(user);}return list;}}下面就是dubbo的服務提供者的配置文件了: 實際項目中,相當于服務的提供者的第一步:編寫配置文件 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" <!-- 提供方應用信息,用于計算依賴關系 --> <dubbo:application name="dubbo-b-server" /> <!-- 這里使用的注冊中心是zookeeper --> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 將該接口暴露到dubbo中 --> <dubbo:service interface="cn.itcast.service.UserService" ref="userServiceImpl" /> <!-- 將具體的實現類加入到Spring容器中 --> <bean id="userServiceImpl" class="cn.dubbo.service.impl.UserServiceImpl" /><br>這里spring與dubbo進行了無縫整合,所以這里進行了spring與dubbo的整合:第二步:讀取配置文件 我們需要在web.xml中將配置文件引入:<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:dubbo/dubbo-*.xml</param-value></context-param><!--Spring的ApplicationContext 載入 --> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>第三步:配置服務消費者: 這里我們建立a的系統: 建立一個maven的jar工程: 導入依賴:<dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version><exclusions><exclusion><!-- 排除傳遞spring依賴 --><artifactId>spring</artifactId><groupId>org.springframework</groupId></exclusion></exclusions></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.3.3</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency></dependencies>從b系統拷貝: 1.1.1. 從b系統中拷貝User對象、UserService接口道a系統 服務的消費者配置: dubbo-consumer.xml<!-- 提供方應用信息,用于計算依賴關系 --><dubbo:application name="dubbo-a-consumer" /><!-- 這里使用的注冊中心是zookeeper --><dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/><!-- 從注冊中心中查找服務 --><dubbo:reference id="userService" interface="cn.itcast.dubbo.service.UserService"/>(1)導入dubbo、zookeeper依賴 (2)在服務提供者端,編寫服務接口,服務接口的實現類,編寫配置文件 (3)修改web.xml讀取配置文件 (4)在服務消費者,即客戶端,調用服務接口,調用服務實現類,編寫配置文件dubbo與zookeeper的關系
Dubbo建議使用Zookeeper作為服務的注冊中心。 1. Zookeeper的作用:zookeeper用來注冊服務和進行負載均衡,哪一個服務由哪一個機器來提供必需讓調用者知道,簡單來說 就是ip地址和服務名稱的對應關系。 zookeeper通過心跳機制可以檢測掛掉的機器并將掛掉機器的ip和服務 對應關系從列表中刪除。至于支持高并發,簡單來說就是橫向擴展,在不更改代碼 的情況通過添加機器來 提高運算能力。通過添加新的機器向zookeeper注冊服務,服務的提供者多了能服務的客戶就多了。2. dubbo:是管理中間層的工具,在業務層到數據倉庫間有非常多服務的接入和服務提供者需要調度,dubbo提供一個 框架解決這個問題。 這個框架中要完成調度必須要有一個分布式的注冊中心,儲存所有服務的元數據,你可以用zk,也可以用別的, 只是大家都用zk。3. zookeeper和dubbo的關系: Dubbo的將注冊中心進行抽象,是得它可以外接不同的存儲媒介給注冊中心提供服務, 有ZooKeeper,Memcached,Redis等。 引入了ZooKeeper作為存儲媒介,也就把ZooKeeper的特性引進來。首先是負載均衡,單注冊中心的承載能力 是有限的,在流量達到一定程度的時 候就需要分流,負載均衡就是為了分流而存在的,一個ZooKeeper群配合 相應的Web應用就可以很容易達到負載均衡;資源同步,單單有負載均衡還不 夠,節點之間的數據和資源需要 同步,ZooKeeper集群就天然具備有這樣的功能;Dubbo(Dubbo與Zookeeper、SpringMVC整合)
第一步:在Linux上安裝Zookeeper Zookeeper作為Dubbo服務的注冊中心,Dubbo原先基于數據庫的注冊中心,沒采用Zookeeper,Zookeeper 一個分布式的服務框架,是樹型的目錄服務的數據存儲,能做到集群管理數據 ,這里能很好的作為Dubbo服務 的注冊中心,Dubbo能與Zookeeper做到集群部署,當提供者出現斷電等異常停機時,Zookeeper注冊中心能 自動刪除提供者信息,當提供者重啟時,能自動恢復注冊數據,以及訂閱請求。(1)下載Zookeeper-3.4.6.tar.gz 地址http://www.apache.org/dist/zookeeper/ (2) 我們放到Linux下的一個文件夾,然后解壓: tar zxvf zookeeper-3.4.6.tar.gzclientPort:監聽客戶端連接的端口。tickTime:基本事件單元,以毫秒為單位。它用來控制心跳和超時,默認情況下最小的會話超時時間為 兩倍的 tickTime。第二步:配置dubbo-admin的管理頁面,方便我們管理頁面(1)下載dubbo-admin-2.4.1.war包,在Linux的tomcat部署,先把dubbo-admin-2.4.1放在 tomcat的webapps/ROOT下,然后進行解壓: jar -xvf dubbo-admin-2.4.1.war (2)然后到webapps/ROOT/WEB-INF下,有一個dubbo.properties文件,里面指向Zookeeper ,使用的 是 Zookeeper 的注冊中心,如圖所示:(3)然后啟動tomcat服務,用戶名和密碼:root,并訪問服務,顯示登陸頁面,說明dubbo-admin 部署成功, 如圖所示:第三步:SpringMVC與Dubbo的整合,這邊使用的Maven的管理項目第一:我們先開發服務注冊的,就是提供服務,項目結構如圖所示:(1)test-maven-api項目加入了一個服務接口,代碼如下: public interface TestRegistryService { public String hello(String name); }(2)test-maven-console在pom.xml加入Dubbo和Zookeeper的jar包、引用test-maven-api的jar包(3)test-maven-console實現具體的服務,代碼如下: @Service("testRegistryService") public class TestRegistryServiceImpl implements TestRegistryService { public String hello(String name) { return "hello"+name; } }(4)我們服務以及實現好了,這時要暴露服務,代碼如下:<!-- 提供方應用名稱信息,這個相當于起一個名字,我們dubbo管理頁面比較清晰是哪個應用 暴露出來的 --> <dubbo:application name="dubbo_provider"></dubbo:application> <!-- 使用zookeeper注冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry> <!-- 要暴露的服務接口 --> <dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" /> dubbo:registry 標簽一些屬性的說明:1)register是否向此注冊中心注冊服務,如果設為false,將只訂閱,不注冊。2)check注冊中心不存在時,是否報錯。3)subscribe是否向此注冊中心訂閱服務,如果設為false,將只注冊,不訂閱。4)timeout注冊中心請求超時時間(毫秒)。5)address可以Zookeeper集群配置,地址可以多個以逗號隔開等。dubbo:service標簽的一些屬性說明: 1)interface服務接口的路徑 2)ref引用對應的實現類的Bean的ID 4)register 默認true ,該協議的服務是否注冊到注冊中心。 (5)啟動項目,然后我們在Dubbo管理頁面上顯示,已經暴露的服務,但顯示還沒有消費者,因為我們還沒 實現消費者服務,如圖所示:第二:我們在開發服務消費者,就是調用服務,我們在新建一個新的消費者項目結構如圖所示: 1)test-maven-server-console的pom.xml引入Dubbo和Zookeeper的jar包、test-maven-api的jar包, 因為引入test-maven-api的jar包,我們在項目中調用像在本地調用一樣。(2)test-maven-server-console項目的具體實現,代碼如下:@Controller public class IndexController { @Autowired private TestRegistryService testRegistryService; @RequestMapping("/hello") public String index(Model model){ String name=testRegistryService.hello("zz"); System.out.println("xx=="+name); return ""; } } (3)我們要引用的地址,代碼如下:<dubbo:application name="dubbo_consumer"></dubbo:application> <!-- 使用zookeeper注冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://192.168.74.129:2181" check="false"></dubbo:registry> <!-- 要引用的服務 --> <dubbo:reference interface="cn.test.dubbo.registry.service. TestRegistryService" id="testRegistryService"></dubbo:reference> 說明:dubbo:reference 的一些屬性的說明:1)interface調用的服務接口2)check 啟動時檢查提供者是否存在,true報錯,false忽略3)registry 從指定注冊中心注冊獲取服務列表,在多個注冊中心時使用,值為<dubbo:registry>的 id屬性,多個注冊中心ID用逗號分隔4)loadbalance 負載均衡策略,可選值:random,roundrobin,leastactive,分別表示:隨機,輪循,最少活躍調用(4)項目啟動,Dubbo管理頁面,能看到消費者,(5)然后訪問消費者項目,Controller層能像調用本地一樣調用服務的具體實現?
總結
以上是生活随笔為你收集整理的Dubbo + Zookeeper入门初探的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZooKeeper快速入门
- 下一篇: Rocketmq原理最佳实践