Today I want to talk a little bit about monitoring in Druid.

Druid database connection pool Druid database connection pool Druid database connection pool Druid database connection pool Druid database connection pool Druid database connection pool Today we’re going to look at the monitoring capabilities in Druid.

1. Preparation

First we create a Spring Boot project, introduce MyBatis, etc., as follows:

Select MyBatis and MySQL driver, do a simple test case.

Let’s connect to the database:

spring.datasource.username=root
spring.datasource.password=123
spring.datasource.url=jdbc:mysql:///test05? serverTimezone=Asia/Shanghai
Copy the code

Create a User entity class and do a simple query as follows:

public class User {
    private Integer id;
    private String username;
    private String address;
    private String password;
    private String email;
    / / omit getter/setter
}
@Mapper
public interface UserMapper {
    List<User> getUserByUsername(String username);
}
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public List<User> getUserByUsername(String username){
        returnuserMapper.getUserByUsername(username); }}@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/user")
    public List<User> getUser(String username) {
        returnuserService.getUserByUsername(username); }}Copy the code

UserMapper. XML is as follows:

<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.javaboy.druid_monitor.mapper.UserMapper">
    <select id="getUserByUsername" resultType="org.javaboy.druid_monitor.model.User">
        select * from user where username=#{username}
    </select>
</mapper>
Copy the code

A simple test, nothing to say.

This environment setup is up to you, so if you already have persistence examples, you can go straight to section 2 and introduce Druid.

Currently the default database connection pool used by the project is HikariDataSource, which is the default database connection pool in Spring Boot.

2. Introduce the Druid

Let’s introduce Druid:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.8</version>
</dependency>
Copy the code

Note that Spring Boot introduced the Druid above, which will make it easier to configure monitoring in the future.

Next we configure WebStatFilter in application.properties to collect data for web-JDBC association monitoring:

# enable WebStatFilter
spring.datasource.druid.web-stat-filter.enabled=true
Configure an interception rule
spring.datasource.druid.web-stat-filter.url-pattern=/ *
Remove unnecessary urls that do not involve SQL queries
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
# Enable session statistics
spring.datasource.druid.web-stat-filter.session-stat-enable=true
The default sessionStatMaxCount is 1000, which can be configured as needed
spring.datasource.druid.web-stat-filter.session-stat-max-count=1000
PrincipalSessionName configures principalSessionName so that Druid knows who the current session user is
The value of this parameter is the sessionName of the user information stored in the session
#spring.datasource.druid.web-stat-filter.principal-session-name=
Cookies are used to identify users
#spring.datasource.druid.web-stat-filter.principal-cookie-name=
# Enable profile to monitor a single URL call list
#spring.datasource.druid.web-stat-filter.profile-enable=
Copy the code

We can configure the first five, the last three can not configure, the meaning of each configuration songgo has been listed in the code.

Next, enable StatViewServlet configuration as follows:

# Enable the built-in monitoring page
spring.datasource.druid.stat-view-servlet.enabled=true
# Address of the built-in monitoring page
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
# Enable Reset All
spring.datasource.druid.stat-view-servlet.reset-enable=true
Set the login user name
spring.datasource.druid.stat-view-servlet.login-username=javaboy
Set the login password
spring.datasource.druid.stat-view-servlet.login-password=123
# whitelist (if allow is not configured or empty, allow all access)
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
# blacklist (deny takes precedence over Allow, if in deny list, even in allow list, will be rejected)
spring.datasource.druid.stat-view-servlet.deny=
Copy the code

Configure the page address, configure the blacklist and whitelist.

Note that the reset button is displayed even if the reset-enable property is set to false, but clicking the button does not reset it.

Well, we’re done here.

3. The test

Ok, let’s start the Spring Boot project to test.

After the Spring Boot project is successfully started, first access the following address:

  • http://localhost:8080/druid/login.html

At this point we should see the login authentication page as follows:

Enter the user name/password (Javaboy /123) configured previously for login. After successful login, you can see the following page:

As you can see from the title bar, the data source, SQL monitoring, SQL firewall, and so on are all in one place.

Next we go to http://localhost:8080/user? Select * from ‘aaa’ where ‘aaa’ = ‘aaa’;

As you can see, the SQL execution is now monitored.

All the other surveillance data is available, and I’m not going to go through them all. If you feel that the data shown here is not intuitive and want to draw your own HTML page, you can also click on the LAST JSON API, you can see the JSON address of each monitoring item, hold the JSON and display it as you want.

4. To advertising

If you want to directly use this monitoring page, which has alibaba’s advertisement, as shown in the picture below, it is particularly awkward for the company to use:

We might want to get rid of the AD, which is easy.

First, after analysis, we found that the ads were built from a file called common.js located in druid-1.2.8.jar! HTTP / / support/resources/js/common js here, common. Js file has the following lines:

init : function() {
	this.buildFooter();
	druid.lang.init();
},
buildFooter : function() {
	var html =' ';
	$(document.body).append(html);
},
Copy the code

The logic is that buildFooter is responsible for building the ads at the end of the page, and calls to buildFooter are done in init.

So to get rid of ads, don’t call buildFooter.

So our idea of unadvertising is very simple. Write a filter, block the request for common.js, and modify it a little bit as follows:

@WebFilter(urlPatterns = "/druid/js/common.js")
public class RemoveAdFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String text = Utils.readFromResource("support/http/resources/js/common.js");
        text = text.replace("this.buildFooter();".""); servletResponse.getWriter().write(text); }}Copy the code

This filter intercepts the /druid/js/common.js request, then reads the common.js file and manually replaces this.buildfooter (); That will do. Just write out the document at the end.

Of course, remember to scan the Filter in the startup class as follows:

@SpringBootApplication
@ServletComponentScan("org.javaboy.druid_monitor.filter")
public class DruidMonitorApplication {

    public static void main(String[] args) { SpringApplication.run(DruidMonitorApplication.class, args); }}Copy the code

Well, there you go. With this filter, there’s no advertising.

Druid_monitor can download this example.

References:

  • Github.com/alibaba/dru…