当前位置:网站首页>Druid data source for background monitoring

Druid data source for background monitoring

2022-06-26 09:51:00 Ma cute's Ma cute

1、 stay pom.xml Integrating Druid data sources in

 <!-- Druid data source -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.1</version>
        </dependency>

2、 stay application.yml Configure the connection database in

spring:
  datasource:
    username: root
    password: 123456
    # If the time zone is wrong , Just add a time zone configuration 
    url: jdbc:mysql://localhost:3306/product?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
#  Specify the type of data source ,spring The default data source is hikari, Specify the Druid data source we introduced ourselves 
    type: com.alibaba.druid.pool.DruidDataSource
    filters: stat,wall,log4j
#  Log monitoring function , Configure monitoring statistics filters,stat: Monitoring statistics 、log4j: logging 、wall: defense sql Inject 

server:
  port: 8001

3、 To configure druid The most powerful function of data source - Background monitoring function

(1)、 Definition config Under folder DruidConfig.java file

@Configuration
public class DruidConfig {
    
    @ConfigurationProperties(prefix = "spring.datasource")  /* And application.yaml binding */
    @Bean
    public DataSource druidDataSource() {
    
        return new DruidDataSource();
    }

    @Bean
    /* Background monitoring function  web.xml*/
//  because springboot Built in servlet Containers , So there was no web.xml
    public ServletRegistrationBean statViewServlet(){
    
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*"); // Get background monitoring 
        // Someone needs to log in and check in the background , Configure account 、 password 
        Map<String, String> map = new HashMap<>();
        map.put("loginUsername","ml");  // Landed key Is constant 
        map.put("loginPassword","123456");
        /* Who is allowed to access , If the parameter is empty , It means that anyone can access */
        map.put("allow","");
        /* No one is allowed to visit */
        map.put("mm","192.168.12.3");  // prohibit ip Address access configuration 
        bean.setInitParameters(map);  // Set initialization parameters 
        return bean;
    }

    @Bean
    public FilterRegistrationBean webStatFilter(){
    
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        /* Which requests can be filtered out */
        Map<String, String> map = new HashMap<>();
        // These things don't count 
        map.put("exclusions","*.js,*.css,/druid/*");
        return filterRegistrationBean;
    }

}

4、 visit http://localhost:8001/druid

(1)、 The home page is as follows
 Insert picture description here
(2)、 The visit page is as follows
 Insert picture description here
(3)、 Use http://localhost:8001/add Implement the following code in the background

@GetMapping("/add")
    public String add() {
    
        String sql = "insert into student (sid,sname,sclass,tid) values('12344555',' Xiao Ming ','1602 class ','12356987')";
        jdbcTemplate.update(sql);
        return "add ok!";
    }

And then click “sql monitor ” You can see the execution just now sql sentence , Realization sql Monitoring function
 Insert picture description here
Click to execute sql sentence , You can see the details
 Insert picture description here

原网站

版权声明
本文为[Ma cute's Ma cute]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260915349015.html