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多数据源内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java split 分隔空值无法得到的解决方式

    Java split 分隔空值无法得到的解决方式

    这篇文章主要介绍了Java split 分隔空值无法得到的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • SpringBoot环境配置知识总结

    SpringBoot环境配置知识总结

    今天带大家了解SpringBoot环境配置的相关知识,文中有非常详细的代码示例,对正在学习java的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-05-05
  • Java设计模式之原型模式详细解析

    Java设计模式之原型模式详细解析

    这篇文章主要介绍了Java设计模式之原型模式详细解析,原型模式就是用一个已经创建的实例作为原型,通过复制该原型对象来创建一个和原型对象相同的新对象,需要的朋友可以参考下
    2023-11-11
  • 完美解决idea无法搜索下载插件的问题

    完美解决idea无法搜索下载插件的问题

    这篇文章主要介绍了完美解决idea无法搜索下载插件的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • java基础之类初始化顺序示例解析

    java基础之类初始化顺序示例解析

    这篇文章主要为大家介绍了java基础之类初始化顺序示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • JAVA进程突然消失问题解决方案

    JAVA进程突然消失问题解决方案

    这篇文章主要介绍了JAVA进程突然消失问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • SpringBoot URL带有特殊字符([]/{}等),报400错误的解决

    SpringBoot URL带有特殊字符([]/{}等),报400错误的解决

    这篇文章主要介绍了SpringBoot URL带有特殊字符([]/{}等),报400错误的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Spring Cloud Ribbon配置详解

    Spring Cloud Ribbon配置详解

    这篇文章主要介绍了Spring Cloud Ribbon配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Spring5使用JSR 330标准注解的方法

    Spring5使用JSR 330标准注解的方法

    从Spring3.0之后,除了Spring自带的注解,我们也可以使用JSR330的标准注解,本文主要介绍了Spring5使用JSR 330标准注解,感兴趣的可以了解一下
    2021-09-09
  • 轻松掌握Java适配器模式

    轻松掌握Java适配器模式

    这篇文章主要帮助大家轻松掌握Java适配器模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09

最新评论