In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Xiaobian to share with you how springboot based on DRIID to achieve data source monitoring, I believe most people do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let us go to understand it together!
As the requirements and technology continue to evolve, the Spring Boot framework is becoming more and more popular, and it appears more and more in our projects. Of course, the main reason is that the Spring Boot construction project is too cool, easy to build, simple to develop, and efficient. Today we are not here to specifically study the Spring Boot project, we are going to talk about encryption and monitoring of data sources, monitoring to say, is not monitoring there is no problem, but data source encryption is related to our system security.
For the usual learning test, we configure the database plaintext password in the project is no problem, because our data is not important, it does not matter, but in the real environment of the production platform, the configuration of plaintext password is likely to cause our database password leakage, eventually leading to our production data leakage, which also reflects the production environment data source encryption necessity. Let's take a look at how to encrypt a data source.
Creating a Spring Boot Project
I won't go into details about the creation process, here are my project dependencies:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.1.RELEASE io.githu.syske druid-datasouce-decrypt 0.0.1-SNAPSHOT druid-datasouce-decrypt Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1 mysql mysql-connector-java runtime com.alibaba druid-spring-boot-starter 1.1.10 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
If your database is Oracle, then you need to replace mysql's database driver with Oracle's driver.
Modify Spring Boot project configuration information
server: port: 8083
I use the yaml method, and then start your project, because there is no controller and other code, so there is no effect, but the project can start normally.
Encrypt data source password, create publickey
There's nothing to talk about here, I'll just put the code:
import org.junit.Test;/** * @program: druid-datasouce-decrypt * @description: * @author: liu yan * @create: 2019-12-02 18:34 */public class DBencrydtTest { @Test public void test() { String[] args = {"root"}; try { com.alibaba.druid.filter.config.ConfigTools.main(args); } catch (Exception e) { } System.out.println(); }}
To be clear, args array is placed in the password, directly run the above code, you will see the console will print the following message:
privateKey:MIIBVAIBADANBgkqhkiFWERAERFrterfgdggE6AgEAAkEAqboz+iNXPv1jgKAhDW7W+L/NwqG6GDTo49BjmlMg3WxBg4w9h5RC3oRO40EOjL7+DtEBBlCZ6OHZfZWKh27FmwIDAQABAkA/azwQszPebX/IiAzRoCDjQYf4ucV3Vg3PUgZlm7okAbsXrxz2xrdnM8Er08YKm3vUOmWQmSvaOI3CqdrK1f2BAiEA4XbEkCOxWVxbDLihyudClvrgLbZZyODlx5E2phn4gXMCIQDAtvMeJiXlGQBxFr/ci0r99FiYUeag/ZFwOjyhIzWBOQIgYg3bEqzTNn/aAUBS7QGCjlLxKDBD//7/L7nRwI9O6k0CIQCdBnUiY8MM4UpS206JzZXVR3vI4TMiinovD8THJ4E5QQIgRM1QlD1PG5YTxBxZMrLm2weBxsqXhvdJuTc1GXmoUxg=publicKey:MFwwDQYJKoZIhvcewrwerfrrgfg43534M/ojVz79Y4CgIQ1u1vi/zcKhuhg06OPQY5pTIN1sQYOMPYeEQt6ETuNBDoy+/g7RAQZQmejh3X2ViodexZsCAwEAAQ==password:O9JBjc86r9IhEoIE6jevJtgsgCXZAKCWH2UtO0tbG62zqIK5G5qJOCm1u9ju+lnno15vmq+TO5WqEWGzvkDNGg==
privateKey is your private key, publicKey is your public key, and password is your encrypted password. There are two configurations we use, one is a public key and the other is a password. The reason for configuring the public key is to decrypt it with the public key. Save the above information and use it later in the configuration of spring boot.
Add data source-related configuration
Add data source configuration information:
#Alibaba druid DataSource Configuration spring: datasource: #DataSource driver type, here is druid type: com.alibaba.druid.pool.DruidDataSource # sql script encoding: utf-8 druid: #Driver class name driver-class-name: com.mysql.cj.jdbc.Driver #Database connection password username: root #Database address url: jdbc:mysql://127.0.0.1:3307/spring? characterEncoding= UTF-8&zeroDateTimeBehavior =convertToNull&allowMultiQueries=true&serverTimezone=Asia/Shanghai #Here is the password we generated previously password: Y2YOft/vPjw/JFPkevqZZKi8pChu5ambR2ivSxgipTbL76pOoxNw3Un5Hcarbe9AqUImr+wS7YI6 TjZOVYjzA == #Here is the connection configuration, key is the publicKey connection-properties: config.decrypt=true; config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJI/xqbyvpVttxfAKulKeSTIb7tZAGaFcPyTnE2r7AHTQ8kOnqKXDda4u59umt9XBFxi7db28KxeVooB138zuRUCAwEAAQ== filter: config: #Druid enabled interceptor enabled: true #Configuration information of connection pool #Number of physical connections established during initialization initial-size: 3 #Minimum number of connections min-idle: 3 #Maximum number of connections max-active: 20 #Maximum waiting time for obtaining connection, in milliseconds max-wait: 60000 #Detect when applying for connection. If the idle time is greater than timeBetweenEvictionRunsMillis, execute validationQuery to check whether the connection is valid. test-while-idle: true #time-between-connect-error-millis: 60000 #when thread is destroyed, when the difference between the last active time and the current time of detecting the current connection is greater than this value, close the current connection min-evitable-idle-time-millis: 30000 #SQL used to check whether the connection is valid must be a query statement #select 'x' in mysql #select 1 from dual validationQuery: select 'x'#validationQuery will be executed when applying for a connection to check whether the connection is valid, enabling will reduce performance, default is true test-on-borrow: false #ValidationQuery will be executed when returning the connection to check whether the connection is valid, enabling will reduce performance, default is true test-on-return: false #cache preparedStatement, mysql5.5 + It is recommended to enable pool-prepared-statements: true #poolPrepared Statements will be automatically modified to true max-pool-prepared-statement-per-connection-size: 20 #Merge monitoring data of multiple DruidDataSources use-global-data-source-stat: false #Configure extensions #Monitor statistically intercepted filters: stat,wall,slf4j #Turn on mergeSql functionality via connectProperties properties; slow SQL logging connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #Output statistics to logs periodically and reset connection pool-related counters each time a log is output. time-between-log-stats-millis: 300000 #Configure DruidStatFilter web-stat-filter: enabled: true url-pattern: '/*' exclusions: '*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*' #Configure DruidStatServlet stat-view-servlet: #Enable StatViewServlet Default is false enabled: true url-pattern: '/druid/*' # IP whitelist (none configured or empty, all access allowed) allow: 127.0.0.1 192.168.0.1 # IP blacklist (deny takes precedence over allow when there is a common) deny: 192.168.0.128 #Disable "Reset All" function on HTML page reset-enable: false #login login-username: admin #login password login-password: admin
The above remarks have been very detailed, there are two places to emphasize here, one is the key where the configuration is publicKey, do not mismatch, one is to pay attention to validationQuery here mysql and Oracle are not the same, of course, you have to be able to remove the configuration.
The configuration information of data source monitoring is also added above, and the comments are detailed enough. After the above configuration is completed, you can start your project. If there is no error, it means that there is no problem with your configuration. If there is an error when starting, it means that there is a problem with your configuration.
After the project is started, to enter the druid data source monitoring page, just enter your project address +/druid as follows, such as my address:
http://localhost:8083/druid
Then enter the username and password you added in the configuration information, you can see the monitoring page, if you want to view sql-related monitoring information, you have to improve your own project, introduce mybatis, configure your sql.
The above is all the content of this article "How to realize data source monitoring based on DRUID in springboot". Thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.