SpringBoot集成中间件

1 集成Redis线程池(JedisPool)

使用的依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

1.1 编写配置文件

在applicaiton.properties文件中添加Redis配置

spring.datasource.redis.host=localhost
spring.datasource.redis.port=6379
spring.datasource.redis.username=redis
spring.datasource.redis.password=redis
spring.datasource.redis.database=0
spring.datasource.redis.pool-max-idle=100
spring.datasource.redis.pool-max-wait=-1
spring.datasource.redis.pool-min-idle=0
spring.datasource.redis.timeout=5000

1.2 编写配置类

创建一个RedisConfig配置类。用于读取application.properties的配置。

@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.redis")
@Primary
public class RedisConfig {
    private String host;
    private Integer port=6379;
    private String username;
    private String password;
    private Integer database=0;
    private Integer poolMaxIdle=100;
    private Integer poolMaxWait=-1;
    private Integer poolMinIdle=0;
    private Integer timeout=5000;
}

1.3 编写线程池类

创建RedisPool线程池类,用于配置Redis线程池。

@Component
@EnableConfigurationProperties(RedisConfig.class)
public class RedisPool {

    @Bean(destroyMethod = "close")
    public JedisPool getJedisPool(RedisConfig redisConfig){
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        // 是否启用pool的jmx管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(true);
        jedisPoolConfig.setMaxIdle(100);
        jedisPoolConfig.setMaxWait(BaseObjectPoolConfig.DEFAULT_MAX_WAIT);
        jedisPoolConfig.setBlockWhenExhausted(false);
        return new JedisPool(jedisPoolConfig,redisConfig.getHost(),redisConfig.getPort(),redisConfig.getUsername(), redisConfig.getPassword());
    }
}

1.4 编写Redis工具类

在使用JedisPool的时候遇到连接不主动释放,实例必须要手动归还的问题。即jedis高版本的jedis.close()来归还。

所以编写了工具类,实现了对Jedis所有接口的封装,可以自动释放连接,配合上述配置,可以直接使用。下载地址

部分展示:

@Slf4j
@Component
public class RedisUtils {
    @Autowired
    private JedisPool jedisPool;

    public Boolean copy(String srcKey, String dstKey, int db, boolean replace) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.copy(srcKey, dstKey, db, replace);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public String set(String key, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public String get(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}

2 集成ClickHouse线程池(Druid)

使用的依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.8</version>
</dependency>

<dependency>
    <!-- will stop using ru.yandex.clickhouse starting from 0.4.0  -->
    <groupId>com.clickhouse</groupId>
    <artifactId>clickhouse-jdbc</artifactId>
    <version>0.3.2-patch9</version>
    <!-- below is only needed when all you want is a shaded jar -->
    <classifier>http</classifier>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

1.1 编写配置文件

在applicaiton.properties文件中添加ClickHouse配置

#===================== druid =======================
spring.datasource.clickhouse.username=default
spring.datasource.clickhouse.password=clickhouse
spring.datasource.clickhouse.url=jdbc:clickhouse://10.10.103.179:18123/aiops
spring.datasource.clickhouse.driverClassName=com.clickhouse.jdbc.ClickHouseDriver
spring.datasource.clickhouse.sslmode=disable
spring.datasource.clickhouse.initialSize=10
spring.datasource.clickhouse.minIdle=10
spring.datasource.clickhouse.maxActive=150
spring.datasource.clickhouse.maxWait=6000
spring.datasource.clickhouse.timeBetweenEvictionRunsMillis=60000
spring.datasource.clickhouse.minEvictableIdleTimeMillis=600000
spring.datasource.clickhouse.maxEvictableIdleTimeMillis=900000
spring.datasource.clickhouse.testWhileIdle=true;
spring.datasource.clickhouse.testOnBorrow=false;
spring.datasource.clickhouse.testOnReturn=false;
spring.datasource.clickhouse.poolPreparedStatements=true;
spring.datasource.clickhouse.maxOpenPreparedStatements=200;
spring.datasource.clickhouse.asyncInit=true;

1.2 编写配置类

创建一个ClickHouseConfiguration配置类。用于读取application.properties的配置。

@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.clickhouse")
@Primary
public class ClickHouseConfiguration {
    private String url;
    private String username = "default";
    private String password;
    private String driverClassName = "com.clickhouse.jdbc.ClickHouseDriver";
    private String sslmode = "disable";
    private String initialSize = "10";
    private String minIdle = "10";
    private String maxActive = "150";
    private String maxWait = "6000";
    private String timeBetweenEvictionRunsMillis = "60000";
    private String minEvictableIdleTimeMillis = "600000";
    private String maxEvictableIdleTimeMillis = "900000";
    private String validationQuery = "select 1";
    private String testWhileIdle = "true";
    private String testOnBorrow = "false";
    private String testOnReturn = "false";
    private String poolPreparedStatements = "true";
    private String maxOpenPreparedStatements = "200";
    private String asyncInit = "true";
}

1.3 编写线程池类

创建ClickHousePool线程池类,用于配置Druid线程池。

@Component
@EnableConfigurationProperties(ClickHouseConfiguration.class)
public class ClickHousePool {
    private DataSource dataSource;

    private ClickHousePool(ClickHouseConfiguration configuration){
        try {
            Properties properties = new Properties();
            //基本属性 url、user、password
            properties.setProperty("url", configuration.getUrl());
            properties.setProperty("driverClassName", configuration.getDriverClassName());
            properties.setProperty("username", configuration.getUsername());
            if (configuration.getPassword()!=null)
            properties.setProperty("password", configuration.getPassword());
            //配置监控统计拦截的filters
            //properties.setProperty("filters","stat");
            //配置初始化大小、最小、最大
            properties.setProperty("maxActive", configuration.getMaxActive());
            properties.setProperty("initialSize", configuration.getInitialSize());
            properties.setProperty("minIdle", configuration.getMinIdle());
            //配置获取连接等待超时的时间
            properties.setProperty("maxWait", configuration.getMaxWait());
            //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
            properties.setProperty("timeBetweenEvictionRunsMillis", configuration.getTimeBetweenEvictionRunsMillis());
            properties.setProperty("sslmode", configuration.getSslmode());
            properties.setProperty("testWhileIdle",configuration.getTestWhileIdle());
            properties.setProperty("testOnBorrow",configuration.getTestOnBorrow());
            properties.setProperty("testOnReturn",configuration.getTestOnReturn());
            properties.setProperty("poolPreparedStatements",configuration.getPoolPreparedStatements());
            properties.setProperty("maxOpenPreparedStatements",configuration.getMaxOpenPreparedStatements());
            properties.setProperty("asyncInit",configuration.getAsyncInit());
            this.dataSource = DruidDataSourceFactory.createDataSource(properties);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*static {
        try {
            Properties properties = new Properties();
            //基本属性 url、user、password
            properties.setProperty("url","jdbc:clickhouse://10.10.103.30:18123/aiops");
            properties.setProperty("driverClassName", "com.clickhouse.jdbc.ClickHouseDriver");
            //properties.setProperty("username", clickHouseConfig.getUsername());
            //properties.setProperty("password", clickHouseConfig.getPassword());
            //配置监控统计拦截的filters
            //properties.setProperty("filters","stat");
            //配置初始化大小、最小、最大
            properties.setProperty("maxActive", "250");
            properties.setProperty("initialSize", "10");
            properties.setProperty("minIdle", "10");
            //配置获取连接等待超时的时间
            properties.setProperty("maxWait", "6000");
            //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
            properties.setProperty("timeBetweenEvictionRunsMillis", "60000");
            properties.setProperty("sslmode", "disable");
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        }catch (Exception e){
            e.printStackTrace();
        }
    }*/

    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}

1.4 使用方式

部分展示:

@Slf4j
@Component
public class RedisUtils {
    @Autowired
    private JedisPool jedisPool;

    public Boolean copy(String srcKey, String dstKey, int db, boolean replace) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.copy(srcKey, dstKey, db, replace);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public String set(String key, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public String get(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值