/* * 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 java.net.InetAddress; import java.net.UnknownHostException; /** * 根据机器名最后的数字编号获取工作进程Id.如果线上机器命名有统一规范,建议使用此种方式. * 列如机器的HostName为:dangdang-db-sharding-dev-01(公司名-部门名-服务名-环境名-编号) * ,会截取HostName最后的编号01作为workerId. * * @author DonneyYoung **/ public class HostNameIdGenerator implements IdGenerator { private final CommonSelfIdGenerator commonSelfIdGenerator = new CommonSelfIdGenerator(); static { initWorkerId(); } static void initWorkerId() { InetAddress address; Long workerId; try { address = InetAddress.getLocalHost(); } catch (final UnknownHostException e) { throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!"); } String hostName = address.getHostName(); try { workerId = Long.valueOf(hostName.replace(hostName.replaceAll("\\d+$", ""), "")); } catch (final NumberFormatException e) { throw new IllegalArgumentException(String.format("Wrong hostname:%s, hostname must be end with number!", hostName)); } CommonSelfIdGenerator.setWorkerId(workerId); } @Override public Number generateId() { return commonSelfIdGenerator.generateId(); } }