SpringBoot实现mysql与clickhouse多数据源的项目实践
更新时间:2023年11月09日 10:44:59 作者:爱上雪茄
本文主要介绍了SpringBoot实现mysql与clickhouse多数据源的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
一、我们来实现一个mysql与clickhouse多数据源配置
二、数据源配置
# 指定服务名称 spring: application: name: demobigdata datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/db?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useAffectedRows=true&allowPublicKeyRetrieval=true username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource druid: # 数据源 clickhouse clickhouse: driverClassName: com.clickhouse.jdbc.ClickHouseDriver url: jdbc:clickhouse://192.168.42.142:8123/bigdata username: default password: 123456 initialSize: 10 maxActive: 100 minIdle: 10 maxWait: 6000
三、MysqlDuridConfig
import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; 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 org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.alibaba.druid.pool.DruidDataSource; @Configuration @MapperScan(basePackages = "org.demoflowable.mysql.dao") public class MysqlDuridConfig { @javax.annotation.Resource MysqlJdbcParamConfig mysqlJdbcParamConfig; @Bean("mysqlDataSource") @Primary public DataSource mysqlDataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(mysqlJdbcParamConfig.getUrl()); datasource.setDriverClassName(mysqlJdbcParamConfig.getDriverClassName()); datasource.setUsername(mysqlJdbcParamConfig.getUsername()); datasource.setPassword(mysqlJdbcParamConfig.getPassword()); return datasource; } @Bean("mysqlTransactionManager") @Primary public DataSourceTransactionManager getDataSourceTransactionManager( @Qualifier("mysqlDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean("sqlSessionFactory") @Primary public SqlSessionFactory getSqlSessionManager(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); Resource[] resource = new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/mapper/mysql/*.xml"); bean.setMapperLocations(resource); Resource configLocation=new PathMatchingResourcePatternResolver().getResource("classpath:META-INF/spring/mybatis-config.xml"); bean.setConfigLocation(configLocation); return bean.getObject(); } }
四、MysqlJdbcParamConfig
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.datasource") public class MysqlJdbcParamConfig { private String driverClassName; private String url; private String username; private String password; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } 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; } }
五、ClickHouseJdbcParamConfig
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.datasource.druid.clickhouse") public class ClickHouseJdbcParamConfig { private String driverClassName; private String url; private Integer initialSize; private Integer maxActive; private Integer minIdle; private Integer maxWait; private String username; private String password; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Integer getInitialSize() { return initialSize; } public void setInitialSize(Integer initialSize) { this.initialSize = initialSize; } public Integer getMaxActive() { return maxActive; } public void setMaxActive(Integer maxActive) { this.maxActive = maxActive; } public Integer getMinIdle() { return minIdle; } public void setMinIdle(Integer minIdle) { this.minIdle = minIdle; } public Integer getMaxWait() { return maxWait; } public void setMaxWait(Integer maxWait) { this.maxWait = maxWait; } 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; } }
六、ClickHouseConfig
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.alibaba.druid.pool.DruidDataSource; @Configuration @ConfigurationProperties @MapperScan(basePackages = "org.demoflowable.clickhouse.dao", sqlSessionFactoryRef = "clickhouseSqlSessionFactory") public class ClickHouseConfig { @javax.annotation.Resource private ClickHouseJdbcParamConfig jdbcParamConfig; @Bean("clickDataSource") public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(jdbcParamConfig.getUrl()); datasource.setDriverClassName(jdbcParamConfig.getDriverClassName()); datasource.setInitialSize(jdbcParamConfig.getInitialSize()); datasource.setMinIdle(jdbcParamConfig.getMinIdle()); datasource.setMaxActive(jdbcParamConfig.getMaxActive()); datasource.setMaxWait(jdbcParamConfig.getMaxWait()); datasource.setUsername(jdbcParamConfig.getUsername()); datasource.setPassword(jdbcParamConfig.getPassword()); return datasource; } @Bean("clickTransactionManager") public DataSourceTransactionManager getDataSourceTransactionManager( @Qualifier("clickDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean("clickhouseSqlSessionFactory") public SqlSessionFactory getSqlSessionManager(@Qualifier("clickDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); Resource[] resource = new PathMatchingResourcePatternResolver() .getResources("classpath:META-INF/mapper/clickhouse/*.xml"); bean.setMapperLocations(resource); return bean.getObject(); } }
七、启动类Application
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @Description: 启动类 */ @SpringBootApplication @ComponentScan({ "org.demoflowable" }) public class Application { /** * @Title: main * @Description: 启动类 * @param args * @date 2023-11-08 01:49:23 */ public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
到此这篇关于SpringBoot实现mysql与clickhouse多数据源的项目实践的文章就介绍到这了,更多相关SpringBoot mysql与clickhouse多数据源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot URL带有特殊字符([]/{}等),报400错误的解决
这篇文章主要介绍了SpringBoot URL带有特殊字符([]/{}等),报400错误的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-02-02
最新评论