SpringBoot整合Druid数据库连接池的方法

网友投稿 406 2022-12-01


SpringBoot整合Druid数据库连接池的方法

一,Druid是什么?

Druid是java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。

二, 在哪里下载druid

maven中央仓库: http://central.maven.org/maven2/com/alibaba/druid/

三, 怎么获取Druid的源码

Druid是一个开源项目,源码托管在github上,源代码仓库地址是 https://github.com/alibaba/druid。同时每次Druid发布正式版本和快照的时候,都会把源码打包,你可以从上面的下载地址中找到相关版本的源码

项目配置

pom.xml

com.alibaba&JwzMMKMwClt;/groupId>

druid

1.1.10

com.alibaba

druid-spring-boot-starter

1.1.10

application.yml

server:

port: 8080

spring:

datasource:

username: root

password: root

url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC

driver-class-name: com.mysql.cj.jdbc.Driver

type: com.alibaba.druid.pool.DruidDataSource

initialSize: 5

minIdle: 5

maxActive: 20

maxWait: 60000

timeBetweenEvictionRunsMillis: 60000

minEvictableIdleTimeMillis: 300000

validationQuery: SELECT 1 FROM DUAL

testWhileIdle: true

testOnBorrow: false

testOnReturn: false

poolPreparedStatements: true

maxPoolPreparedStatementPerConnectionSize: 25

filters: stat,wall,slf4j

connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

useGlobalDataSourceStat: true

cache:

type: redis

redis:

host: 127.0.0.1

port: 6379

password:

pool:

mhttp://ax-active: 100

max-idle: 10

max-wait: 100000

lettuce:

shutdown-timeout: 0

timeout: 5000

database: 0

thymeleaf:

cache: false;

mybatis:

mapper-locations: classpath:zhw.example.zhw.loginModule.loginDao/*.xml

配置JdbcConfig

package zhw.example.zhw.loginModule.config;

import com.alibaba.druid.pool.DruidDataSource;

import com.alibaba.druid.support.http.StatViewServlet;

import com.alibaba.druid.support.http.WebStatFilter;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.boot.web.servlet.ServletRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

@Configuration

public class JdbcConfig {

@ConfigurationProperties(prefix = "spring.datasource")

@Bean

public DataSource dataSource(){

return new DruidDataSource();

}

/**

* 配置Druid监控

*

* @return StatViewServlet

*/

@Bean

public ServletRegistrationBean servletRegistrationBean() {

ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

Map map = new HashMap<>();

//访问的用户名密码

map.put(StatViewServlet.PARAM_NAME_USERNAME, "root");

map.put(StatViewServlet.PARAM_NAME_PASSWORD, "root");

//允许访问的ip,默认是所有ip

map.put(StatViewServlet.PARAM_NAME_ALLOW, "");

//禁止访问的ip

map.put(StatViewServlet.PARAM_NAME_DENY, "192.168.1.1");

bean.setInitParameters(map);

return bean;

}

/**

* 配置一个监控的filter

*

* @return WebStatFilter

*/

@Bean

public FilterRegistrationBean filterRegistrationBean() {

FilterRegistrationBean bean = new FilterRegistrationBean<>();

bean.setFilter(new WebStatFilter());

Map map = new HashMap<>();

//移除这些监听

map.put(WebStatFilter.PARAM_NAME_EXCLUSIONS, "*.js,*.css,/druid/*,*.gif,*.jpg,*.png");

bean.setInitParameters(map);

//拦截所有请求,全部都要走druid监听

bean.setUrlPatterns(Collections.singletonList("/*"));

return bean;

}

}

测试配置url白名单

如果工程中配置了Apache Shiro,需要在配置类中添加白名单

监控界面


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java调用腾讯云短信API接口的实现
下一篇:springboot+thymeleaf+druid+mybatis 多模块实现用户登录功能
相关文章

 发表评论

暂时没有评论,来抢沙发吧~