Dubbo不得不知的知識點

  • 2019 年 12 月 16 日
  • 筆記

一、Dubbo 快速入門


Dubbo核心功能解釋

dubbo 阿里開源的一個SOA服務治理框架,從目前來看把它稱作是一個RPC遠程調用框架更為貼切。單從RPC框架來說,功能較完善,支援多種傳輸和序列化方案。所以想必大家已經知道他的核心功能了:就是遠程調用。

快速演示Dubbo的遠程調用

實現步驟

  • 創建服務端項目
  • 創建客戶端項目

dubbo 引入:

<dependency>      <groupId>org.apache.dubbo</groupId>      <artifactId>dubbo</artifactId>      <version>2.7.4.1</version>  </dependency>

dubbo 默認必填依懶:

服務端程式碼:

public void openServer(int port) {      ApplicationConfig config = new ApplicationConfig();      config.setName("simple-app");      ProtocolConfig protocolConfig=new ProtocolConfig();      protocolConfig.setName("dubbo");      protocolConfig.setPort(port);      protocolConfig.setThreads(20);      ServiceConfig<UserService> serviceConfig=new ServiceConfig();      serviceConfig.setApplication(config);      serviceConfig.setProtocol(protocolConfig);      serviceConfig.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));      serviceConfig.setInterface(UserService.class);      serviceConfig.setRef(new UserServiceImpl());      serviceConfig.export();  }

客戶端程式碼:

static String remoteUrl = "dubbo://127.0.0.1:12345/tuling.dubbo.server.UserService";  // 構建遠程服務對象  public UserService buildRemoteService(String remoteUrl) {      ApplicationConfig application = new ApplicationConfig();      application.setName("young-app");      ReferenceConfig<UserService> referenceConfig = new ReferenceConfig<>();      referenceConfig.setApplication(application);      referenceConfig.setInterface(UserService.class);      referenceConfig.setUrl(remoteUrl);      UserService userService = referenceConfig.get();      return userService;  }

基於Dubbo實現服務集群:

在上一個例子中如多個服務的集群?即當有多個服務同時提供的時候,客戶端該調用哪個?以什麼方式進行調用以實現負載均衡?

一個簡單的辦法是將多個服務的URL同時設置到客戶端並初始化對應的服務實例,然後以輪詢的方式進行調用。

但如果訪問增大,需要擴容伺服器數量,那麼就必須增加配置重啟客戶端實例。顯然這不是我們願意看到的。Dubbo引入了服務註冊中的概念,可以解決動態擴容的問題。

演示基於註冊中心實現服集群:

修改服務端程式碼,添加zookeeper 註冊中心。

修改客戶端程式碼,添加zookeeper 註冊中心。

觀察 多個服務時,客戶端如何調用。

觀察 動態增減服務,客戶端的調用。

# 服務端連接註冊中心  serviceConfig.setRegistry(new RegistryConfig("zookeeper://224.1.1.1:2222"));
# 客戶端連接註冊中心  referenceConfig.setRegistry(new RegistryConfig("zookeeper://224.1.1.1:2222"));
#查看 基於UDP 佔用的2222 埠  netstat -ano|findstr 2222

基於spring IOC維護Dubbo 實例

在前面兩個例子中 出現了,ApplicationConfig、ReferenceConfig、RegistryConfig、com.alibaba.dubbo.config.ServiceConfig等實例 ,很顯然不需要每次調用的時候都去創建該實例那就需要一個IOC 容器去管理這些實例,spring 是一個很好的選擇。

提供者配置

<?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://dubbo.apache.org/schema/dubbo"         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">      <!-- 提供方應用資訊,用於計算依賴關係 -->      <dubbo:application name="simple-app"  />      <!-- 使用zookeeper廣播註冊中心暴露服務地址 -->      <dubbo:registry address="zookeeper://224.5.6.7:1234" />      <!-- 用dubbo協議在20880埠暴露服務 -->      <dubbo:protocol name="dubbo" port="20880" />      <!-- 聲明需要暴露的服務介面 -->      <dubbo:service interface="tuling.dubbo.server.UserService" ref="userService" />      <!-- 和本地bean一樣實現服務 -->      <bean id="userService" class="tuling.dubbo.server.impl.UserServiceImpl" />  </beans>

提供者服務暴露程式碼:

ApplicationContext context = new ClassPathXmlApplicationContext("/spring-provide.xml");  ((ClassPathXmlApplicationContext) context).start();  System.in.read();

消費者配置

<?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://dubbo.apache.org/schema/dubbo"         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">        <dubbo:application name="young-app"/>      <dubbo:registry address="zookeeper://224.5.6.7:1234"/>      <dubbo:reference id="userService" interface="tuling.dubbo.server.UserService"/>  </beans>

消費者調用程式碼:

ApplicationContext context = new ClassPathXmlApplicationContext("/spring-consumer.xml");  UserService userService = context.getBean(UserService.class);  UserVo u = userService.getUser(1111);  System.out.println(u);

二、Dubbo常規配置說明


Dubbo配置的整體說明:

標籤

用途

解釋

公共

用於配置當前應用資訊,不管該應用是提供者還是消費者

公共

用於配置連接註冊中心相關資訊

服務

用於配置提供服務的協議資訊,協議由提供方指定,消費方被動接受

服務

用於暴露一個服務,定義服務的元資訊,一個服務可以用多個協議暴露,一個服務也可以註冊到多個註冊中心

服務

當 ProtocolConfig 和 ServiceConfig 某屬性沒有配置時,採用此預設值,可選

引用

當 ReferenceConfig 某屬性沒有配置時,採用此預設值,可選

引用

用於創建一個遠程服務代理,一個引用可以指向多個註冊中心

公共

用於 ServiceConfig 和 ReferenceConfig 指定方法級的配置資訊

公共

用於指定方法參數配置

配置關係圖:

配置分類

所有配置項分為三大類。

  1. 服務發現:表示該配置項用於服務的註冊與發現,目的是讓消費方找到提供方。
  2. 服務治理:表示該配置項用於治理服務間的關係,或為開發測試提供便利條件。
  3. 性能調優:表示該配置項用於調優性能,不同的選項對性能會產生影響。