SpringBoot2添加对Mysql、ClickHouse、Redis的支持

开始事务支持

1
@EnableTransactionManagement

数据源配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#mysql
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/yuewen_bigdata_data_api?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
#clickhouse-本地docker服务搭建的
spring.datasource.clickhouse.jdbc-url=jdbc:clickhouse://127.0.0.1:8123/system
spring.datasource.clickhouse.username=
spring.datasource.clickhouse.password=
#redis
#redis-单点配置
spring.redis.database=0
spring.redis.password=121
spring.redis.host=localhost
spring.redis.port=6379
#redis-cluster配置
#spring.redis.cluster.nodes=12.25:6379,15:6380,15:6381
#redis-哨兵配置
#spring.redis.sentinel.master=yuewen-redis-sentinel
#spring.redis.sentinel.nodes=12.25:6379,15:6380,15:6381

加载配置

DataSourceConfig.java

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
package com.yuewen.bigdata.dataapi.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
* Created by xiehui1956(@)gmail.com on 2020/4/26
*/
@Configuration
public class DataSourceConfig {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Primary
@Bean(name = "masterDataSource")
@Qualifier(value = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource getDefaultDataSource() {
return DataSourceBuilder.create().build();
}

/**
* clickHouse操作模板
*
* @return
*/
@Bean(name = "clickHouseDataSource")
@Qualifier(value = "clickHouseDataSource")
@ConfigurationProperties(prefix = "spring.datasource.clickhouse")
public DataSource getServiceClickHouseDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "masterJdbcTemplate")
public JdbcTemplate masterJdbcTemplate(@Qualifier(value = "masterDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

@Bean(name = "clickHouseJdbcTemplate")
public JdbcTemplate clickHouseJdbcTemplate(@Qualifier(value = "clickHouseDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

Mysql事务管理

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
package com.yuewen.bigdata.dataapi.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
* Created by xiehui1956(@)gmail.com on 2020/4/26
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryMaster"
, transactionManagerRef = "transactionManagerMaster"
, basePackages = {"com.yuewen.bigdata.dataapi.dao"})
public class DataSourceMasterManager {

@Autowired
@Qualifier("masterDataSource")
private DataSource masterDataSource;

@Primary
@Bean(name = "entityManagerFactoryMaster")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(masterDataSource)
.packages("com.yuewen.bigdata.dataapi.entity")
.persistenceUnit("masterDataPersistenceUnit")
.build();
}

@Bean("transactionManagerMaster")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryBean(builder).getObject());
}
}

使用

1
2
3
4
5
6
7
8
9
@Resource
@Qualifier(value = "clickHouseJdbcTemplate")
private JdbcTemplate jdbcTemplate;

@GetMapping("/tt")
public CommonResult test(CommonResult commonResult) {
List<Map<String, Object>> show_tables = jdbcTemplate.queryForList("show tables");
return commonResult.setData(show_tables.stream().limit(10).collect(Collectors.toList()));
}

响应结果

图片