Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
1ad2ad8c
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1ad2ad8c
编写于
12月 02, 2016
作者:
D
DonneyYoung
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add HostNameIdGenerator.
上级
de6bf4f6
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
203 addition
and
1 deletion
+203
-1
pom.xml
pom.xml
+19
-0
sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/pom.xml
...-generator-parent/sharding-jdbc-self-id-generator/pom.xml
+15
-0
sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/HostNameIdGenerator.java
...e/rdb/sharding/id/generator/self/HostNameIdGenerator.java
+51
-0
sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/AllTest.java
...gdang/ddframe/rdb/sharding/id/generator/self/AllTest.java
+2
-1
sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/HostNameIdGeneratorTest.java
...b/sharding/id/generator/self/HostNameIdGeneratorTest.java
+116
-0
未找到文件。
pom.xml
浏览文件 @
1ad2ad8c
...
...
@@ -71,6 +71,7 @@
<taglist-maven-plugin.version>
2.4
</taglist-maven-plugin.version>
<maven-gpg-plugin.version>
1.6
</maven-gpg-plugin.version>
<javadocExecutable>
${java.home}/../bin/javadoc
</javadocExecutable>
<powermock.version>
1.6.6
</powermock.version>
</properties>
<dependencyManagement>
...
...
@@ -268,6 +269,24 @@
<artifactId>
elastic-job-core
</artifactId>
<version>
${elastic-job.version}
</version>
</dependency>
<dependency>
<groupId>
org.mockito
</groupId>
<artifactId>
mockito-all
</artifactId>
<version>
${mockito.version}
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.powermock
</groupId>
<artifactId>
powermock-api-mockito
</artifactId>
<version>
${powermock.version}
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.powermock
</groupId>
<artifactId>
powermock-module-junit4
</artifactId>
<version>
${powermock.version}
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
</dependencyManagement>
...
...
sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/pom.xml
浏览文件 @
1ad2ad8c
...
...
@@ -30,5 +30,20 @@
<artifactId>
jcl-over-slf4j
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.powermock
</groupId>
<artifactId>
powermock-api-mockito
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.mockito
</groupId>
<artifactId>
mockito-all
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.powermock
</groupId>
<artifactId>
powermock-module-junit4
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/main/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/HostNameIdGenerator.java
0 → 100644
浏览文件 @
1ad2ad8c
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。
*
* @author DonneyYoung
**/
@Getter
@Slf4j
public
class
HostNameIdGenerator
implements
IdGenerator
{
private
static
final
CommonSelfIdGenerator
COMMON_SELF_ID_GENERATOR
;
static
{
COMMON_SELF_ID_GENERATOR
=
new
CommonSelfIdGenerator
();
initWorkerId
();
}
static
void
initWorkerId
()
{
InetAddress
addr
;
Long
workerId
;
try
{
addr
=
InetAddress
.
getLocalHost
();
}
catch
(
final
UnknownHostException
e
)
{
throw
new
IllegalStateException
(
"Cannot get LocalHost InetAddress , please check your network!"
);
}
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!"
);
}
CommonSelfIdGenerator
.
setWorkerId
(
workerId
);
}
@Override
public
Number
generateId
()
{
return
COMMON_SELF_ID_GENERATOR
.
generateId
();
}
}
sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/AllTest.java
浏览文件 @
1ad2ad8c
...
...
@@ -25,7 +25,8 @@ import org.junit.runners.Suite;
ApiWorkerIdTest
.
class
,
SystemPropertyWorkerIdTest
.
class
,
SystemEnvWorkerIdTest
.
class
,
CommonSelfIdGeneratorTest
.
class
CommonSelfIdGeneratorTest
.
class
,
HostNameIdGeneratorTest
.
class
})
public
class
AllTest
{
}
sharding-jdbc-id-generator-parent/sharding-jdbc-self-id-generator/src/test/java/com/dangdang/ddframe/rdb/sharding/id/generator/self/HostNameIdGeneratorTest.java
0 → 100644
浏览文件 @
1ad2ad8c
/*
* 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
org.junit.Before
;
import
org.junit.FixMethodOrder
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.MethodSorters
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
static
org
.
hamcrest
.
core
.
Is
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
@RunWith
(
PowerMockRunner
.
class
)
@PrepareForTest
({
HostNameIdGenerator
.
class
})
@FixMethodOrder
(
MethodSorters
.
NAME_ASCENDING
)
public
class
HostNameIdGeneratorTest
{
@Rule
public
ExpectedException
exception
=
ExpectedException
.
none
();
private
InetAddress
rightAddr
;
private
InetAddress
wrongAddr
;
@Before
public
void
init
()
throws
UnknownHostException
{
int
ipv4Int
=
-
1062731412
;
byte
[]
ipv4Byte
=
new
byte
[
4
];
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
ipv4Byte
[
i
]
=
(
byte
)
(
ipv4Int
>>>
(
24
-
i
*
8
));
}
rightAddr
=
InetAddress
.
getByAddress
(
"dangdang-db-sharding-dev-233"
,
ipv4Byte
);
wrongAddr
=
InetAddress
.
getByAddress
(
"dangdang-db-sharding-dev"
,
ipv4Byte
);
PowerMockito
.
mockStatic
(
InetAddress
.
class
);
PowerMockito
.
when
(
InetAddress
.
getLocalHost
()).
thenReturn
(
rightAddr
);
new
HostNameIdGenerator
();
}
@Test
public
void
testRightHostName
()
throws
UnknownHostException
{
PowerMockito
.
mockStatic
(
InetAddress
.
class
);
PowerMockito
.
when
(
InetAddress
.
getLocalHost
()).
thenReturn
(
rightAddr
);
HostNameIdGenerator
.
initWorkerId
();
HostNameIdGenerator
idGenerator
=
new
HostNameIdGenerator
();
assertThat
(
CommonSelfIdGenerator
.
getWorkerId
(),
is
(
233L
));
}
@Test
public
void
testUnknownHost
()
throws
UnknownHostException
{
PowerMockito
.
mockStatic
(
InetAddress
.
class
);
PowerMockito
.
when
(
InetAddress
.
getLocalHost
()).
thenThrow
(
new
UnknownHostException
());
exception
.
expect
(
IllegalStateException
.
class
);
exception
.
expectMessage
(
"Cannot get LocalHost InetAddress , please check your network!"
);
HostNameIdGenerator
.
initWorkerId
();
}
@Test
public
void
testWrongHostName
()
throws
UnknownHostException
{
PowerMockito
.
mockStatic
(
InetAddress
.
class
);
PowerMockito
.
when
(
InetAddress
.
getLocalHost
()).
thenReturn
(
wrongAddr
);
exception
.
expect
(
IllegalArgumentException
.
class
);
exception
.
expectMessage
(
"Wrong hostname , hostname must be end with number!"
);
HostNameIdGenerator
.
initWorkerId
();
}
@Test
public
void
generateId
()
throws
Exception
{
PowerMockito
.
mockStatic
(
InetAddress
.
class
);
PowerMockito
.
when
(
InetAddress
.
getLocalHost
()).
thenReturn
(
rightAddr
);
HostNameIdGenerator
.
initWorkerId
();
int
threadNumber
=
Runtime
.
getRuntime
().
availableProcessors
()
<<
1
;
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadNumber
);
final
int
taskNumber
=
threadNumber
<<
2
;
final
HostNameIdGenerator
idGenerator
=
new
HostNameIdGenerator
();
Set
<
Long
>
hashSet
=
new
HashSet
<>();
for
(
int
i
=
0
;
i
<
taskNumber
;
i
++)
{
hashSet
.
add
(
executor
.
submit
(
new
Callable
<
Long
>()
{
@Override
public
Long
call
()
throws
Exception
{
return
(
Long
)
idGenerator
.
generateId
();
}
}).
get
());
}
assertThat
(
hashSet
.
size
(),
is
(
taskNumber
));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录