xuxueli / xxl-job

A distributed task scheduling framework.(分布式任务调度平台XXL-JOB)
http://www.xuxueli.com/xxl-job/
GNU General Public License v3.0
27.46k stars 10.86k forks source link

[请教贴] 关于com.xxl.job.admin.core.route.strategy.ExecutorRouteConsistentHash#hash方法 #1284

Closed jzdayz closed 4 years ago

jzdayz commented 4 years ago

Please answer some questions before submitting your issue. Thanks!

Which version of XXL-JOB do you using?

2.1.0

Expected behavior

Actual behavior

Steps to reproduce the behavior

Other information

private static long hash(String key) {

        // md5 byte
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 not supported", e);
        }
        md5.reset();
        byte[] keyBytes = null;
        try {
            keyBytes = key.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Unknown string :" + key, e);
        }

        md5.update(keyBytes);
        byte[] digest = md5.digest();

        // hash code, Truncate to 32-bits
        long hashCode = ((long) (digest[3] & 0xFF) << 24)
                | ((long) (digest[2] & 0xFF) << 16)
                | ((long) (digest[1] & 0xFF) << 8)
                | (digest[0] & 0xFF);

        long truncateHashCode = hashCode & 0xffffffffL;
        return truncateHashCode;
    }

这个digest[2] & 0xFF的值不还是digest[2] ,请问一下,这样做是否是有什么缘由 hashCode & 0xffffffffL 这个的话是有什么特殊的含义吗?

xuxueli commented 4 years ago

你好,这是为了转换为32位哈希值、