您的位置 首页 > 娱乐休闲

精通Spring Boot:整合druid监控SQL执行

咳咳,今天我们做些简单而实用的东西,使用springboot 整合alibaba的driud数据库监控工具。 alibaba已经提供了spring-boot-starter的jar包了。先看加个依赖~~ 自己找哈) 。

<!--阿里巴巴druid数据库管理--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency>

配置信息是少不了滴~~

# 开发环境下配置 spring: datasource: druid: url: jdbc:mysql://localhost:3306/test?allowMultiQueries:true&characterEncoding:UTF-8&zeroDateTimeBehavior:convertToNull username: root password: 123456 driver-class-name: com.my # 连接池配置 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.;druid. max-pool-prepared-statement-per-connection-size: 20 filters: stat,wall #filters: #配置多个英文逗号分隔(统计,sql注入,log4j过滤) type: com.alibaba.druid.

接下来是重点,重点,重点......@Configuration注解的配置类

@Configuration public class DruidConfig { private static final Logger logger = LoggerFac); @Autowired private AppConfig appConfig; @Value("${}") private String dbUrl; @Value("${}") private String username; @Value("${}") private String password; @Value("${}") private String driverClassName; @Value("${}") private int initialSize; @Value("${}") private int minIdle; @Value("${}") private int maxActive; @Value("${}") private int maxWait; @Value("${}") private int timeBetweenEvictionRunsMillis; @Value("${}") private int minEvictableIdleTimeMillis; @Value("${}") private String validationQuery; @Value("${}") private boolean testWhileIdle; @Value("${}") private boolean testOnBorrow; @Value("${}") private boolean testOnReturn; @Value("${}") private boolean poolPreparedStatements; @Value("${}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${}") private String filters; @Value("{}") private String connectionProperties; @Bean //声明其为Bean实例 @Primary //在同样的DataSource中,首先使用被标注的DataSource public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); da(dbUrl); da(username); da(password); da(driverClassName); //configuration da(initialSize); da(minIdle); da(maxActive); da(maxWait); da(timeBetweenEvictionRunsMillis); da(minEvictableIdleTimeMillis); da(validationQuery); da(testWhileIdle); da(testOnBorrow); da(testOnReturn); da(poolPreparedStatements); da(maxPoolPreparedStatementPerConnectionSize); try { da(filters); } catch (Exception e) { logger.error("druid configuration initialization filter", e); } da(connectionProperties); return datasource; } @Bean public ServletRegistrationBean druidServlet() { ("init Druid Servlet Configuration "); ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); (new StatViewServlet()); ("/druid/*"); Map<String, String> initParameters = new HashMap<>(); ini("loginUsername", a());// 用户名 ini("loginPassword", a());// 密码 ini("resetEnable", a());// 禁用HTML页面上的“Reset All”功能 ini("allow", ""); // IP白名单 (没有配置或者为空,则允许所有访问) //ini("deny", "192.168.20.38");// IP黑名单 (存在共同时,deny优先于allow) (initParameters); return servletRegistrationBean; } @Bean public FilterRegistrationBean druidWebStatViewFilter() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(new WebStatFilter()); regi("urlPatterns", "/*"); regi("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"); return registrationBean; } }

这里解释说下,AppConfig类是我自定义的一些配置项目,和集成druid没有关系噢。 到这就可以写个连接数据库查询测试下druid了,在地址栏输入localhost:8080/druid就能访问druid的登录页面了。

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“精通Spring,Boot整合druid监控SQL执行”边界阅读