Dubbo 源碼分析系列之一環境搭建


環境搭建的步驟有哪些

  1. 依賴外部的環境
  2. 使用的開發工具
  3. 源碼的拉取
  4. 結構大致介紹

1 依賴的外部環境

  • 安裝JDK
  • 安裝Git
  • 安裝maven

這邊我們就不介紹怎么安裝這些外部環境了,大家自行從安裝這些外部環境哈

2 使用的外部工具

  • 編輯器使用 IntelliJ IDEA (簡單好用,快捷鍵豐富)
  • GitHub Desktop GitHub的客戶端軟件,用起來真順手

3 源碼的拉取

源碼的話我們上GitHub 上面直接拉取dubbo的源碼即可, Dubbo 然后我們 Fork出一個屬於我們自己的倉庫,之后我們就可以在我們GitHub的倉庫中看到這個項目了。然后我們將這個項目down到本地進行調試

具體Git操作不詳細介紹,下面貼出幾個比較好的供大家參考

https://blog.csdn.net/qq_32040767/article/details/77096761

https://blog.csdn.net/menggudaoke/article/details/77744541

https://juejin.im/entry/5a5f3b286fb9a01c9064e83b

https://www.cnblogs.com/MrJun/p/3351478.html

這里我們為什么使用Fork 而不是Star,這是因為star的項目你不是Pull Request 到源項目的,而Fork 是可以的。想想一下要是你在看源碼的過程中發現了bug。然后提交給他們並審核這是一件多酷的事情

4 Dubbo的大致介紹

下面的這個 Dubbo 官網上最為經典的介紹,一張圖就將整個Dubbo 過程大致的介紹的差不多。

什么是Dubbo?

一個分布式服務治理框架

Dubbo的作用是什么?

作用是解決下面的問題

  • 單一應用架構

  • 垂直應用架構

  • 分布式服務架構

  • 流動計算架構

節點角色說明

節點 角色說明
Provider 暴露服務的服務提供方
Consumer 調用遠程服務的服務消費方
Registry 服務注冊與發現的注冊中心
Monitor 統計服務的調用次數和調用時間的監控中心
Container 服務運行容器

調用關系說明

  1. 服務容器負責啟動,加載,運行服務提供者。

  2. 服務容器負責啟動,加載,運行服務提供者。
  3. 服務提供者在啟動時,向注冊中心注冊自己提供的服務。
  4. 服務消費者在啟動時,向注冊中心訂閱自己所需的服務。
  5. 注冊中心返回服務提供者地址列表給消費者,如果有變更,注冊中心將基於長連接推送變更數據給消費者。
  6. 服務消費者,從提供者地址列表中,基於軟負載均衡算法,選一台提供者進行調用,如果調用失敗,再選另一台調用。
  7. 服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鍾發送一次統計數據到監控中心。

詳細介紹下 Registry Provider Consumer

因為這三者是整個Dubbo 調用過程的鏈路核心

talk is cheap show me the code

光說不行,咱們通過看下項目中的源碼來看下dubbo整個結構

dubbo提供了 一個demo項目供大家測試調試 整個demo已經在你down的dubbo的項目中了

打開我們的dubbo項目,我們會看到這樣的一個子項目

這里就是那個demo子項目 就是我們可以直接運行測試的項目

將整個測試項目展開,我們會看見這幾個文件

我們先進入consumer項目中的 Consumer類中,運行這個類。在這里是一個main函數,我們直接運行就ok了

之后我們再啟動Provider 類。這樣的話,我們就啟動了一對消費者和生產類。這樣的話,最簡單的dubbo程序就 啟動了。

下面我們根據這個最簡單的類來分析下

package org.apache.dubbo.demo; // 服務對外暴露接口 public interface DemoService { String sayHello(String name); } 
package org.apache.dubbo.demo.consumer;

import org.apache.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {

    /**
     * To get ipv6 address to work, add
     * System.setProperty("java.net.preferIPv6Addresses", "true");
     * before running your application.
     */
    public static void main(String[] args) {
        //加載配置的xml文件
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        //從這個上下文bean中獲取類
        DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
        while (true) {
            try {
                Thread.sleep(1000);
                String hello = demoService.sayHello("world"); // call remote method
                System.out.println(hello); // get result
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
}

消費者的測試demo,循環調用方法

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion), don't set it same as provider --> <dubbo:application name="demo-consumer"/> <!-- use multicast registry center to discover service --> <!-- 測試的時候使用組播來進行注冊發現 --> <dubbo:registry address="multicast://224.5.6.7:1234"/> <!-- generate proxy for the remote service, then demoService can be used in the same way as the local regular interface --> <!--生成一個代理,在這個方法調用遠程方法就像調用本地方法一樣 --> <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/> </beans> 

我們通過配置文件xml來進行服務的發現和啟用,還有對注冊中心的配置

下面介紹下服務的提供者Provider

package org.apache.dubbo.demo.provider; import org.apache.dubbo.demo.DemoService; import org.apache.dubbo.rpc.RpcContext; import java.text.SimpleDateFormat; import java.util.Date; public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { //對外暴露的服務內部實現類 System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); } } 
package org.apache.dubbo.demo.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { /** * To get ipv6 address to work, add * System.setProperty("java.net.preferIPv6Addresses", "true"); * before running your application. */ public static void main(String[] args) throws Exception { //加載xml配置 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"}); context.start(); System.in.read(); // press any key to exit } } 
<?xml version="1.0" encoding="UTF-8"?> <!-- provider's application name, used for tracing dependency relationship --> <!-- 提供一個應用名稱,用於查找依賴的關系--> <dubbo:application name="demo-provider"/> <!-- use multicast registry center to export service --> <!-- 使用組播來進行服務的暴露--> <dubbo:registry address="multicast://224.5.6.7:1234"/> <!-- use dubbo protocol to export service on port 20880 --> <!-- 使用dubbo協議對外暴露時的端口--> <dubbo:protocol name="dubbo" port="20880"/> <!-- service implementation, as same as regular local bean --> <!-- 業務實現類,對外暴露類的內部實現--> <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/> <!-- declare the service interface to be exported --> <!-- 對外暴露服務的接口 --> <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/> </beans> 

 


 

 

 

博客園展示效果不好,可以移步到GitHub 博客上

http://wsccoder.top/2018/09/14/Dubbo-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90%E7%B3%BB%E5%88%97%E4%B9%8B%E4%B8%80%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/ 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM