1、添加依赖
<!-- MyBatis-Plus Starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- MySQL JDBC Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!-- ClickHouse JDBC Connector --> <dependency> <groupId>ru.yandex.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.2.4</version> </dependency>
2、配置文件
spring.datasource.dynamic.primary=master spring.datasource.dynamic.strict=false spring.datasource.dynamic.datasource.master.url=jdbc:mysql://172.16.22.80:3306/hw-manage?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&useSSL=false spring.datasource.dynamic.datasource.master.username=root spring.datasource.dynamic.datasource.master.password=root spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.jdbc.Driver spring.datasource.dynamic.datasource.click.url=jdbc:clickhouse://41.6.251.1:8123,41.6.251.2:8123,41.6.251.3:8123/vehicle_dev spring.datasource.dynamic.datasource.click.type=ru.yandex.clickhouse.BalancedClickhouseDataSource spring.datasource.dynamic.datasource.click.username=11 spring.datasource.dynamic.datasource.click.password=22 spring.datasource.dynamic.datasource.click.driver-class-name=ru.yandex.clickhouse.ClickHouseDriver
3、添加配置类
package com.zygh.hzhw.manage.config; import com.baomidou.dynamic.datasource.creator.AbstractDataSourceCreator; import com.baomidou.dynamic.datasource.creator.DataSourceCreator; import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import ru.yandex.clickhouse.BalancedClickhouseDataSource; import ru.yandex.clickhouse.settings.ClickHouseProperties; import javax.sql.DataSource; import java.util.Objects; // 保证执行顺序在前面,否则 会被其他加载器给创建,导致自定义的加载器执行不到 @Order(0) @Configuration public class DynamicDataSourceConfig extends AbstractDataSourceCreator implements DataSourceCreator { @Override public DataSource doCreateDataSource(DataSourceProperty dataSourceProperty) { ClickHouseProperties properties = new ClickHouseProperties(); properties.setUser("11"); properties.setPassword("22"); properties.setMaxThreads(10); properties.setAsync(true); properties.setMaxTotal(2000); properties.setDefaultMaxPerRoute(1000); properties.setBufferSize(2000); properties.setApacheBufferSize(2000); properties.setSocketTimeout(Integer.MAX_VALUE); properties.setMaxQuerySize(Long.MAX_VALUE); properties.setConnectionTimeout(10 * 60 * 1000); BalancedClickhouseDataSource clickhouseDataSource = new BalancedClickhouseDataSource("jdbc:clickhouse://41.6.251.1:8123,41.6.251.2:8123,41.6.251.3:8123/vehicle_dev", properties); return clickhouseDataSource; } @Override public boolean support(DataSourceProperty dataSourceProperty) { // 适配器模式,判断是否需要使用当前动态数据源创建 Class<? extends DataSource> type = dataSourceProperty.getType(); if (Objects.isNull(type)) { return Boolean.FALSE; } return StringUtils.equals(type.getName(), BalancedClickhouseDataSource.class.getName()); } }
标签:springboot,数据源,dynamic,datasource,spring,import,properties,clickhouse From: https://www.cnblogs.com/liubaihui/p/17499076.html