Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
项目统一框架
amos-boot-biz
Commits
d9420f49
Commit
d9420f49
authored
Dec 04, 2023
by
Lambertliu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(sharding):解决分库分表多数据源集成版本冲突问题
parent
35c0c347
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
141 additions
and
6 deletions
+141
-6
pom.xml
...t-module-tzspatrol/amos-boot-module-tzspatrol-biz/pom.xml
+11
-0
RepositoryTs.java
...oin/amos/patrol/business/dao/repository/RepositoryTs.java
+8
-0
PlanTaskServiceImpl.java
...mos/patrol/business/service/impl/PlanTaskServiceImpl.java
+5
-5
RepositoryTImpl.java
...in/amos/patrol/business/service/impl/RepositoryTImpl.java
+24
-1
DataSourceConfiguration.java
...m/yeejoin/amos/patrol/config/DataSourceConfiguration.java
+65
-0
DataSourceHealthConfig.java
...om/yeejoin/amos/patrol/config/DataSourceHealthConfig.java
+28
-0
application-dev.properties
...spatrol-biz/src/main/resources/application-dev.properties
+0
-0
No files found.
amos-boot-system-tzs/amos-boot-module-tzspatrol/amos-boot-module-tzspatrol-biz/pom.xml
View file @
d9420f49
...
@@ -80,6 +80,17 @@
...
@@ -80,6 +80,17 @@
</exclusion>
</exclusion>
</exclusions>
</exclusions>
</dependency>
</dependency>
<dependency>
<groupId>
com.baomidou
</groupId>
<artifactId>
dynamic-datasource-spring-boot-starter
</artifactId>
<version>
4.2.0
</version>
<scope>
compile
</scope>
</dependency>
<dependency>
<groupId>
org.apache.shardingsphere
</groupId>
<artifactId>
shardingsphere-jdbc-core-spring-boot-starter
</artifactId>
<version>
5.2.1
</version>
</dependency>
<!-- 添加fastjson 依赖包. -->
<!-- 添加fastjson 依赖包. -->
<dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<groupId>
com.alibaba
</groupId>
...
...
amos-boot-system-tzs/amos-boot-module-tzspatrol/amos-boot-module-tzspatrol-biz/src/main/java/com/yeejoin/amos/patrol/business/dao/repository/RepositoryTs.java
View file @
d9420f49
...
@@ -7,6 +7,14 @@ public interface RepositoryTs {
...
@@ -7,6 +7,14 @@ public interface RepositoryTs {
// 批量存储的方法
// 批量存储的方法
<
S
>
Iterable
<
S
>
batchSave
(
Iterable
<
S
>
var1
);
<
S
>
Iterable
<
S
>
batchSave
(
Iterable
<
S
>
var1
);
/**
* 分库分表数据源
* @param var1
* @param <S>
* @return
*/
<
S
>
Iterable
<
S
>
batchSaveBySharding
(
Iterable
<
S
>
var1
);
// 批量更新的方法
// 批量更新的方法
<
S
>
Iterable
<
S
>
batchUpdate
(
Iterable
<
S
>
var1
);
<
S
>
Iterable
<
S
>
batchUpdate
(
Iterable
<
S
>
var1
);
<
S
>
Iterable
<
S
>
batchSaveNoAsync
(
Iterable
<
S
>
var1
);
<
S
>
Iterable
<
S
>
batchSaveNoAsync
(
Iterable
<
S
>
var1
);
...
...
amos-boot-system-tzs/amos-boot-module-tzspatrol/amos-boot-module-tzspatrol-biz/src/main/java/com/yeejoin/amos/patrol/business/service/impl/PlanTaskServiceImpl.java
View file @
d9420f49
...
@@ -2440,7 +2440,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
...
@@ -2440,7 +2440,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
return
d
;
return
d
;
}).
collect
(
Collectors
.
toList
());
}).
collect
(
Collectors
.
toList
());
repositoryTs
.
batchSave
(
collect
);
repositoryTs
.
batchSave
BySharding
(
collect
);
log
.
info
(
"归档p_plan_task表完成========"
);
log
.
info
(
"归档p_plan_task表完成========"
);
}
}
...
@@ -2453,7 +2453,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
...
@@ -2453,7 +2453,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
BeanUtils
.
copyProperties
(
e
,
d
);
BeanUtils
.
copyProperties
(
e
,
d
);
return
d
;
return
d
;
}).
collect
(
Collectors
.
toList
());
}).
collect
(
Collectors
.
toList
());
repositoryTs
.
batchSave
(
collect
);
repositoryTs
.
batchSave
BySharding
(
collect
);
log
.
info
(
"归档p_plan_task_detail表完成========"
);
log
.
info
(
"归档p_plan_task_detail表完成========"
);
}
}
...
@@ -2466,7 +2466,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
...
@@ -2466,7 +2466,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
BeanUtils
.
copyProperties
(
e
,
d
);
BeanUtils
.
copyProperties
(
e
,
d
);
return
d
;
return
d
;
}).
collect
(
Collectors
.
toList
());
}).
collect
(
Collectors
.
toList
());
repositoryTs
.
batchSave
(
collect
);
repositoryTs
.
batchSave
BySharding
(
collect
);
log
.
info
(
"归档p_check表完成========"
);
log
.
info
(
"归档p_check表完成========"
);
}
}
...
@@ -2479,7 +2479,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
...
@@ -2479,7 +2479,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
BeanUtils
.
copyProperties
(
e
,
d
);
BeanUtils
.
copyProperties
(
e
,
d
);
return
d
;
return
d
;
}).
collect
(
Collectors
.
toList
());
}).
collect
(
Collectors
.
toList
());
repositoryTs
.
batchSave
(
collect
);
repositoryTs
.
batchSave
BySharding
(
collect
);
log
.
info
(
"归档p_check_input表完成========"
);
log
.
info
(
"归档p_check_input表完成========"
);
}
}
...
@@ -2493,7 +2493,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
...
@@ -2493,7 +2493,7 @@ public class PlanTaskServiceImpl implements IPlanTaskService {
return
d
;
return
d
;
}).
collect
(
Collectors
.
toList
());
}).
collect
(
Collectors
.
toList
());
repositoryTs
.
batchSave
(
collect
);
repositoryTs
.
batchSave
BySharding
(
collect
);
log
.
info
(
"归档p_check_shot表完成========"
);
log
.
info
(
"归档p_check_shot表完成========"
);
}
}
...
...
amos-boot-system-tzs/amos-boot-module-tzspatrol/amos-boot-module-tzspatrol-biz/src/main/java/com/yeejoin/amos/patrol/business/service/impl/RepositoryTImpl.java
View file @
d9420f49
package
com
.
yeejoin
.
amos
.
patrol
.
business
.
service
.
impl
;
package
com
.
yeejoin
.
amos
.
patrol
.
business
.
service
.
impl
;
import
com.baomidou.dynamic.datasource.annotation.DS
;
import
com.yeejoin.amos.patrol.business.dao.repository.RepositoryTs
;
import
com.yeejoin.amos.patrol.business.dao.repository.RepositoryTs
;
import
com.yeejoin.amos.patrol.config.DataSourceConfiguration
;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.scheduling.annotation.Async
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.transaction.annotation.Transactional
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManager
;
import
javax.persistence.PersistenceContext
;
import
javax.persistence.PersistenceContext
;
import
java.util.Iterator
;
import
java.util.Iterator
;
...
@@ -43,6 +44,28 @@ public class RepositoryTImpl implements RepositoryTs {
...
@@ -43,6 +44,28 @@ public class RepositoryTImpl implements RepositoryTs {
}
}
@Override
@Override
@Transactional
@Async
(
"asyncServiceExecutor"
)
@DS
(
DataSourceConfiguration
.
SHARDING_DATA_SOURCE_NAME
)
public
<
S
>
Iterable
<
S
>
batchSaveBySharding
(
Iterable
<
S
>
var1
)
{
Iterator
<
S
>
iterator
=
var1
.
iterator
();
int
index
=
0
;
while
(
iterator
.
hasNext
()){
em
.
persist
(
iterator
.
next
());
index
++;
if
(
index
%
BATCH_SIZE
==
0
){
em
.
flush
();
em
.
clear
();
}
}
if
(
index
%
BATCH_SIZE
!=
0
){
em
.
flush
();
em
.
clear
();
}
return
var1
;
}
@Override
public
<
S
>
Iterable
<
S
>
batchUpdate
(
Iterable
<
S
>
var1
)
{
public
<
S
>
Iterable
<
S
>
batchUpdate
(
Iterable
<
S
>
var1
)
{
Iterator
<
S
>
iterator
=
var1
.
iterator
();
Iterator
<
S
>
iterator
=
var1
.
iterator
();
int
index
=
0
;
int
index
=
0
;
...
...
amos-boot-system-tzs/amos-boot-module-tzspatrol/amos-boot-module-tzspatrol-biz/src/main/java/com/yeejoin/amos/patrol/config/DataSourceConfiguration.java
0 → 100644
View file @
d9420f49
package
com
.
yeejoin
.
amos
.
patrol
.
config
;
import
com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties
;
import
com.baomidou.dynamic.datasource.DynamicRoutingDataSource
;
import
com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator
;
import
com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider
;
import
com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Primary
;
import
javax.sql.DataSource
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author LiuLin
* @date 2023年12月01日 09:57
*/
@Configuration
public
class
DataSourceConfiguration
{
private
final
DynamicDataSourceProperties
properties
;
private
final
DefaultDataSourceCreator
dataSourceCreator
;
private
final
DataSource
shardingSphereDataSource
;
public
static
final
String
SHARDING_DATA_SOURCE_NAME
=
"gits_sharding"
;
public
DataSourceConfiguration
(
DynamicDataSourceProperties
properties
,
DefaultDataSourceCreator
dataSourceCreator
,
@Qualifier
(
"shardingSphereDataSource"
)
DataSource
shardingSphereDataSource
)
{
this
.
properties
=
properties
;
this
.
dataSourceCreator
=
dataSourceCreator
;
this
.
shardingSphereDataSource
=
shardingSphereDataSource
;
}
@Bean
public
DynamicDataSourceProvider
dynamicDataSourceProvider
()
{
return
new
AbstractDataSourceProvider
(
dataSourceCreator
)
{
@Override
public
Map
<
String
,
DataSource
>
loadDataSources
()
{
Map
<
String
,
DataSource
>
dataSourceMap
=
new
HashMap
<>();
dataSourceMap
.
put
(
SHARDING_DATA_SOURCE_NAME
,
shardingSphereDataSource
);
return
dataSourceMap
;
}
};
}
/**
* 将dynamic-datasource设置为首选
* 当Spring存在多个数据源时,自动注入首选对象
* 设置为主要数据源之后,就可以支持shardingSphere原生的配置方式了
*/
@Primary
@Bean
public
DataSource
dataSource
(
List
<
DynamicDataSourceProvider
>
providers
)
{
DynamicRoutingDataSource
dataSource
=
new
DynamicRoutingDataSource
(
providers
);
dataSource
.
setPrimary
(
properties
.
getPrimary
());
dataSource
.
setStrict
(
properties
.
getStrict
());
dataSource
.
setStrategy
(
properties
.
getStrategy
());
dataSource
.
setP6spy
(
properties
.
getP6spy
());
dataSource
.
setSeata
(
properties
.
getSeata
());
return
dataSource
;
}
}
\ No newline at end of file
amos-boot-system-tzs/amos-boot-module-tzspatrol/amos-boot-module-tzspatrol-biz/src/main/java/com/yeejoin/amos/patrol/config/DataSourceHealthConfig.java
0 → 100644
View file @
d9420f49
package
com
.
yeejoin
.
amos
.
patrol
.
config
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration
;
import
org.springframework.boot.actuate.health.AbstractHealthIndicator
;
import
org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator
;
import
org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.util.StringUtils
;
import
javax.sql.DataSource
;
import
java.util.Map
;
@Configuration
public
class
DataSourceHealthConfig
extends
DataSourceHealthContributorAutoConfiguration
{
public
DataSourceHealthConfig
(
Map
<
String
,
DataSource
>
dataSources
,
ObjectProvider
<
DataSourcePoolMetadataProvider
>
metadataProviders
)
{
super
(
dataSources
,
metadataProviders
);
}
@Override
protected
AbstractHealthIndicator
createIndicator
(
DataSource
source
)
{
DataSourceHealthIndicator
indicator
=
(
DataSourceHealthIndicator
)
super
.
createIndicator
(
source
);
if
(!
StringUtils
.
hasText
(
indicator
.
getQuery
()))
{
indicator
.
setQuery
(
"select 1"
);
}
return
indicator
;
}
}
\ No newline at end of file
amos-boot-system-tzs/amos-boot-module-tzspatrol/amos-boot-module-tzspatrol-biz/src/main/resources/application-dev.properties
View file @
d9420f49
This diff is collapsed.
Click to expand it.
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