You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2020/08/18 06:35:03 UTC

[dubbo] branch master updated: Replace custom MD5 method with Bytes.getMD5 (#6597)

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new e24a7b0  Replace custom MD5 method with Bytes.getMD5 (#6597)
e24a7b0 is described below

commit e24a7b0e5deb79b060aad1114f6e7a171fe00a49
Author: Jia He <49...@qq.com>
AuthorDate: Tue Aug 18 14:34:41 2020 +0800

    Replace custom MD5 method with Bytes.getMD5 (#6597)
    
    fix #6594
---
 .../loadbalance/ConsistentHashLoadBalance.java     | 23 +++-------------------
 1 file changed, 3 insertions(+), 20 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
index 855521f..15f65b3 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
@@ -17,13 +17,10 @@
 package org.apache.dubbo.rpc.cluster.loadbalance;
 
 import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.io.Bytes;
 import org.apache.dubbo.rpc.Invocation;
 import org.apache.dubbo.rpc.Invoker;
 import org.apache.dubbo.rpc.support.RpcUtils;
-
-import java.nio.charset.StandardCharsets;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
@@ -88,7 +85,7 @@ public class ConsistentHashLoadBalance extends AbstractLoadBalance {
             for (Invoker<T> invoker : invokers) {
                 String address = invoker.getUrl().getAddress();
                 for (int i = 0; i < replicaNumber / 4; i++) {
-                    byte[] digest = md5(address + i);
+                    byte[] digest = Bytes.getMD5(address + i);
                     for (int h = 0; h < 4; h++) {
                         long m = hash(digest, h);
                         virtualInvokers.put(m, invoker);
@@ -99,7 +96,7 @@ public class ConsistentHashLoadBalance extends AbstractLoadBalance {
 
         public Invoker<T> select(Invocation invocation) {
             String key = toKey(invocation.getArguments());
-            byte[] digest = md5(key);
+            byte[] digest = Bytes.getMD5(key);
             return selectForKey(hash(digest, 0));
         }
 
@@ -128,20 +125,6 @@ public class ConsistentHashLoadBalance extends AbstractLoadBalance {
                     | (digest[number * 4] & 0xFF))
                     & 0xFFFFFFFFL;
         }
-
-        private byte[] md5(String value) {
-            MessageDigest md5;
-            try {
-                md5 = MessageDigest.getInstance("MD5");
-            } catch (NoSuchAlgorithmException e) {
-                throw new IllegalStateException(e.getMessage(), e);
-            }
-            md5.reset();
-            byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
-            md5.update(bytes);
-            return md5.digest();
-        }
-
     }
 
 }