springboot集成阿里数据连接池druid

springboot集成阿里数据连接池druid

  • 1.pom文件
  • 2.application.propreties
  • 3.driud配置类
  • 4.启动项目
  • 5.访问druid连接池监控控制台

1.pom文件

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

2.application.propreties

#应用名称
spring.application.name=cache
#应用端口
server.port=8080

#数据库
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#打印sql
spring.jpa.show-sql=true

#指明数据库类型
spring.datasource.name=MYSQL

#druid连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# druid参数调优(可选)
#初始化时建立物理连接的个数
spring.datasource.druid.initial-size=3
#最小连接池数量
spring.datasource.druid.min-idle=3
#最大连接池数量
spring.datasource.druid.max-active=10
#获取连接时最大等待时间
spring.datasource.druid.max-wait=60000
#慢SQL执行时间
spring.datasource.druid.filter.stat.slow-sql-millis=1

3.driud配置类

package com.lyj.cache.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;

/**
 * druid监控控制台配置
 */
@Configuration
public class DruidConfig {
     
    // 主要实现web监控的配置处理
    @Bean
    public ServletRegistrationBean druidServlet() {
     
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        //servletRegistrationBean.addInitParameter("allow", "127.0.0.1,192.168.202.233");//白名单
        //servletRegistrationBean.addInitParameter("deny", "192.168.202.234");//黑名单
        servletRegistrationBean.addInitParameter("loginUsername", "root");//用户名
        servletRegistrationBean.addInitParameter("loginPassword", "root");//密码
        servletRegistrationBean.addInitParameter("resetEnable", "false");//是否可以重置数据源
        return servletRegistrationBean;

    }

    //过滤
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
     
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");//所有请求进行监控处理
        filterRegistrationBean.addInitParameter("exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");//排除
        return filterRegistrationBean;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
     
        return new DruidDataSource();
    }
}

4.启动项目

Connected to the target VM, address: '127.0.0.1:6875', transport: 'socket'

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-07-14 15:41:39.791  INFO 2572 --- [  restartedMain] com.lyj.cache.CacheApplication           : Starting CacheApplication on lyj with PID 2572 (F:\intellij_study\spring_boot\cache\target\classes started by 李亚杰 in F:\intellij_study\spring_boot\cache)
2020-07-14 15:41:39.795  INFO 2572 --- [  restartedMain] com.lyj.cache.CacheApplication           : No active profile set, falling back to default profiles: default
2020-07-14 15:41:39.891  INFO 2572 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-07-14 15:41:39.892  INFO 2572 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-07-14 15:41:40.927  INFO 2572 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-07-14 15:41:40.957  INFO 2572 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17ms. Found 0 JPA repository interfaces.
2020-07-14 15:41:41.856  INFO 2572 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-07-14 15:41:41.868  INFO 2572 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-07-14 15:41:41.868  INFO 2572 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-14 15:41:42.041  INFO 2572 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-07-14 15:41:42.042  INFO 2572 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2149 ms
2020-07-14 15:41:42.325  INFO 2572 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-14 15:41:42.380  INFO 2572 --- [  restartedMain] com.alibaba.druid.pool.DruidDataSource   : {
     dataSource-1,MYSQL} inited
2020-07-14 15:41:42.563  INFO 2572 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-07-14 15:41:42.611  WARN 2572 --- [  restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-07-14 15:41:42.646  INFO 2572 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.17.Final
2020-07-14 15:41:43.060  INFO 2572 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {
     5.1.0.Final}
2020-07-14 15:41:43.247  INFO 2572 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-07-14 15:41:43.267  INFO 2572 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect
2020-07-14 15:41:43.316  INFO 2572 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-07-14 15:41:43.321  INFO 2572 --- [  restartedMain] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-07-14 15:41:43.322  INFO 2572 --- [  restartedMain] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-07-14 15:41:43.338  INFO 2572 --- [  restartedMain] com.lyj.cache.CacheApplication           : Started CacheApplication in 4.456 seconds (JVM running for 7.43)
2020-07-14 15:41:43.515  INFO 2572 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-07-14 15:41:43.527  INFO 2572 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-07-14 15:41:57.150  INFO 2572 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-07-14 15:41:57.151  INFO 2572 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-07-14 15:41:57.166  INFO 2572 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 15 ms

5.访问druid连接池监控控制台

springboot集成阿里数据连接池druid_第1张图片
输入在druid配置类里设置的用户名密码,即可登录成功
springboot集成阿里数据连接池druid_第2张图片

你可能感兴趣的:(java,spring,boot,Druid)