以下是配置数据源的操作:
@Configuration @EnableConfigurationProperties(ShardingDataSourceConfig.class) public class DataSourceConfig { private static final Logger logger = LoggerFactory.getLogger(DataSourceConfig.class); @Resource OracleConfig oracleConfig; @Resource private ShardingDataSourceConfig shardingDataSourceConfig; @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") public DataSource secondaryDataSource() throws Exception{ //return DataSourceBuilder.create().build(); ShardingDruidDataSource datasource = new ShardingDruidDataSource(); logger.debug("decrypt = {}", shardingDataSourceConfig.isDecrypt()); logger.debug("decryptKey = {}", shardingDataSourceConfig.getDecryptKey()); logger.debug("encryptedPassword = {}", datasource.getEncryptedPassword()); datasource.setUrl(oracleConfig.dbUrl); datasource.setUsername(oracleConfig.username); //datasource.setPassword(oracleConfig.password); datasource.setEncryptedPassword(oracleConfig.encryptedPassword); datasource.setDriverClassName(oracleConfig.driverClassName); //configuration datasource.setInitialSize(oracleConfig.initialSize); datasource.setMinIdle(oracleConfig.minIdle); datasource.setMaxActive(oracleConfig.maxActive); datasource.setMaxWait(oracleConfig.maxWait); datasource.setTimeBetweenEvictionRunsMillis(oracleConfig.timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(oracleConfig.minEvictableIdleTimeMillis); datasource.setValidationQuery(oracleConfig.validationQuery); datasource.setTestWhileIdle(oracleConfig.testWhileIdle); datasource.setTestOnBorrow(oracleConfig.testOnBorrow); datasource.setTestOnReturn(oracleConfig.testOnReturn); datasource.setPoolPreparedStatements(oracleConfig.poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(oracleConfig.maxPoolPreparedStatementPerConnectionSize); if (shardingDataSourceConfig.isDecrypt() && !Strings.isEmpty(datasource.getEncryptedPassword()) && !Strings.isEmpty(shardingDataSourceConfig.getDecryptKey())) { String decryptedPassword = ConfigTools.decrypt(shardingDataSourceConfig.getDecryptKey(), datasource.getEncryptedPassword()); datasource.setPassword(decryptedPassword); } try { datasource.setFilters(oracleConfig.filters); } catch (Exception e) { logger.error("druid configuration initialization filter", e); } datasource.setConnectionProperties(oracleConfig.connectionProperties); return datasource; } @Bean(name = "secondaryJdbcTemplate") public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
数据分片,加解密操作公钥和私钥分开:
@ConfigurationProperties(prefix="shardingdatasource") public class ShardingDataSourceConfig { /** * 密码加密:私钥 * */ private static String Pri = ""; /** * 密码加密:公钥 * */ private static String Pub = ""; private boolean decrypt = true; private String decryptKey = Pub; public boolean isDecrypt() { return decrypt; } public void setDecrypt(boolean decrypt) { this.decrypt = decrypt; } public String getDecryptKey() { return decryptKey; } public void setDecryptKey(String decryptKey) { this.decryptKey = decryptKey; } public static void main(String[] args) throws Exception { String encrypted = ConfigTools.encrypt(Pri, "sad"); System.out.println(encrypted); String decrypted = ConfigTools.decrypt(Pub, encrypted); System.out.println(decrypted); } }
连接数据源的配置:
@Configuration public class OracleConfig { private static final Logger logger = LoggerFactory.getLogger(OracleConfig.class); //@Autowired //private AppConfig appConfig; @Value("${spring.datasource.druid.url}") public String dbUrl; @Value("${spring.datasource.druid.username}") public String username; //@Value("${spring.datasource.druid.password}") //public String password; @Value("${spring.datasource.druid.encryptedPassword}") public String encryptedPassword; @Value("${spring.datasource.druid.driver-class-name}") public String driverClassName; @Value("${spring.datasource.druid.initial-size}") public int initialSize; @Value("${spring.datasource.druid.min-idle}") public int minIdle; @Value("${spring.datasource.druid.max-active}") public int maxActive; @Value("${spring.datasource.druid.max-wait}") public int maxWait; @Value("${spring.datasource.druid.time-between-eviction-runs-millis}") public int timeBetweenEvictionRunsMillis; @Value("${spring.datasource.druid.min-evictable-idle-time-millis}") public int minEvictableIdleTimeMillis; @Value("${spring.datasource.druid.validation-query}") public String validationQuery; @Value("${spring.datasource.druid.test-while-idle}") public boolean testWhileIdle; @Value("${spring.datasource.druid.test-on-borrow}") public boolean testOnBorrow; @Value("${spring.datasource.druid.test-on-return}") public boolean testOnReturn; @Value("${spring.datasource.druid.pool-prepared-statements}") public boolean poolPreparedStatements; @Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}") public int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.druid.filters}") public String filters; @Value("{spring.datasource.druid.connection-properties}") public String connectionProperties; }
配置文件如下:
datasource: druid: url: jdbc:oracle:thin:@10.10.61.46:1521:lisprd1 username: epassport #password: YnVuddrPRh6Hlsd encryptedPassword: v1XNQzFrMH0UeZhkzxBgHg/JgQr6x0a/HIrVTvLJ6z9gvRi3s/9qHqpJeIm24X/rp71Lx0N8OZ8R1tdkI0cx1GDU2s1/WeF1c1IH001SxfZcLpou0TWQCwilkCqYv5UATjrCfStTBkwwT17rQIESFFG9hDFadOQW8UD1soei+tU= driver-class-name: oracle.jdbc.OracleDriver # 连接池配置 initial-size: 1 max-active: 20 min-idle: 1 max-wait: 10000 pool-prepared-statements: true max-open-prepared-statements: 20 validation-query: SELECT 1 FROM DUAL validation-query-timeout: 5000 test-on-borrow: false test-on-return: false test-while-idle: true time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 30000 max-evictable-idle-time-millis: 60000 removeAbandoned: true removeAbandonedTimeout: 1800 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 max-pool-prepared-statement-per-connection-size: 20 filters: stat,wall #filters: #配置多个英文逗号分隔(统计,sql注入,log4j过滤) type: com.alibaba.druid.pool.DruidDataSource
这样配置就差不多ok了。只要再代码中使用jdbcTemplet执行sql就能成功读取数据
原文:https://www.cnblogs.com/otways/p/11156190.html