【微服務】Dubbo初體驗

1、前言

以前微服務這塊只用過SpringCloud搭建,可是最近面試會被問到dubbo框架,雖然以前也學了可是都忘了,故寫此博客加深印象。html

2、原理簡介

  Dubbo是一個分佈式服務框架,以及阿里巴巴內部的SOA服務化治理方案的核心框架。其功能主要包括:高性能NIO通信及多協議集成,服務動態尋址與路由,軟負載均衡與容錯,依賴分析與降級等。java

Dubbo核心部分包含:

  1. 遠程通信: 提供對多種基於長鏈接的NIO框架抽象封裝,包括多種線程模型,序列化,以及「請求-響應」模式的信息交換方式。
  2. 集羣容錯: 提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集羣支持。
  3. 自動發現: 基於註冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方能夠平滑增長或減小機器。

Dubbo能作什麼:

  1. 透明化的遠程調用,就像調用本地方法同樣調用遠程方法,只須要簡單配置,沒有任何侵入。
  2. 軟負載均衡及容錯機制,可在內網替代F5等硬件負載均衡器,下降成本,減小單點。
  3. 服務自動註冊與發現,再也不須要寫死服務提供放地址,註冊中心基於接口名查詢服務提供者的IP地址,而且可以平滑添加或刪除服務提供者。

Dubbo的架構圖以下:

 

架構圖
架構圖

 

節點說明:
1.Provider
暴露服務的生產者,向註冊中心註冊其提供的服務,並彙報調用時間到監控中心,此時間不包含網絡開銷。git

2.Container
服務運行容器github

3.Consumer
調用遠程服務的服務消費者,服務消費者向註冊中心獲取服務提供者地址列表 , 並根據負載算法直接調用提供者,同時彙報調用時間到監控中心,此時間包含網絡開銷。面試

4.Monitor
統計服務的調用次調和調用時間的監控中心,統計先在內存彙總後每分鐘一次發送到監控中心服務器,並以報表展現。算法

5.Registry
註冊中心負責服務地址的註冊與查找,至關於目錄服務,服務提供者和消費者只在啓動時與註冊中心交互,註冊中心不轉發請求,壓力較小 。spring

dubbo不一樣服務間的區別與聯繫:
(1) 註冊中心,服務提供者,服務消費者三者之間均爲長鏈接,監控中心除外
(2) 註冊中心經過長鏈接感知服務提供者的存在,服務提供者宕機,註冊中心將當即推送事件通知消費者
(3) 註冊中心和監控中心所有宕機,不影響已運行的提供者和消費者,消費者在本地緩存了提供者列表
(4) 註冊中心和監控中心都是可選的,服務消費者能夠直連服務提供者數據庫

3、傳統maven項目整合

3.1搭建註冊中心zookeeper

  1. 解壓 tar -zxf zookeeper-3.4.13.tar.gz
  2. 將conf目錄下的zoo_sample.cfg重命名爲zoo.cfg
  3. 進入bin目錄啓動,./zkServer.sh start ../conf/zoo.cfg

3.2參考這裏

https://www.jianshu.com/p/302001c1c21fapache

4、SpringBoot整合Dubbo

4.1建立公共依賴m-common

裏面就只包括實體類User和接口UserService
enter description here緩存

User.java

  1. // 使用dubbo要求傳輸的對象必須實現序列化接口 
  2. public class User implements Serializable
  3.  
  4. private Long id; 
  5.  
  6. private String username; 
  7.  
  8. private String password; 
  9.  
  10. public Long getId()
  11. return id; 
  12.  
  13. public void setId(Long id)
  14. this.id = id; 
  15.  
  16. public String getUsername()
  17. return username; 
  18.  
  19. public void setUsername(String username)
  20. this.username = username; 
  21.  
  22. public String getPassword()
  23. return password; 
  24.  
  25. public void setPassword(String password)
  26. this.password = password; 
  27.  
  28. @Override 
  29. public String toString()
  30. return "User{"
  31. "id=" + id + 
  32. ", username='" + username + '\''
  33. ", password='" + password + '\''
  34. '}'

UserService.java

public interface UserService {

    /** * 查詢全部用戶 * @return */
    public List<User> queryAll();
}

4.2建立服務生產者

項目結構如圖:
enter description here

  1. 在pom.xml文件引入依賴
<dependency>
            <groupId>cn.sp</groupId>
            <artifactId>m-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
  1. 在application.properties添加配置
dubbo.application.name=dubbo-producer
# 註冊中心地址
dubbo.registry.address=zookeeper://ip:2181
# 指定通訊協議
dubbo.protocol.name=dubbo
# 通訊端口 這裏指的是與消費者間的通訊協議與端口
dubbo.protocol.port=12345
  1. 在啓動類添加註解 @EnableDubbo來啓用dubbo
  2. 編寫UserService的實現類
  1. package cn.sp.service.impl; 
  2.  
  3. import cn.sp.bean.User; 
  4. import cn.sp.service.UserService; 
  5. import com.alibaba.dubbo.config.annotation.Service; 
  6. import org.springframework.stereotype.Component; 
  7.  
  8. import java.util.ArrayList; 
  9. import java.util.List; 
  10.  
  11. /** 
  12. * Created by 2YSP on 2019/5/12. 
  13. */ 
  14. @Component 
  15. @Service(interfaceClass = UserService.class) 
  16. public class UserServiceImpl implements UserService
  17.  
  18. @Override 
  19. public List<User> queryAll()
  20. // 模擬查詢數據庫 
  21. List<User> userList = new ArrayList<>(100); 
  22. for(int i=1;i < 101;i++){ 
  23. User user = new User(); 
  24. user.setId((long) i); 
  25. user.setUsername("username_"+i); 
  26. user.setPassword("xxxx"); 
  27. userList.add(user); 
  28. return userList; 
  29.  

注意: 這裏的 @Service註解包名是 com.alibaba.dubbo.config.annotation.Service,而不是Spring的那個。
最後啓動便可。

4.3建立消費者

  1. 建立SpringBoot項目dubbo-consumer
  2. pom.xml引入依賴
    與生產者的相同。
  3. 添加配置
    dubbo.application.name=dubbo-consumer
    dubbo.registry.address=zookeeper://ip:2181
  4. 在啓動類添加註解 @EnableDubbo
  5. 編寫測試類
  1. @RunWith(SpringRunner.class) 
  2. @SpringBootTest 
  3. public class DubboConsumerApplicationTests
  4.  
  5.  
  6. @Reference 
  7. private UserService userService; 
  8.  
  9.  
  10. @Test 
  11. public void contextLoads()
  12. List<User> users = userService.queryAll(); 
  13. users.forEach(user -> System.out.println(user)); 
  14.  
  1. 運行contextLoads()方法,控制檯輸出以下表示整合成功。

User{id=1, username='username_1', password='xxxx'}
User{id=2, username='username_2', password='xxxx'}
User{id=3, username='username_3', password='xxxx'}
User{id=4, username='username_4', password='xxxx'}
User{id=5, username='username_5', password='xxxx'}
User{id=6, username='username_6', password='xxxx'}
User{id=7, username='username_7', password='xxxx'}
User{id=8, username='username_8', password='xxxx'}
User{id=9, username='username_9', password='xxxx'}
。。。。

參考資料:https://github.com/apache/incubator-dubbo-spring-boot-project
代碼地址:https://github.com/2YSP/dubbo-springboot-demo

相關文章
相關標籤/搜索