栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot集成中间件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot集成中间件

1 集成Redis线程池(JedisPool)

使用的依赖


    redis.clients
    jedis
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)

使用的依赖


    com.alibaba
    druid
    1.2.8



    
    com.clickhouse
    clickhouse-jdbc
    0.3.2-patch9
    
    http
    
        
            *
            *
        
    
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();
        }
    }

    

    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;
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966982.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号