主頁 > 後端開發 > Dubbo原始碼(八) - 負載均衡

Dubbo原始碼(八) - 負載均衡

2022-08-17 07:31:41 後端開發

前言

本文基于Dubbo2.6.x版本,中文注釋版原始碼已上傳github:xiaoguyu/dubbo

負載均衡,英文名稱為Load Balance,其含義就是指將負載(作業任務)進行平衡、分攤到多個操作單元上進行運行,

例如:在Dubbo中,同一個服務有多個服務提供者,每個服務提供者所在的機器性能不一致,如果流量均勻分攤,則會導致有些服務提供者負載過高,有些則輕輕松松,導致資源浪費,負載均衡就解決這個問題,

原始碼

LoadBalance就是負載均衡的介面,咱們先看看類圖

Untitled

Dubbo提供了4中內置的負載均衡實作:

  1. RandomLoadBalance:基于權重隨機演算法
  2. LeastActiveLoadBalance:基于最少活躍呼叫數演算法
  3. ConsistentHashLoadBalance:基于 hash 一致性演算法
  4. RoundRobinLoadBalance:基于加權輪詢演算法

那么負載均衡是在哪里被用的的呢?

AbstractClusterInvokerselectreselect方法,不熟悉這兩個方法的,可以去看《Dubbo集群》

AbstractLoadBalance

抽象類封裝了一些公共的邏輯,在看具體實作類之前,我們先看看抽象類AbstractLoadBalance中的方法

public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) {
    if (invokers == null || invokers.isEmpty())
        return null;
    // 如果 invokers 串列中僅有一個 Invoker,直接回傳即可,無需進行負載均衡
    if (invokers.size() == 1)
        return invokers.get(0);
    // 呼叫 doSelect 方法進行負載均衡,該方法為抽象方法,由子類實作
    return doSelect(invokers, url, invocation);
}

LoadBalance介面只有一個方法,那就是 select 方法,這是負載均衡的入口,根據 invoker 數量判斷是否需要進行負載均衡,這里的 doSelect 是個抽象方法,由子類實作,

protected int getWeight(Invoker<?> invoker, Invocation invocation) {
    int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
    if (weight > 0) {
        // 獲取服務提供者啟動時間戳
        long timestamp = invoker.getUrl().getParameter(Constants.REMOTE_TIMESTAMP_KEY, 0L);
        if (timestamp > 0L) {
            // 計算服務提供者運行時長
            int uptime = (int) (System.currentTimeMillis() - timestamp);
            // 獲取服務預熱時間,默認為10分鐘
            int warmup = invoker.getUrl().getParameter(Constants.WARMUP_KEY, Constants.DEFAULT_WARMUP);
            // 如果服務運行時間小于預熱時間,則重新計算服務權重,即降權
            if (uptime > 0 && uptime < warmup) {
                // 重新計算服務權重
                weight = calculateWarmupWeight(uptime, warmup, weight);
            }
        }
    }
    return weight;
}

static int calculateWarmupWeight(int uptime, int warmup, int weight) {
    // 計算權重,下面代碼邏輯上形似于 (uptime / warmup) * weight,
    // 隨著服務運行時間 uptime 增大,權重計算值 ww 會慢慢接近配置值 weight
    int ww = (int) ((float) uptime / ((float) warmup / (float) weight));
    return ww < 1 ? 1 : (ww > weight ? weight : ww);
}

getWeight 是獲取權重的方法,默認權重為100,這里有個服務預熱的操作,當服務的啟動時間小于預熱時間,權重會減少,這個權重由 calculateWarmupWeight 方法計算,

預熱的目的是讓服務啟動后“低功率”運行一段時間,使其效率慢慢提升至最佳狀態,

以上就是抽象類的全部方法,下面我們看實作類的,

RandomLoadBalance

RandomLoadBalance 是加權隨機演算法的具體實作,是Dubbo默認的負載均衡策略,

假設我們有一組服務器 servers = [A, B, C],他們對應的權重為 weights = [5, 3, 2],權重總和為10,

Untitled

我們取一個大于等于0,小于10的亂數,計算亂數落在哪個區間,例如4在A區間,7在B區間,

權重越大,落在該區間的概率就越大,這就是加權隨機演算法,

下面看具體代碼實作

public class RandomLoadBalance extends AbstractLoadBalance {

    public static final String NAME = "random";

    private final Random random = new Random();

    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        int length = invokers.size(); // Number of invokers
        int totalWeight = 0; // The sum of weights
        boolean sameWeight = true; // Every invoker has the same weight?
        // 下面這個回圈有兩個作用,第一是計算總權重 totalWeight,
        // 第二是檢測每個服務提供者的權重是否相同
        for (int i = 0; i < length; i++) {
            int weight = getWeight(invokers.get(i), invocation);
            totalWeight += weight; // Sum
            // 檢測當前服務提供者的權重與上一個服務提供者的權重是否相同,
            // 不相同的話,則將 sameWeight 置為 false,
            if (sameWeight && i > 0
                    && weight != getWeight(invokers.get(i - 1), invocation)) {
                sameWeight = false;
            }
        }
        if (totalWeight > 0 && !sameWeight) {
            // 隨機獲取一個 [0, totalWeight) 區間內的數字
            int offset = random.nextInt(totalWeight);
            // Return a invoker based on the random value.
            // 回圈讓 offset 數減去服務提供者權重值,當 offset 小于0時,回傳相應的 Invoker,
            // 舉例說明一下,我們有 servers = [A, B, C],weights = [5, 3, 2],offset = 7,
            // 第一次回圈,offset - 5 = 2 > 0,即 offset > 5,
            // 表明其不會落在服務器 A 對應的區間上,
            // 第二次回圈,offset - 3 = -1 < 0,即 5 < offset < 8,
            // 表明其會落在服務器 B 對應的區間上
            for (int i = 0; i < length; i++) {
                // 讓隨機值 offset 減去權重值
                offset -= getWeight(invokers.get(i), invocation);
                if (offset < 0) {
                    // 回傳相應的 Invoker
                    return invokers.get(i);
                }
            }
        }
        // 如果所有服務提供者權重值相同,此時直接隨機回傳一個即可
        return invokers.get(random.nextInt(length));
    }
}

如果權重一致,就隨機選擇一個,如果權重不同,則根據權重分配,

LeastActiveLoadBalance

最小活躍數負載均衡,這個活躍數表示執行中的請求數量,每個服務提供者對應一個活躍數 active,初始情況下,所有服務提供者活躍數均為0,每收到一個請求,活躍數加1,完成請求后則將活躍數減1,

在流量均勻的情況下,活躍數越低的服務提供者,其性能越好,

protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
    int length = invokers.size(); // Number of invokers
    // 最小的活躍數
    int leastActive = -1; // The least active value of all invokers
    // 具有相同“最小活躍數”的服務者提供者(以下用 Invoker 代稱)數量
    int leastCount = 0; // The number of invokers having the same least active value (leastActive)
    // leastIndexs 用于記錄具有相同“最小活躍數”的 Invoker 在 invokers 串列中的下標資訊
    int[] leastIndexs = new int[length]; // The index of invokers having the same least active value (leastActive)
    int totalWeight = 0; // The sum of with warmup weights
    // 第一個最小活躍數的 Invoker 權重值,用于與其他具有相同最小活躍數的 Invoker 的權重進行對比,
    // 以檢測是否“所有具有相同最小活躍數的 Invoker 的權重”均相等
    int firstWeight = 0; // Initial value, used for comparision
    boolean sameWeight = true; // Every invoker has the same weight value?

    // 遍歷 invokers 串列
    for (int i = 0; i < length; i++) {
        Invoker<T> invoker = invokers.get(i);
        // 獲取 Invoker 對應的活躍數
        int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive(); // Active number
        // 獲取權重
        int afterWarmup = getWeight(invoker, invocation); // Weight
        // 發現更小的活躍數,重新開始
        if (leastActive == -1 || active < leastActive) { // Restart, when find a invoker having smaller least active value.
            // 使用當前活躍數 active 更新最小活躍數 leastActive
            leastActive = active; // Record the current least active value
            // 更新 leastCount 為 1
            leastCount = 1; // Reset leastCount, count again based on current leastCount
            // 記錄當前下標值到 leastIndexs 中
            leastIndexs[0] = i; // Reset
            totalWeight = afterWarmup; // Reset
            firstWeight = afterWarmup; // Record the weight the first invoker
            sameWeight = true; // Reset, every invoker has the same weight value?
        // 當前 Invoker 的活躍數 active 與最小活躍數 leastActive 相同
        } else if (active == leastActive) { // If current invoker's active value equals with leaseActive, then accumulating.
            // 在 leastIndexs 中記錄下當前 Invoker 在 invokers 集合中的下標
            leastIndexs[leastCount++] = i; // Record index number of this invoker
            // 累加權重
            totalWeight += afterWarmup; // Add this invoker's weight to totalWeight.
            // If every invoker has the same weight?
            // 檢測當前 Invoker 的權重與 firstWeight 是否相等,
            // 不相等則將 sameWeight 置為 false
            if (sameWeight && i > 0
                    && afterWarmup != firstWeight) {
                sameWeight = false;
            }
        }
    }
    // assert(leastCount > 0)
    // 當只有一個 Invoker 具有最小活躍數,此時直接回傳該 Invoker 即可
    if (leastCount == 1) {
        // If we got exactly one invoker having the least active value, return this invoker directly.
        return invokers.get(leastIndexs[0]);
    }
    // 有多個 Invoker 具有相同的最小活躍數,但它們之間的權重不同
    if (!sameWeight && totalWeight > 0) {
        // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
        // 隨機生成一個 [0, totalWeight) 之間的數字
        int offsetWeight = random.nextInt(totalWeight) + 1;
        // Return a invoker based on the random value.
        // 回圈讓亂數減去具有最小活躍數的 Invoker 的權重值,
        // 當 offset 小于等于0時,回傳相應的 Invoker
        for (int i = 0; i < leastCount; i++) {
            int leastIndex = leastIndexs[i];
            // 獲取權重值,并讓亂數減去權重值
            offsetWeight -= getWeight(invokers.get(leastIndex), invocation);
            if (offsetWeight <= 0)
                return invokers.get(leastIndex);
        }
    }
    // If all invokers have the same weight value or totalWeight=0, return evenly.
    // 如果權重相同或權重為0時,隨機回傳一個 Invoker
    return invokers.get(leastIndexs[random.nextInt(leastCount)]);
}

代碼比較多,不過都有注釋,耐心看即可,這里大體做了幾件事:

  1. 遍歷 invokers 集合,找出活躍數最小的 invoker
  2. 如果只有一個 invoker 有最小活躍數,則回傳
  3. 如果有多個 invoker 有相同的最小活躍數,則這些 invoker 進行加權隨機演算法處理(也就是對這幾個最小活躍數 invoker 進行 RandomLoadBalance 的邏輯)

這里有個點想擴展說下,就是獲取活躍數的方法

RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive();

RpcStatus記錄著當前呼叫次數、總數、失敗數、呼叫間隔等狀態資訊,

這些資訊,在服務消費者端由ActiveLimitFilter記錄,在服務提供者端由ExecuteLimitFilter記錄,也就是,想要拿到正確的活躍數,需要ActiveLimitFilter生效才行,

@Activate(group = Constants.CONSUMER, value = https://www.cnblogs.com/konghuanxi/p/Constants.ACTIVES_KEY)
public class ActiveLimitFilter implements Filter

ActiveLimitFilter生效需要滿足兩個條件,消費者端以及URL中攜帶actives引數,actives可在消費者端或生產者端配置,含義為:每服務消費者每服務每方法最大并發呼叫數

<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" registry="remoteRegistry" actives="5" />
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" loadbalance="leastactive" actives="5" />

當然,也能給消費者介面指定過濾器的方法來啟用ActiveLimitFilter

<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" filter="activelimit" />

RoundRobinLoadBalance

RoundRobinLoadBalance是加權輪詢負載均衡的實作,加權輪詢的原理步驟如下:

假設服務 [A, B, C] 的權重為 [5, 1, 1] ,即總權重為 7, 當前權重currentWeight初始為[0, 0, 0]

  1. 當前權重加上每個服務各自的權重,跳轉步驟2

    此時currentWeight為 [0+5, 0+1, 0+1] = [5, 1, 1]

  2. 回傳currentWeight中最高的服務,跳轉步驟3

    currentWeight為 [5, 1, 1] ,回傳服務A

  3. 將第2步中的那個最高權重在currentWeight對應的值減去總權重,跳轉步驟4

    currentWeight為 [5 - 7, 1, 1] = [-2, 1, 1]

  4. 重復步驟1

下面的GIF圖為了好表示柱狀圖,所以我將currentWeight初始權重變為10

Untitled

經過一定回圈次數,最終currentWeight又會回歸初始值,而這個回圈次數計算如下:

次數 = 服務A的權重 + 服務B的權重 + …

每個服務的呼叫次數也等于它的權重

看完原理,我們繼續看原始碼

protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
    // key = 全限定類名 + "." + 方法名,比如 com.xxx.DemoService.sayHello
    String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
    // 獲取 url 到 WeightedRoundRobin 映射表,如果為空,則創建一個新的
    ConcurrentMap<String, WeightedRoundRobin> map = methodWeightMap.get(key);
    if (map == null) {
        methodWeightMap.putIfAbsent(key, new ConcurrentHashMap<String, WeightedRoundRobin>());
        map = methodWeightMap.get(key);
    }
    // 權重總和
    int totalWeight = 0;
    // 最大權重
    long maxCurrent = Long.MIN_VALUE;
    long now = System.currentTimeMillis();
    Invoker<T> selectedInvoker = null;
    WeightedRoundRobin selectedWRR = null;

    // 下面這個回圈主要做了這樣幾件事情:
    //   1. 遍歷 Invoker 串列,檢測當前 Invoker 是否有
    //      相應的 WeightedRoundRobin,沒有則創建
    //   2. 檢測 Invoker 權重是否發生了變化,若變化了,
    //      則更新 WeightedRoundRobin 的 weight 欄位
    //   3. 讓 current 欄位加上自身權重,等價于 current += weight
    //   4. 設定 lastUpdate 欄位,即 lastUpdate = now
    //   5. 尋找具有最大 current 的 Invoker,以及 Invoker 對應的 WeightedRoundRobin,
    //      暫存起來,留作后用
    //   6. 計算權重總和
    for (Invoker<T> invoker : invokers) {
        String identifyString = invoker.getUrl().toIdentityString();
        WeightedRoundRobin weightedRoundRobin = map.get(identifyString);
        int weight = getWeight(invoker, invocation);
        if (weight < 0) {
            weight = 0;
        }
        // 檢測當前 Invoker 是否有對應的 WeightedRoundRobin,沒有則創建
        if (weightedRoundRobin == null) {
            weightedRoundRobin = new WeightedRoundRobin();
            // 設定 Invoker 權重
            weightedRoundRobin.setWeight(weight);
            // 存盤 url 唯一標識 identifyString 到 weightedRoundRobin 的映射關系
            map.putIfAbsent(identifyString, weightedRoundRobin);
            weightedRoundRobin = map.get(identifyString);
        }
        // Invoker 權重不等于 WeightedRoundRobin 中保存的權重,說明權重變化了,此時進行更新
        if (weight != weightedRoundRobin.getWeight()) {
            //weight changed
            weightedRoundRobin.setWeight(weight);
        }
        // 讓 current 加上自身權重,等價于 current += weight
        long cur = weightedRoundRobin.increaseCurrent();
        // 設定 lastUpdate,表示近期更新過
        weightedRoundRobin.setLastUpdate(now);
        if (cur > maxCurrent) {
            maxCurrent = cur;
            // 將具有最大 current 權重的 Invoker 賦值給 selectedInvoker
            selectedInvoker = invoker;
            // 將 Invoker 對應的 weightedRoundRobin 賦值給 selectedWRR,留作后用
            selectedWRR = weightedRoundRobin;
        }
        // 計算權重總和
        totalWeight += weight;
    }
    // 對 <identifyString, WeightedRoundRobin> 進行檢查,過濾掉長時間未被更新的節點,
    // 該節點可能掛了,invokers 中不包含該節點,所以該節點的 lastUpdate 長時間無法被更新,
    // 若未更新時長超過閾值后,就會被移除掉,默認閾值為60秒,
    if (!updateLock.get() && invokers.size() != map.size()) {
        if (updateLock.compareAndSet(false, true)) {
            try {
                // copy -> modify -> update reference
                ConcurrentMap<String, WeightedRoundRobin> newMap = new ConcurrentHashMap<String, WeightedRoundRobin>();
                // 拷貝
                newMap.putAll(map);

                // 遍歷修改,即移除過期記錄
                Iterator<Entry<String, WeightedRoundRobin>> it = newMap.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, WeightedRoundRobin> item = it.next();
                    if (now - item.getValue().getLastUpdate() > RECYCLE_PERIOD) {
                        it.remove();
                    }
                }
                // 更新參考
                methodWeightMap.put(key, newMap);
            } finally {
                updateLock.set(false);
            }
        }
    }
    if (selectedInvoker != null) {
        // 讓 current 減去權重總和,等價于 current -= totalWeight
        selectedWRR.sel(totalWeight);
        // 回傳具有最大 current 的 Invoker
        return selectedInvoker;
    }
    // should not happen here
    return invokers.get(0);
}

注釋寫的很詳細了,和原理步驟差不多,原始碼中多個對長時間未更新 invoker 的處理,

ConsistentHashLoadBalance

一致性Hash演算法,

其原理簡單講,就是假定有一個圓環,每個服務根據其 hash 值,在圓環上有個位置(如圖的cache-1、cache-2等),當有請求過來的,同樣根據請求的 hash 值確定請求的位置,并根據請求的位置去獲取最近的下一個服務的位置,如下圖:

Untitled

當請求落在 cache-2 和 cache-3 之間時,下一個最近的是 cache-3,如果 cache-3 服務不可用,那么最近的下個服務就是 cache-4

這時,又引入了一個資源傾斜的問題,那就是大量請求集中在同一個服務中,由于服務在圓環上分布不均,導致大部分請求都落在cache-2中,如下圖:

Untitled

那么該如何處理資源傾斜的問題?引入虛擬節點,就是一個服務有多個多個位置,這樣就能使請求更均勻,如下圖:

Untitled

以上就是一致性hash演算法的原理,下面講講Dubbo的原始碼

public class ConsistentHashLoadBalance extends AbstractLoadBalance {

    private final ConcurrentMap<String, ConsistentHashSelector<?>> selectors = new ConcurrentHashMap<String, ConsistentHashSelector<?>>();

    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        String methodName = RpcUtils.getMethodName(invocation);
        String key = invokers.get(0).getUrl().getServiceKey() + "." + methodName;

        // 獲取 invokers 原始的 hashcode
        int identityHashCode = System.identityHashCode(invokers);
        ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.get(key);
        // 如果 invokers 是一個新的 List 物件,意味著服務提供者數量發生了變化,可能新增也可能減少了,
        // 此時 selector.identityHashCode != identityHashCode 條件成立
        if (selector == null || selector.identityHashCode != identityHashCode) {
            // 創建新的 ConsistentHashSelector
            selectors.put(key, new ConsistentHashSelector<T>(invokers, methodName, identityHashCode));
            selector = (ConsistentHashSelector<T>) selectors.get(key);
        }
        // 呼叫 ConsistentHashSelector 的 select 方法選擇 Invoker
        return selector.select(invocation);
    }

    private static final class ConsistentHashSelector<T> {...}
}

doSelect 方法先從快取獲取 selector ,如果快取沒有,則創建并放入快取,然后呼叫 selector.select 方法獲取 invoker ,所以一致性 hash 的實作,在ConsistentHashSelector中,我們先看其構造方法

ConsistentHashSelector(List<Invoker<T>> invokers, String methodName, int identityHashCode) {
    // 可以認為virtualInvokers組成了hash環
    this.virtualInvokers = new TreeMap<Long, Invoker<T>>();
    this.identityHashCode = identityHashCode;
    URL url = invokers.get(0).getUrl();
    // 獲取虛擬節點數,默認為160
    this.replicaNumber = url.getMethodParameter(methodName, "hash.nodes", 160);
    // 獲取參與 hash 計算的引數下標值,默認對第一個引數進行 hash 運算
    String[] index = Constants.COMMA_SPLIT_PATTERN.split(url.getMethodParameter(methodName, "hash.arguments", "0"));
    argumentIndex = new int[index.length];
    for (int i = 0; i < index.length; i++) {
        argumentIndex[i] = Integer.parseInt(index[i]);
    }
    for (Invoker<T> invoker : invokers) {
        String address = invoker.getUrl().getAddress();
        for (int i = 0; i < replicaNumber / 4; i++) {
            // 對 address + i 進行 md5 運算,得到一個長度為16的位元組陣列
            byte[] digest = md5(address + i);
            // 對 digest 部分位元組進行4次 hash 運算,得到四個不同的 long 型正整數
            for (int h = 0; h < 4; h++) {
                // h = 0 時,取 digest 中下標為 0 ~ 3 的4個位元組進行位運算
                // h = 1 時,取 digest 中下標為 4 ~ 7 的4個位元組進行位運算
                // h = 2, h = 3 時程序同上
                long m = hash(digest, h);
                // 將 hash 到 invoker 的映射關系存盤到 virtualInvokers 中,
                // virtualInvokers 需要提供高效的查詢操作,因此選用 TreeMap 作為存盤結構
                virtualInvokers.put(m, invoker);
            }
        }
    }
}

ConsistentHashSelector的構造方法,主要是計算 invokers 的每一個 invoker 的hash,并將其放入 virtualInvokers 中,從這里可以看到,Dubbo默認的虛擬節點為160個,對比一致性 hash 演算法中,virtualInvokers 就是 hash 環,invoker 就是節點,

我們繼續看如何從 hash 環中找到最近的節點

public Invoker<T> select(Invocation invocation) {
    // 將引數轉為 key
    String key = toKey(invocation.getArguments());
    // 對引數 key 進行 md5 運算
    byte[] digest = md5(key);
    // 取 digest 陣列的前四個位元組進行 hash 運算,再將 hash 值傳給 selectForKey 方法,
    // 尋找合適的 Invoker
    return selectForKey(hash(digest, 0));
}

private Invoker<T> selectForKey(long hash) {
    // 到 TreeMap 中查找第一個節點值大于或等于當前 hash 的 Invoker
    Map.Entry<Long, Invoker<T>> entry = virtualInvokers.tailMap(hash, true).firstEntry();
    // 如果 hash 大于 Invoker 在圓環上最大的位置,此時 entry = null,
    // 需要將 TreeMap 的頭節點賦值給 entry
    if (entry == null) {
        entry = virtualInvokers.firstEntry();
    }
    return entry.getValue();
}

選擇的程序也很簡單,依賴的是 TreeMap 的 tailMap 方法,

總結

本文介紹了Dubbo內置的4中負載均衡實作,至此,Dubbo的集群容錯的四個部分,也就是服務目錄 Directory、服務路由 Router、集群 Cluster 和負載均衡 LoadBalance 都已全部講完,


參考資料

Dubbo開發指南

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/501976.html

標籤:Java

上一篇:RocketMQ保姆級教程

下一篇:光說不練假把式,一起Kafka業務實戰。

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more