From f6bd816136a96804e0f772a78f4aaca6797cb18b Mon Sep 17 00:00:00 2001 From: gaohongtao Date: Thu, 15 Dec 2016 16:49:52 +0800 Subject: [PATCH] fixed #191 Using IP lowest bit to generate worker id --- sharding-jdbc-doc/content/post/release_notes.md | 5 +++++ .../id/generator/self/CommonSelfIdGenerator.java | 9 +++++++++ .../sharding/id/generator/self/IPIdGenerator.java | 14 ++++++-------- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/sharding-jdbc-doc/content/post/release_notes.md b/sharding-jdbc-doc/content/post/release_notes.md index dc3cd49e86d99..946d5463c40e0 100644 --- a/sharding-jdbc-doc/content/post/release_notes.md +++ b/sharding-jdbc-doc/content/post/release_notes.md @@ -8,6 +8,11 @@ weight = 1 ## 1.4.1-SNAPSHOT +### 功能提升 + +1. [ISSUE #191](https://github.com/dangdangdotcom/sharding-jdbc/issues/191) 根据主机的IP生成workerId的IdGenerator实现 +1. [ISSUE #192](https://github.com/dangdangdotcom/sharding-jdbc/issues/192) 根据HOSTNAME的数字尾缀获取workerId的IdGenerator + ### 缺陷修正 1. [ISSUE #194](https://github.com/dangdangdotcom/sharding-jdbc/issues/194) jdbc接口中资源释放错误 diff --git a/sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/CommonSelfIdGenerator.java b/sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/CommonSelfIdGenerator.java index 5b7a080ffd999..7e90e0a3c1b9e 100644 --- a/sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/CommonSelfIdGenerator.java +++ b/sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/CommonSelfIdGenerator.java @@ -112,6 +112,15 @@ public static void setWorkerId(final Long workerId) { CommonSelfIdGenerator.workerId = workerId; } + /** + * 获取工作Id的二进制长度. + * + * @return 工作Id的二进制长度 + */ + public static long getWorkerIdLength() { + return WORKER_ID_BITS; + } + /** * 生成Id. * diff --git a/sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/IPIdGenerator.java b/sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/IPIdGenerator.java index 5fca2c82413c2..12786a3c667e5 100644 --- a/sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/IPIdGenerator.java +++ b/sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/IPIdGenerator.java @@ -23,9 +23,9 @@ import java.net.UnknownHostException; /** - * 根据机器IP获取工作进程Id,如果线上机器的IP二进制表示的最后10位不重复,建议使用此种方式 - * ,列如机器的IP为192.168.1.108,二进制表示:11000000 10101000 00000001 01101100 - * ,截取最后10位 01 01101100,转为十进制364,设置workerId为364 + * 根据机器IP获取工作进程Id,如果线上机器的IP二进制表示的最后10位不重复,建议使用此种方式 + * ,列如机器的IP为192.168.1.108,二进制表示:11000000 10101000 00000001 01101100 + * ,截取最后10位 01 01101100,转为十进制364,设置workerId为364. * * @author DonneyYoung */ @@ -39,15 +39,13 @@ public class IPIdGenerator implements IdGenerator { static void initWorkerId() { InetAddress address; - Long workerId; try { address = InetAddress.getLocalHost(); - } catch (UnknownHostException e) { + } catch (final UnknownHostException e) { throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!"); } - String[] ipAddress = address.getHostAddress().split("\\."); - workerId = ((Long.valueOf(ipAddress[ipAddress.length - 2]) & 0b11) << 8) + Long.valueOf(ipAddress[ipAddress.length - 1]); - CommonSelfIdGenerator.setWorkerId(workerId); + byte[] ipAddressByteArray = address.getAddress(); + CommonSelfIdGenerator.setWorkerId((long) (((ipAddressByteArray[ipAddressByteArray.length - 2] & 0B11) << Byte.SIZE) + (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF))); } @Override