Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
T
traffic-front
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangxiaoming
traffic-front
Commits
c3599816
Commit
c3599816
authored
Jun 06, 2018
by
eddie.woo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modify
parent
33d528dd
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
257 additions
and
0 deletions
+257
-0
DataSourceConfig.java
.../pwc/taxtech/atms/common/datasource/DataSourceConfig.java
+76
-0
DataSourceFactory.java
...pwc/taxtech/atms/common/datasource/DataSourceFactory.java
+54
-0
ShardingContextHolder.java
...taxtech/atms/common/datasource/ShardingContextHolder.java
+23
-0
DataSourcePoolService.java
.../pwc/taxtech/atms/service/impl/DataSourcePoolService.java
+96
-0
conf.properties
atms-api/src/main/resources/conf/conf.properties
+2
-0
conf_profile_dev.properties
atms-api/src/main/resources/conf/conf_profile_dev.properties
+2
-0
conf_profile_pub.properties
atms-api/src/main/resources/conf/conf_profile_pub.properties
+2
-0
conf_profile_staging.properties
...i/src/main/resources/conf/conf_profile_staging.properties
+2
-0
No files found.
atms-api/src/main/java/pwc/taxtech/atms/common/datasource/DataSourceConfig.java
0 → 100644
View file @
c3599816
package
pwc
.
taxtech
.
atms
.
common
.
datasource
;
public
class
DataSourceConfig
{
private
String
url
;
private
String
username
;
private
String
password
;
private
Integer
clientTimeout
;
private
Integer
threadNum
;
private
Integer
transferFileType
;
private
boolean
renameUploaded
;
private
Integer
retryTimes
;
public
String
getUrl
()
{
return
url
;
}
public
void
setUrl
(
String
url
)
{
this
.
url
=
url
;
}
public
String
getUsername
()
{
return
username
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
public
String
getPassword
()
{
return
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
public
Integer
getClientTimeout
()
{
return
clientTimeout
;
}
public
void
setClientTimeout
(
Integer
clientTimeout
)
{
this
.
clientTimeout
=
clientTimeout
;
}
public
Integer
getThreadNum
()
{
return
threadNum
;
}
public
void
setThreadNum
(
Integer
threadNum
)
{
this
.
threadNum
=
threadNum
;
}
public
Integer
getTransferFileType
()
{
return
transferFileType
;
}
public
void
setTransferFileType
(
Integer
transferFileType
)
{
this
.
transferFileType
=
transferFileType
;
}
public
boolean
isRenameUploaded
()
{
return
renameUploaded
;
}
public
void
setRenameUploaded
(
boolean
renameUploaded
)
{
this
.
renameUploaded
=
renameUploaded
;
}
public
Integer
getRetryTimes
()
{
return
retryTimes
;
}
public
void
setRetryTimes
(
Integer
retryTimes
)
{
this
.
retryTimes
=
retryTimes
;
}
}
atms-api/src/main/java/pwc/taxtech/atms/common/datasource/DataSourceFactory.java
0 → 100644
View file @
c3599816
package
pwc
.
taxtech
.
atms
.
common
.
datasource
;
import
com.alibaba.druid.pool.DruidDataSource
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.pool2.KeyedPooledObjectFactory
;
import
org.apache.commons.pool2.PooledObject
;
import
org.apache.commons.pool2.impl.DefaultPooledObject
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
public
class
DataSourceFactory
implements
KeyedPooledObjectFactory
<
String
,
DruidDataSource
>
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
DataSourceFactory
.
class
);
private
DataSourceConfig
dataSourceConfig
;
private
static
final
String
SYMBOL
=
"demo_db_name"
;
public
DataSourceFactory
(
DataSourceConfig
dataSourceConfig
)
{
this
.
dataSourceConfig
=
dataSourceConfig
;
}
@Override
public
PooledObject
<
DruidDataSource
>
makeObject
(
String
s
)
throws
Exception
{
DruidDataSource
dynamicDataSource
=
new
DruidDataSource
();
String
url
=
StringUtils
.
replace
(
dataSourceConfig
.
getUrl
(),
SYMBOL
,
s
);
dynamicDataSource
.
setUrl
(
url
);
dynamicDataSource
.
setUsername
(
dataSourceConfig
.
getUsername
());
dynamicDataSource
.
setPassword
(
dataSourceConfig
.
getPassword
());
return
new
DefaultPooledObject
<>(
dynamicDataSource
);
}
@Override
public
void
destroyObject
(
String
s
,
PooledObject
<
DruidDataSource
>
pooledObject
)
throws
Exception
{
pooledObject
.
getObject
().
close
();
}
@Override
public
boolean
validateObject
(
String
s
,
PooledObject
<
DruidDataSource
>
pooledObject
)
{
try
{
return
pooledObject
.
getObject
().
isEnable
();
}
catch
(
Exception
e
)
{
logger
.
error
(
"Failed to validate DruidDataSource: "
,
e
);
}
return
false
;
}
@Override
public
void
activateObject
(
String
s
,
PooledObject
<
DruidDataSource
>
pooledObject
)
throws
Exception
{
}
@Override
public
void
passivateObject
(
String
s
,
PooledObject
<
DruidDataSource
>
pooledObject
)
throws
Exception
{
}
}
atms-api/src/main/java/pwc/taxtech/atms/common/datasource/ShardingContextHolder.java
0 → 100644
View file @
c3599816
package
pwc
.
taxtech
.
atms
.
common
.
datasource
;
public
class
ShardingContextHolder
{
private
static
final
ThreadLocal
<
String
>
contextHolder
=
new
ThreadLocal
<>();
/**
* 设置数据库名
*
* @param key key
*/
public
static
void
setDataSourceKey
(
String
key
)
{
contextHolder
.
set
(
key
);
}
/**
* 获取数据库名
*
* @return key
*/
public
static
String
getDataSourceKey
()
{
return
contextHolder
.
get
();
}
}
atms-api/src/main/java/pwc/taxtech/atms/service/impl/DataSourcePoolService.java
0 → 100644
View file @
c3599816
package
pwc
.
taxtech
.
atms
.
service
.
impl
;
import
com.alibaba.druid.pool.DruidDataSource
;
import
com.beust.jcommander.ParameterException
;
import
com.google.common.collect.Maps
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.pool2.impl.GenericKeyedObjectPool
;
import
org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig
;
import
org.apache.ibatis.session.SqlSessionFactory
;
import
org.mybatis.spring.SqlSessionFactoryBean
;
import
org.mybatis.spring.SqlSessionTemplate
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.core.io.support.PathMatchingResourcePatternResolver
;
import
org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
;
import
org.springframework.stereotype.Service
;
import
pwc.taxtech.atms.common.datasource.DataSourceConfig
;
import
pwc.taxtech.atms.common.datasource.DataSourceFactory
;
import
pwc.taxtech.atms.common.datasource.ShardingContextHolder
;
import
javax.annotation.PostConstruct
;
import
javax.sql.DataSource
;
@Service
(
"DynamicDataSource"
)
public
class
DataSourcePoolService
extends
AbstractRoutingDataSource
{
private
GenericKeyedObjectPool
<
String
,
DruidDataSource
>
dataSourcePool
;
@Value
(
"${jdbc_url_demo}"
)
private
String
demoUrl
;
@Value
(
"${jdbc_user}"
)
private
String
jdbcUser
;
@Value
(
"${jdbc_password}"
)
private
String
jdbcPwd
;
@PostConstruct
public
void
init
()
throws
Exception
{
DataSourceConfig
config
=
new
DataSourceConfig
();
config
.
setUrl
(
demoUrl
);
config
.
setUsername
(
jdbcUser
);
config
.
setPassword
(
jdbcPwd
);
GenericKeyedObjectPoolConfig
poolConfig
=
new
GenericKeyedObjectPoolConfig
();
poolConfig
.
setTestOnBorrow
(
true
);
poolConfig
.
setTestOnReturn
(
true
);
dataSourcePool
=
new
GenericKeyedObjectPool
<>(
new
DataSourceFactory
(
config
),
poolConfig
);
this
.
setTargetDataSources
(
Maps
.
newHashMap
());
}
@Override
protected
Object
determineCurrentLookupKey
()
{
return
ShardingContextHolder
.
getDataSourceKey
();
}
@Override
protected
DataSource
determineTargetDataSource
()
{
try
{
String
key
=
ShardingContextHolder
.
getDataSourceKey
();
DruidDataSource
dataSource
=
dataSourcePool
.
borrowObject
(
key
);
dataSourcePool
.
returnObject
(
key
,
dataSource
);
return
dataSource
;
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"determineTargetDataSource error."
,
e
);
}
}
private
void
createDataSource
(
String
key
)
{
if
(
StringUtils
.
isBlank
(
key
))
{
throw
new
ParameterException
(
"DataSource name is empty."
);
}
try
{
dataSourcePool
.
addObject
(
key
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"createDataSource error."
,
e
);
}
}
@Bean
public
SqlSessionFactory
dynamicSqlSessionFactory
(
@Qualifier
(
"DynamicDataSource"
)
DataSource
dynamicDataSource
)
throws
Exception
{
SqlSessionFactoryBean
bean
=
new
SqlSessionFactoryBean
();
bean
.
setDataSource
(
dynamicDataSource
);
bean
.
setMapperLocations
(
new
PathMatchingResourcePatternResolver
()
.
getResources
(
"classpath*:mapper/*.xml"
));
return
bean
.
getObject
();
}
@Bean
public
SqlSessionTemplate
dynamicSqlSessionTemplate
(
@Qualifier
(
"dynamicSqlSessionFactory"
)
SqlSessionFactory
sqlSessionFactory
)
throws
Exception
{
return
new
SqlSessionTemplate
(
sqlSessionFactory
);
}
}
atms-api/src/main/resources/conf/conf.properties
View file @
c3599816
...
...
@@ -2,6 +2,8 @@ jdbc_url=${jdbc_url}
jdbc_user
=
${jdbc_user}
jdbc_password
=
${jdbc_password}
jdbc_url_demo
=
${jdbc_url_demo}
mail_jdbc_url
=
${mail_jdbc_url}
mail_jdbc_user
=
${mail_jdbc_user}
mail_jdbc_password
=
${mail_jdbc_password}
...
...
atms-api/src/main/resources/conf/conf_profile_dev.properties
View file @
c3599816
...
...
@@ -2,6 +2,8 @@ jdbc_url=jdbc:mysql://10.157.107.89:3306/tax_admin?useUnicode=true&character
jdbc_user
=
root
jdbc_password
=
tax@Admin2018
jdbc_url_demo
=
jdbc:mysql://10.157.107.89:3306/demo_db_name?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
mail_jdbc_url
=
jdbc:sqlserver://192.168.1.102:1434;DatabaseName=MAILMaster
mail_jdbc_user
=
sa
mail_jdbc_password
=
atmsunittestSQL
...
...
atms-api/src/main/resources/conf/conf_profile_pub.properties
View file @
c3599816
...
...
@@ -2,6 +2,8 @@ jdbc_url=jdbc:sqlserver://192.168.1.102:1434;DatabaseName=QATaxAdmin8
jdbc_user
=
sa
jdbc_password
=
atmsunittestSQL
jdbc_url_demo
=
jdbc:mysql://10.157.107.89:3306/demo_db_name?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
mail_jdbc_url
=
jdbc:sqlserver://192.168.1.102:1434;DatabaseName=MAILMaster
mail_jdbc_user
=
sa
mail_jdbc_password
=
atmsunittestSQL
...
...
atms-api/src/main/resources/conf/conf_profile_staging.properties
View file @
c3599816
...
...
@@ -2,6 +2,8 @@ jdbc_url=jdbc:mysql://10.157.107.89:3306/tax_admin?useUnicode=true&character
jdbc_user
=
root
jdbc_password
=
tax@Admin2018
jdbc_url_demo
=
jdbc:mysql://10.157.107.89:3306/demo_db_name?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
mail_jdbc_url
=
jdbc:sqlserver://192.168.1.102:1434;DatabaseName=MAILMaster
mail_jdbc_user
=
sa
mail_jdbc_password
=
atmsunittestSQL
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment