Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
6c877971
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,发现更多精彩内容 >>
提交
6c877971
编写于
10月 28, 2019
作者:
P
Pramy
提交者:
Liang Zhang
10月 28, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement MySQL time service (#3343)
Change-Id: I791c1d3ff4c4cbf0f90464c7bb8bb9490dca4019
上级
ebdba014
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
238 addition
and
0 deletion
+238
-0
sharding-core/database-time-service/pom.xml
sharding-core/database-time-service/pom.xml
+46
-0
sharding-core/database-time-service/src/main/java/org/apache/shardingsphere/route/time/exception/TimeServiceInitException.java
...sphere/route/time/exception/TimeServiceInitException.java
+32
-0
sharding-core/database-time-service/src/main/java/org/apache/shardingsphere/route/time/mysql/MySQLTimeService.java
...che/shardingsphere/route/time/mysql/MySQLTimeService.java
+84
-0
sharding-core/database-time-service/src/main/resources/META-INF/services/org.apache.shardingsphere.core.route.spi.TimeService
...ices/org.apache.shardingsphere.core.route.spi.TimeService
+18
-0
sharding-core/database-time-service/src/test/java/org/apache/shardingsphere/route/time/mysql/MySQLTimeServiceTest.java
...shardingsphere/route/time/mysql/MySQLTimeServiceTest.java
+57
-0
sharding-core/pom.xml
sharding-core/pom.xml
+1
-0
未找到文件。
sharding-core/database-time-service/pom.xml
0 → 100644
浏览文件 @
6c877971
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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.
-->
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<groupId>
org.apache.shardingsphere
</groupId>
<artifactId>
sharding-core
</artifactId>
<version>
4.0.0-RC3-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
database-time-service
</artifactId>
<name>
${project.artifactId}
</name>
<dependencies>
<dependency>
<groupId>
org.apache.shardingsphere
</groupId>
<artifactId>
sharding-core-route
</artifactId>
<version>
${project.version}
</version>
</dependency>
<dependency>
<groupId>
com.zaxxer
</groupId>
<artifactId>
HikariCP-java7
</artifactId>
</dependency>
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
sharding-core/database-time-service/src/main/java/org/apache/shardingsphere/route/time/exception/TimeServiceInitException.java
0 → 100644
浏览文件 @
6c877971
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package
org.apache.shardingsphere.route.time.exception
;
/**
* TimeService init exception.
*
* @author chenchuangliu
*/
public
class
TimeServiceInitException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
-
834638295454826244L
;
public
TimeServiceInitException
(
final
String
message
,
final
Throwable
cause
)
{
super
(
message
,
cause
);
}
}
sharding-core/database-time-service/src/main/java/org/apache/shardingsphere/route/time/mysql/MySQLTimeService.java
0 → 100644
浏览文件 @
6c877971
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package
org.apache.shardingsphere.route.time.mysql
;
import
org.apache.shardingsphere.core.route.spi.TimeService
;
import
org.apache.shardingsphere.route.time.exception.TimeServiceInitException
;
import
javax.sql.DataSource
;
import
java.beans.IntrospectionException
;
import
java.beans.PropertyDescriptor
;
import
java.io.IOException
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.Date
;
import
java.util.Properties
;
/**
* MySQL time service.
* Need to create a mysql-time-service.properties file under the classpath.
*
* @author chenchuangliu
*/
public
final
class
MySQLTimeService
implements
TimeService
{
private
static
DataSource
dataSource
;
static
{
init
();
}
private
static
void
init
()
{
try
{
Properties
properties
=
new
Properties
();
properties
.
load
(
MySQLTimeService
.
class
.
getResourceAsStream
(
"/mysql-time-service.properties"
));
if
(
properties
.
isEmpty
())
{
return
;
}
String
dataSourceType
=
(
String
)
properties
.
remove
(
"dataSourceType"
);
Class
dataSourceClass
=
Class
.
forName
(
dataSourceType
);
DataSource
dataSource
=
(
DataSource
)
dataSourceClass
.
newInstance
();
for
(
String
each
:
properties
.
stringPropertyNames
())
{
PropertyDescriptor
propertyDescriptor
=
new
PropertyDescriptor
(
each
,
dataSourceClass
);
Method
writeMethod
=
propertyDescriptor
.
getWriteMethod
();
writeMethod
.
invoke
(
dataSource
,
properties
.
getProperty
(
each
));
}
MySQLTimeService
.
dataSource
=
dataSource
;
}
catch
(
final
NullPointerException
|
IOException
|
ClassNotFoundException
|
InstantiationException
|
IllegalAccessException
|
InvocationTargetException
|
IntrospectionException
e
)
{
throw
new
TimeServiceInitException
(
"please check your mysql-time-service.properties"
,
e
);
}
}
@Override
public
Date
getTime
()
{
if
(
null
!=
dataSource
)
{
try
(
Connection
connection
=
dataSource
.
getConnection
();
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
"select now()"
);
ResultSet
resultSet
=
preparedStatement
.
executeQuery
())
{
resultSet
.
next
();
return
(
Date
)
resultSet
.
getObject
(
1
);
}
catch
(
final
SQLException
ignore
)
{
}
}
return
new
Date
();
}
}
sharding-core/database-time-service/src/main/resources/META-INF/services/org.apache.shardingsphere.core.route.spi.TimeService
0 → 100644
浏览文件 @
6c877971
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
org.apache.shardingsphere.route.time.mysql.MySQLTimeService
sharding-core/database-time-service/src/test/java/org/apache/shardingsphere/route/time/mysql/MySQLTimeServiceTest.java
0 → 100644
浏览文件 @
6c877971
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package
org.apache.shardingsphere.route.time.mysql
;
import
lombok.SneakyThrows
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
javax.sql.DataSource
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.lang.reflect.Field
;
import
java.util.Properties
;
public
final
class
MySQLTimeServiceTest
{
private
final
File
file
=
new
File
(
MySQLTimeServiceTest
.
class
.
getResource
(
"/"
).
getPath
()
+
"mysql-time-service.properties"
);
@Test
public
void
assertInitDataSource
()
throws
IOException
{
FileOutputStream
stream
=
new
FileOutputStream
(
file
);
Properties
properties
=
new
Properties
();
properties
.
put
(
"dataSourceType"
,
"com.zaxxer.hikari.HikariDataSource"
);
properties
.
put
(
"jdbcUrl"
,
"jdbc:mysql://localhost:3306/test"
);
properties
.
put
(
"username"
,
"root"
);
properties
.
put
(
"password"
,
"root"
);
properties
.
put
(
"driverClassName"
,
"com.mysql.jdbc.Driver"
);
properties
.
store
(
stream
,
null
);
stream
.
close
();
MySQLTimeService
service
=
new
MySQLTimeService
();
Assert
.
assertNotNull
(
getDataSource
(
service
));
Assert
.
assertTrue
(
file
.
delete
());
}
@SneakyThrows
private
DataSource
getDataSource
(
final
MySQLTimeService
service
)
{
Field
field
=
service
.
getClass
().
getDeclaredField
(
"dataSource"
);
field
.
setAccessible
(
true
);
return
(
DataSource
)
field
.
get
(
null
);
}
}
sharding-core/pom.xml
浏览文件 @
6c877971
...
@@ -33,6 +33,7 @@
...
@@ -33,6 +33,7 @@
<module>
sharding-core-parse
</module>
<module>
sharding-core-parse
</module>
<module>
sharding-core-preprocessor
</module>
<module>
sharding-core-preprocessor
</module>
<module>
sharding-core-route
</module>
<module>
sharding-core-route
</module>
<module>
database-time-service
</module>
<module>
sharding-core-rewrite
</module>
<module>
sharding-core-rewrite
</module>
<module>
sharding-core-execute
</module>
<module>
sharding-core-execute
</module>
<module>
sharding-core-merge
</module>
<module>
sharding-core-merge
</module>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录