-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed #192 Extract workerId from hostname string
- Loading branch information
Showing
2 changed files
with
49 additions
and
38 deletions.
There are no files selected for viewing
44 changes: 27 additions & 17 deletions
44
...rc/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/HostNameIdGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,61 @@ | ||
/* | ||
* Copyright 1999-2015 dangdang.com. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* </p> | ||
*/ | ||
|
||
package com.dangdang.ddframe.rdb.sharding.id.generator.self; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
/** | ||
* . | ||
* 根据机器名最后的数字编号获取工作进程Id,如果线上机器命名有统一规范,建议使用此种方式 | ||
* ,列如机器的HostName为 | ||
* ,dangdang-db-sharding-dev-01(公司名-部门名-服务名-环境名-编号) | ||
* ,会截取HostName最后的编号01作为workerId。 | ||
* 根据机器名最后的数字编号获取工作进程Id.如果线上机器命名有统一规范,建议使用此种方式. | ||
* 列如机器的HostName为:dangdang-db-sharding-dev-01(公司名-部门名-服务名-环境名-编号) | ||
* ,会截取HostName最后的编号01作为workerId. | ||
* | ||
* @author DonneyYoung | ||
**/ | ||
@Getter | ||
@Slf4j | ||
public class HostNameIdGenerator implements IdGenerator { | ||
|
||
private static final CommonSelfIdGenerator COMMON_SELF_ID_GENERATOR; | ||
private final CommonSelfIdGenerator commonSelfIdGenerator = new CommonSelfIdGenerator(); | ||
|
||
static { | ||
COMMON_SELF_ID_GENERATOR = new CommonSelfIdGenerator(); | ||
initWorkerId(); | ||
} | ||
|
||
static void initWorkerId() { | ||
InetAddress addr; | ||
InetAddress address; | ||
Long workerId; | ||
try { | ||
addr = InetAddress.getLocalHost(); | ||
address = InetAddress.getLocalHost(); | ||
} catch (final UnknownHostException e) { | ||
throw new IllegalStateException("Cannot get LocalHost InetAddress , please check your network!"); | ||
throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!"); | ||
} | ||
String hostName = address.getHostName(); | ||
try { | ||
String hostName = addr.getHostName(); | ||
workerId = Long.valueOf(hostName.replace(hostName.replaceAll("\\d+$", ""), "")); | ||
} catch (final NumberFormatException e) { | ||
throw new IllegalArgumentException("Wrong hostname , hostname must be end with number!"); | ||
throw new IllegalArgumentException(String.format("Wrong hostname:%s, hostname must be end with number!", hostName)); | ||
} | ||
CommonSelfIdGenerator.setWorkerId(workerId); | ||
} | ||
|
||
@Override | ||
public Number generateId() { | ||
return COMMON_SELF_ID_GENERATOR.generateId(); | ||
return commonSelfIdGenerator.generateId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters