当前位置:   article > 正文

druid以及监控台使用及配置_druid控制台

druid控制台

一、druid

1.添加依赖

引入依赖

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2.配置

  • 配置yml文件
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 配置数据源的三种方式
    1.在配置类中创建一个数据源
@Value("spring.datasource.url")
    private String url;
    @Value("spring.datasource.username")
    private String userName;
    @Value("spring.datasource.password")
    private String password;
    @Value("spring.datasource.driver-class-name")
    private String driverClassName;
    @Value("spring.datasource.type")
    private String type;
    
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setDbType(type);
        return dataSource;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2.使用注解ConfigurationProperties

@Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return new DruidDataSource();
    }
  • 1
  • 2
  • 3
  • 4
  • 5

3.使用DataSourceProperties构造者设计模式

	@Bean
    public DataSource dataSource(DataSourceProperties properties){
       return properties.initializeDataSourceBuilder().build();
    }
  • 1
  • 2
  • 3
  • 4

二、监控台配置

1.创建配置类

/**
     * 监控台的servlet
     */
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new StatViewServlet());
        servletRegistrationBean.addUrlMappings("/druid/*")
        // 添加IP白名单
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
        // 添加IP黑名单,当白名单和黑名单重复时,黑名单优先级更高
        // servletRegistrationBean.addInitParameter("deny", "127.0.0.1");
        // 添加控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "123456");
        // 是否能够重置数据
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    /**
     * 配置服务过滤器 :监控哪些访问
     * @return 返回过滤器配置对象
     */
    @Bean
    public FilterRegistrationBean statFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        // 添加过滤规则
        filterRegistrationBean.addUrlPatterns("/*");
        // 忽略过滤格式
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,");
        return filterRegistrationBean;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

本地访问http://localhost:8080/druid
在这里插入图片描述

三、Druid常用启动器

1.添加依赖

添加常用启动器依赖

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

2.配置

依赖添加完成后,不需要创建数据源,可直接使用,但是访问Druid的控制台,却无法访问。
大致查看源码:
在这里插入图片描述

//创建一个DruidDataSourceWrapper数据源
@ConditionalOnMissingBean
    public DataSource dataSource() {
        LOGGER.info("Init DruidDataSource");
        return new DruidDataSourceWrapper();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
@ConfigurationProperties("spring.datasource.druid")
public class DruidDataSourceWrapper extends DruidDataSource implements InitializingBean {
    @Autowired
    private DataSourceProperties basicProperties;

    public DruidDataSourceWrapper() {
    }
	//重写InitializingBean方法,springboot启动时,会自动加载 
    public void afterPropertiesSet() throws Exception {
        if(super.getUsername() == null) {
        	//从对象basicProperties中拿设置的用户名
            super.setUsername(this.basicProperties.determineUsername());
        }

        if(super.getPassword() == null) {
        	//从对象basicProperties中拿设置的用户名
            super.setPassword(this.basicProperties.determinePassword());
        }

        if(super.getUrl() == null) {
            super.setUrl(this.basicProperties.determineUrl());
        }

        if(super.getDriverClassName() == null) {
            super.setDriverClassName(this.basicProperties.getDriverClassName());
        }

    }
}
//查看basicProperties
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {

	private ClassLoader classLoader;

	private String name;

	private boolean generateUniqueName;

	private Class<? extends DataSource> type;

	private String driverClassName;
	....
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

由此看出,在yml文件数据源的配置:spring.datasource.url等等
我们在回头看上面的截图中:DruidDataSourceAutoConfigure类上@Import注解中引入DruidStatViewServletConfiguration.class,我们可以点开看看:

@ConditionalOnWebApplication
@ConditionalOnProperty(
    name = {"spring.datasource.druid.stat-view-servlet.enabled"},
    havingValue = "true"
)
public class DruidStatViewServletConfiguration {
    private static final String DEFAULT_ALLOW_IP = "127.0.0.1";

    public DruidStatViewServletConfiguration() {
    }

    @Bean
    public ServletRegistrationBean statViewServletRegistrationBean(DruidStatProperties properties) {
    	//获取Druid监控台配置信息
        StatViewServlet config = properties.getStatViewServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.setServlet(new com.alibaba.druid.support.http.StatViewServlet());
        registrationBean.addUrlMappings(new String[]{config.getUrlPattern() != null?config.getUrlPattern():"/druid/*"});
        if(config.getAllow() != null) {
            registrationBean.addInitParameter("allow", config.getAllow());
        } else {
            registrationBean.addInitParameter("allow", "127.0.0.1");
        }

        if(config.getDeny() != null) {
            registrationBean.addInitParameter("deny", config.getDeny());
        }

        if(config.getLoginUsername() != null) {
            registrationBean.addInitParameter("loginUsername", config.getLoginUsername());
        }

        if(config.getLoginPassword() != null) {
            registrationBean.addInitParameter("loginPassword", config.getLoginPassword());
        }

        if(config.getResetEnable() != null) {
            registrationBean.addInitParameter("resetEnable", config.getResetEnable());
        }

        return registrationBean;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

ConditionalOnProperty注解做了限制,由此可看出yml需要配置spring.datasource.druid.stat-view-servlet.enabled属性,且为true。其他的用户名、密码如何配置呢
配置信息都是从StatViewServlet类中获取,查看一下代码

@ConfigurationProperties("spring.datasource.druid")
public class DruidStatProperties {
    private String[] aopPatterns;
    private DruidStatProperties.StatViewServlet statViewServlet = new DruidStatProperties.StatViewServlet();
    public static class StatViewServlet {
        private boolean enabled;
        private String urlPattern;
        private String allow;
        private String deny;
        private String loginUsername;
        private String loginPassword;
        private String resetEnable;

 	}
 }      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

由此看出:设置账号的spring.datasource.druid.StatViewServlet.loginUsername
yml的格式spring.datasource.druid.stat-view-servlet.login-username
其他配置同理

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/355091
推荐阅读
相关标签
  

闽ICP备14008679号