使用Camunda配置双数据源不是必须的,因为Camunda表的命名方式和自己的方式有区别,而且放在一起显得比较乱,所以将Camunda的表单独存放。

首先多数据源配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
spring:
datasource:
# 主数据源配置
primary:
# 我这用了P6spy,如果你本地没用需要替换成你自己的驱动
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
# 协议也需要换
url: jdbc:p6spy:mariadb://192.168.10.65:3306/am_server?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
username: root
password: 123456
# Canmunda数据源配置
camunda:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mariadb://192.168.10.65:3306/camunda?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
username: root
password: 123456

然后新建一个配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
* 多数据源配置
*
* @author 阮雪峰
*/
@Slf4j
@Configuration
public class DataSourceConfig {

@Primary
@Bean(name = "primaryDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSourceProperties primaryDataSourceProperties() {
// 引入主数据源
return new DataSourceProperties();
}

/**
* 主数据源配置
*
* @return DataSource
*/
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary.hikari")
public DataSource primaryDataSource() {
// 获得 DataSourceProperties 对象
DataSourceProperties properties = this.primaryDataSourceProperties();
// 创建 HikariDataSource 对象
return createHikariDataSource(properties);
}

@Bean(name = "camundaDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.camunda")
public DataSourceProperties camundaDataSourceProperties() {
return new DataSourceProperties();
}

/**
* Canmunda数据源
*
* @return DataSource
*/
@Bean(name = "camundaBpmDataSource")
@ConfigurationProperties(prefix = "spring.datasource.camunda.hikari")
public DataSource camundaDataSource() {
log.info("---工作流数据源加载成功!---");
// 获得 DataSourceProperties 对象
DataSourceProperties properties = this.camundaDataSourceProperties();
// 创建 HikariDataSource 对象
return createHikariDataSource(properties);
}

private static HikariDataSource createHikariDataSource(DataSourceProperties properties) {
// 创建 HikariDataSource 对象
HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
// 设置线程池名
if (StringUtils.hasText(properties.getName())) {
dataSource.setPoolName(properties.getName());
}
return dataSource;
}
}

启动项目。