在Spring Cloud项目中,您可以利用Druid整合阿里数据库连接池以提高性能和管理数据。这种整合提供了更好的监控、扩展和配置选项,是许多开发人员青睐的选择。通过合理配置,您可以轻松实现数据库连接池的优化,进而改善系统的稳定性和性能表现。文章来源地址https://www.toymoban.com/diary/apps/720.html
示例代码
1. 配置 pom
<!-- druid 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> <exclusions> <exclusion> <groupId>com.alibaba</groupId> <artifactId>jconsole</artifactId> </exclusion> <exclusion> <groupId>com.alibaba</groupId> <artifactId>tools</artifactId> </exclusion> </exclusions> </dependency>
2. 配置 application.properties
#druid连接池 spring.datasource.type: com.alibaba.druid.pool.DruidDataSource #最大活跃数 spring.datasource.maxActive: 20 #初始化数量 spring.datasource.initialSize: 1 #最大连接等待超时时间 spring.datasource.maxWait: 60000 #打开PSCache,并且指定每个连接PSCache的大小 spring.datasource.poolPreparedStatements: true spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20 #通过connectionProperties属性来打开mergeSql功能;慢SQL记录 #connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 spring.datasource.minIdle: 1 spring.datasource.timeBetweenEvictionRunsMillis: 60000 spring.datasource.minEvictableIdleTimeMillis: 300000 spring.datasource.validationQuery: select 1 from dual spring.datasource.testWhileIdle: true spring.datasource.testOnBorrow: false spring.datasource.testOnReturn: false #配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙 filters: stat, wall, log4j
3.Config 配置类
/** * 说明:第一数据源配置 */ @Configuration @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") //扫描 Mapper 接口并容器管理 public class MasterDataSourceConfig { static final String PACKAGE = "org.fh.mapper.dsno1"; //master 目录 static final String MAPPER_LOCATION = "classpath:mybatis/dsno1/*/*.xml"; //扫描的 xml 目录 static final String CONFIG_LOCATION = "classpath:mybatis/dsno1/mybatis-config.xml"; //自定义的mybatis config 文件位置 static final String TYPE_ALIASES_PACKAGE = "org.fh.entity"; //扫描的 实体类 目录 @Value("${datasource.no1.url}") private String url; @Value("${datasource.no1.username}") private String user; @Value("${datasource.no1.password}") private String password; @Value("${datasource.no1.driver-class-name}") private String driverClass; @Bean(name = "masterDataSource") @Primary public DataSource masterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClass); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; } @Bean(name = "masterTransactionManager") @Primary public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(masterDataSource()); } @Bean(name = "masterSqlSessionFactory") @Primary public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION)); sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(MasterDataSourceConfig.CONFIG_LOCATION)); sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPE_ALIASES_PACKAGE); return sessionFactory.getObject(); } }
文章来源:https://www.toymoban.com/diary/apps/720.html
到此这篇关于如何在Spring Cloud中利用Druid整合阿里数据库连接池的文章就介绍到这了,更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!