• SpringBoot实现mysql与clickhouse多数据源


    一、我们来实现一个mysql与clickhouse多数据源配置

    二、数据源配置

    1. # 指定服务名称
    2. spring:
    3. application:
    4. name: demobigdata
    5. datasource:
    6. driver-class-name: com.mysql.jdbc.Driver
    7. 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
    8. username: root
    9. password: 123456
    10. type: com.alibaba.druid.pool.DruidDataSource
    11. druid:
    12. # 数据源 clickhouse
    13. clickhouse:
    14. driverClassName: com.clickhouse.jdbc.ClickHouseDriver
    15. url: jdbc:clickhouse://192.168.42.142:8123/bigdata
    16. username: default
    17. password: 123456
    18. initialSize: 10
    19. maxActive: 100
    20. minIdle: 10
    21. maxWait: 6000

    三、MysqlDuridConfig

    1. import javax.sql.DataSource;
    2. import org.apache.ibatis.session.SqlSessionFactory;
    3. import org.mybatis.spring.SqlSessionFactoryBean;
    4. import org.mybatis.spring.annotation.MapperScan;
    5. import org.springframework.beans.factory.annotation.Qualifier;
    6. import org.springframework.context.annotation.Bean;
    7. import org.springframework.context.annotation.Configuration;
    8. import org.springframework.context.annotation.Primary;
    9. import org.springframework.core.io.Resource;
    10. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    11. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    12. import com.alibaba.druid.pool.DruidDataSource;
    13. @Configuration
    14. @MapperScan(basePackages = "org.demoflowable.mysql.dao")
    15. public class MysqlDuridConfig {
    16. @javax.annotation.Resource
    17. MysqlJdbcParamConfig mysqlJdbcParamConfig;
    18. @Bean("mysqlDataSource")
    19. @Primary
    20. public DataSource mysqlDataSource() {
    21. DruidDataSource datasource = new DruidDataSource();
    22. datasource.setUrl(mysqlJdbcParamConfig.getUrl());
    23. datasource.setDriverClassName(mysqlJdbcParamConfig.getDriverClassName());
    24. datasource.setUsername(mysqlJdbcParamConfig.getUsername());
    25. datasource.setPassword(mysqlJdbcParamConfig.getPassword());
    26. return datasource;
    27. }
    28. @Bean("mysqlTransactionManager")
    29. @Primary
    30. public DataSourceTransactionManager getDataSourceTransactionManager(
    31. @Qualifier("mysqlDataSource") DataSource dataSource) {
    32. return new DataSourceTransactionManager(dataSource);
    33. }
    34. @Bean("sqlSessionFactory")
    35. @Primary
    36. public SqlSessionFactory getSqlSessionManager(@Qualifier("mysqlDataSource") DataSource dataSource)
    37. throws Exception {
    38. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    39. bean.setDataSource(dataSource);
    40. Resource[] resource = new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/mapper/mysql/*.xml");
    41. bean.setMapperLocations(resource);
    42. Resource configLocation=new PathMatchingResourcePatternResolver().getResource("classpath:META-INF/spring/mybatis-config.xml");
    43. bean.setConfigLocation(configLocation);
    44. return bean.getObject();
    45. }
    46. }

    四、MysqlJdbcParamConfig

    1. import org.springframework.boot.context.properties.ConfigurationProperties;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. @ConfigurationProperties(prefix = "spring.datasource")
    5. public class MysqlJdbcParamConfig {
    6. private String driverClassName;
    7. private String url;
    8. private String username;
    9. private String password;
    10. public String getDriverClassName() {
    11. return driverClassName;
    12. }
    13. public void setDriverClassName(String driverClassName) {
    14. this.driverClassName = driverClassName;
    15. }
    16. public String getUrl() {
    17. return url;
    18. }
    19. public void setUrl(String url) {
    20. this.url = url;
    21. }
    22. public String getUsername() {
    23. return username;
    24. }
    25. public void setUsername(String username) {
    26. this.username = username;
    27. }
    28. public String getPassword() {
    29. return password;
    30. }
    31. public void setPassword(String password) {
    32. this.password = password;
    33. }
    34. }

    五、ClickHouseJdbcParamConfig

    1. import org.springframework.boot.context.properties.ConfigurationProperties;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. @ConfigurationProperties(prefix = "spring.datasource.druid.clickhouse")
    5. public class ClickHouseJdbcParamConfig {
    6. private String driverClassName;
    7. private String url;
    8. private Integer initialSize;
    9. private Integer maxActive;
    10. private Integer minIdle;
    11. private Integer maxWait;
    12. private String username;
    13. private String password;
    14. public String getDriverClassName() {
    15. return driverClassName;
    16. }
    17. public void setDriverClassName(String driverClassName) {
    18. this.driverClassName = driverClassName;
    19. }
    20. public String getUrl() {
    21. return url;
    22. }
    23. public void setUrl(String url) {
    24. this.url = url;
    25. }
    26. public Integer getInitialSize() {
    27. return initialSize;
    28. }
    29. public void setInitialSize(Integer initialSize) {
    30. this.initialSize = initialSize;
    31. }
    32. public Integer getMaxActive() {
    33. return maxActive;
    34. }
    35. public void setMaxActive(Integer maxActive) {
    36. this.maxActive = maxActive;
    37. }
    38. public Integer getMinIdle() {
    39. return minIdle;
    40. }
    41. public void setMinIdle(Integer minIdle) {
    42. this.minIdle = minIdle;
    43. }
    44. public Integer getMaxWait() {
    45. return maxWait;
    46. }
    47. public void setMaxWait(Integer maxWait) {
    48. this.maxWait = maxWait;
    49. }
    50. public String getUsername() {
    51. return username;
    52. }
    53. public void setUsername(String username) {
    54. this.username = username;
    55. }
    56. public String getPassword() {
    57. return password;
    58. }
    59. public void setPassword(String password) {
    60. this.password = password;
    61. }
    62. }

    六、ClickHouseConfig 

    1. import org.apache.ibatis.session.SqlSessionFactory;
    2. import org.mybatis.spring.SqlSessionFactoryBean;
    3. import org.mybatis.spring.annotation.MapperScan;
    4. import org.springframework.beans.factory.annotation.Qualifier;
    5. import org.springframework.boot.context.properties.ConfigurationProperties;
    6. import org.springframework.context.annotation.Bean;
    7. import org.springframework.context.annotation.Configuration;
    8. import org.springframework.core.io.Resource;
    9. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    10. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    11. import com.alibaba.druid.pool.DruidDataSource;
    12. @Configuration
    13. @ConfigurationProperties
    14. @MapperScan(basePackages = "org.demoflowable.clickhouse.dao", sqlSessionFactoryRef = "clickhouseSqlSessionFactory")
    15. public class ClickHouseConfig {
    16. @javax.annotation.Resource
    17. private ClickHouseJdbcParamConfig jdbcParamConfig;
    18. @Bean("clickDataSource")
    19. public DataSource dataSource() {
    20. DruidDataSource datasource = new DruidDataSource();
    21. datasource.setUrl(jdbcParamConfig.getUrl());
    22. datasource.setDriverClassName(jdbcParamConfig.getDriverClassName());
    23. datasource.setInitialSize(jdbcParamConfig.getInitialSize());
    24. datasource.setMinIdle(jdbcParamConfig.getMinIdle());
    25. datasource.setMaxActive(jdbcParamConfig.getMaxActive());
    26. datasource.setMaxWait(jdbcParamConfig.getMaxWait());
    27. datasource.setUsername(jdbcParamConfig.getUsername());
    28. datasource.setPassword(jdbcParamConfig.getPassword());
    29. return datasource;
    30. }
    31. @Bean("clickTransactionManager")
    32. public DataSourceTransactionManager getDataSourceTransactionManager(
    33. @Qualifier("clickDataSource") DataSource dataSource) {
    34. return new DataSourceTransactionManager(dataSource);
    35. }
    36. @Bean("clickhouseSqlSessionFactory")
    37. public SqlSessionFactory getSqlSessionManager(@Qualifier("clickDataSource") DataSource dataSource)
    38. throws Exception {
    39. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    40. bean.setDataSource(dataSource);
    41. Resource[] resource = new PathMatchingResourcePatternResolver()
    42. .getResources("classpath:META-INF/mapper/clickhouse/*.xml");
    43. bean.setMapperLocations(resource);
    44. return bean.getObject();
    45. }
    46. }

    七、启动类Application

    1. import org.springframework.boot.SpringApplication;
    2. import org.springframework.boot.autoconfigure.SpringBootApplication;
    3. import org.springframework.context.annotation.ComponentScan;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import org.springframework.web.bind.annotation.RequestParam;
    6. import org.springframework.web.bind.annotation.RestController;
    7. /**
    8. * @Description: 启动类
    9. */
    10. @SpringBootApplication
    11. @ComponentScan({ "org.demoflowable" })
    12. public class Application {
    13. /**
    14. * @Title: main
    15. * @Description: 启动类
    16. * @param args
    17. * @date 2023-11-08 01:49:23
    18. */
    19. public static void main(String[] args) {
    20. SpringApplication.run(Application.class, args);
    21. }
    22. }

  • 相关阅读:
    【informix】解决启动报错大全,以及解决办法
    Netty高性能之Reactor模型
    EasyExcel 修改导出的文件属性
    【Three.js】第十二章 Materials 材质
    程序员的成长分岔路——技术管理和技术专家,怎么选?
    【Mycat】Mycat主从复制
    利用python写一个可视化的界面
    华为云文件上传(单个上传和分段上传)
    MXNet对GoogLeNet的实现(并行连结网络)
    MySQL系列——集群复制方式及原理
  • 原文地址:https://blog.csdn.net/itorac/article/details/134299234