Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
ec8adab1
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,体验更适合开发者的 AI 搜索 >>
提交
ec8adab1
编写于
10月 10, 2017
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add props for spring boot starter
上级
21816e5b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
44 addition
and
46 deletion
+44
-46
sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/main/java/io/shardingjdbc/spring/boot/SpringBootConfiguration.java
.../io/shardingjdbc/spring/boot/SpringBootConfiguration.java
+40
-4
sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/main/java/io/shardingjdbc/spring/boot/util/EnvironmentAwareUtil.java
...o/shardingjdbc/spring/boot/util/EnvironmentAwareUtil.java
+0
-42
sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/java/io.shardingjdbc.spring.boot/type/SpringBootShardingTest.java
...shardingjdbc.spring.boot/type/SpringBootShardingTest.java
+1
-0
sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/application-sharding.properties
...tarter/src/test/resources/application-sharding.properties
+3
-0
未找到文件。
sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/main/java/io/shardingjdbc/spring/boot/SpringBootConfiguration.java
浏览文件 @
ec8adab1
package
io.shardingjdbc.spring.boot
;
import
com.google.common.base.Preconditions
;
import
com.google.common.base.Strings
;
import
io.shardingjdbc.core.api.MasterSlaveDataSourceFactory
;
import
io.shardingjdbc.core.api.ShardingDataSourceFactory
;
import
io.shardingjdbc.core.constant.ShardingPropertiesConstant
;
import
io.shardingjdbc.core.exception.ShardingJdbcException
;
import
io.shardingjdbc.core.util.DataSourceUtil
;
import
io.shardingjdbc.spring.boot.masterslave.SpringBootMasterSlaveRuleConfigurationProperties
;
import
io.shardingjdbc.spring.boot.sharding.SpringBootShardingRuleConfigurationProperties
;
import
io.shardingjdbc.spring.boot.util.EnvironmentAwareUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.bind.RelaxedPropertyResolver
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.EnvironmentAware
;
import
org.springframework.context.annotation.Bean
;
...
...
@@ -16,6 +21,7 @@ import javax.sql.DataSource;
import
java.sql.SQLException
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Properties
;
/**
* Spring boot sharding and master-slave configuration.
...
...
@@ -32,16 +38,46 @@ public class SpringBootConfiguration implements EnvironmentAware {
@Autowired
private
SpringBootMasterSlaveRuleConfigurationProperties
masterSlaveProperties
;
private
Map
<
String
,
DataSource
>
dataSourceMap
=
new
HashMap
<>();
private
final
Map
<
String
,
DataSource
>
dataSourceMap
=
new
HashMap
<>();
private
final
Properties
props
=
new
Properties
();
@Bean
public
DataSource
dataSource
()
throws
SQLException
{
return
null
==
masterSlaveProperties
.
getMasterDataSourceName
()
?
ShardingDataSourceFactory
.
createDataSource
(
dataSourceMap
,
shardingProperties
.
getShardingRuleConfiguration
())
return
null
==
masterSlaveProperties
.
getMasterDataSourceName
()
?
ShardingDataSourceFactory
.
createDataSource
(
dataSourceMap
,
shardingProperties
.
getShardingRuleConfiguration
()
,
props
)
:
MasterSlaveDataSourceFactory
.
createDataSource
(
dataSourceMap
,
masterSlaveProperties
.
getMasterSlaveRuleConfiguration
());
}
@Override
public
void
setEnvironment
(
final
Environment
environment
)
{
EnvironmentAwareUtil
.
setDataSourceMap
(
dataSourceMap
,
environment
);
setDataSourceMap
(
environment
);
setShardingProperties
(
environment
);
}
private
void
setDataSourceMap
(
final
Environment
environment
)
{
RelaxedPropertyResolver
propertyResolver
=
new
RelaxedPropertyResolver
(
environment
,
"sharding.jdbc.datasource."
);
String
dataSources
=
propertyResolver
.
getProperty
(
"names"
);
for
(
String
each
:
dataSources
.
split
(
","
))
{
try
{
Map
<
String
,
Object
>
dataSourceProps
=
propertyResolver
.
getSubProperties
(
each
+
"."
);
Preconditions
.
checkState
(!
dataSourceProps
.
isEmpty
(),
"Wrong datasource properties!"
);
DataSource
dataSource
=
DataSourceUtil
.
getDataSource
(
dataSourceProps
.
get
(
"type"
).
toString
(),
dataSourceProps
);
dataSourceMap
.
put
(
each
,
dataSource
);
}
catch
(
final
ReflectiveOperationException
ex
)
{
throw
new
ShardingJdbcException
(
"Can't find datasource type!"
,
ex
);
}
}
}
private
void
setShardingProperties
(
final
Environment
environment
)
{
RelaxedPropertyResolver
propertyResolver
=
new
RelaxedPropertyResolver
(
environment
,
"sharding.jdbc.config.sharding.props."
);
String
showSQL
=
propertyResolver
.
getProperty
(
ShardingPropertiesConstant
.
SQL_SHOW
.
getKey
());
if
(!
Strings
.
isNullOrEmpty
(
showSQL
))
{
props
.
setProperty
(
ShardingPropertiesConstant
.
SQL_SHOW
.
getKey
(),
showSQL
);
}
String
executorSize
=
propertyResolver
.
getProperty
(
ShardingPropertiesConstant
.
EXECUTOR_SIZE
.
getKey
());
if
(!
Strings
.
isNullOrEmpty
(
executorSize
))
{
props
.
setProperty
(
ShardingPropertiesConstant
.
EXECUTOR_SIZE
.
getKey
(),
executorSize
);
}
}
}
sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/main/java/io/shardingjdbc/spring/boot/util/EnvironmentAwareUtil.java
已删除
100644 → 0
浏览文件 @
21816e5b
package
io.shardingjdbc.spring.boot.util
;
import
com.google.common.base.Preconditions
;
import
io.shardingjdbc.core.exception.ShardingJdbcException
;
import
io.shardingjdbc.core.util.DataSourceUtil
;
import
lombok.AccessLevel
;
import
lombok.NoArgsConstructor
;
import
org.springframework.boot.bind.RelaxedPropertyResolver
;
import
org.springframework.core.env.Environment
;
import
javax.sql.DataSource
;
import
java.util.Map
;
/**
* Sharding jdbc spring boot environment aware util.
*
* @author caohao
*/
@NoArgsConstructor
(
access
=
AccessLevel
.
PRIVATE
)
public
final
class
EnvironmentAwareUtil
{
/**
* Set data source map.
*
* @param dataSourceMap data source map
* @param environment environment for spring boot
*/
public
static
void
setDataSourceMap
(
final
Map
<
String
,
DataSource
>
dataSourceMap
,
final
Environment
environment
)
{
RelaxedPropertyResolver
propertyResolver
=
new
RelaxedPropertyResolver
(
environment
,
"sharding.jdbc.datasource."
);
String
dataSources
=
propertyResolver
.
getProperty
(
"names"
);
for
(
String
each
:
dataSources
.
split
(
","
))
{
try
{
Map
<
String
,
Object
>
dataSourceProps
=
propertyResolver
.
getSubProperties
(
each
+
"."
);
Preconditions
.
checkState
(!
dataSourceProps
.
isEmpty
(),
"Wrong datasource properties!"
);
DataSource
dataSource
=
DataSourceUtil
.
getDataSource
(
dataSourceProps
.
get
(
"type"
).
toString
(),
dataSourceProps
);
dataSourceMap
.
put
(
each
,
dataSource
);
}
catch
(
final
ReflectiveOperationException
ex
)
{
throw
new
ShardingJdbcException
(
"Can't find datasource type!"
,
ex
);
}
}
}
}
sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/java/io.shardingjdbc.spring.boot/type/SpringBootShardingTest.java
浏览文件 @
ec8adab1
...
...
@@ -36,5 +36,6 @@ public class SpringBootShardingTest {
for
(
DataSource
each
:
shardingContext
.
getShardingRule
().
getDataSourceMap
().
values
())
{
assertThat
(((
BasicDataSource
)
each
).
getMaxActive
(),
is
(
16
));
}
assertTrue
(
shardingContext
.
isShowSQL
());
}
}
sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/application-sharding.properties
浏览文件 @
ec8adab1
...
...
@@ -31,3 +31,6 @@ sharding.jdbc.config.sharding.tables.t_order_item.actualDataNodes=ds_${0..1}.t_o
sharding.jdbc.config.sharding.tables.t_order_item.tableStrategy.inline.shardingColumn
=
order_id
sharding.jdbc.config.sharding.tables.t_order_item.tableStrategy.inline.algorithmInlineExpression
=
t_order_item_${order_id % 2}
sharding.jdbc.config.sharding.tables.t_order_item.keyGeneratorColumnName
=
order_item_id
sharding.jdbc.config.sharding.props.sql.show
=
true
sharding.jdbc.config.sharding.props.executor.size
=
100
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录