{"id":10008154,"date":"2023-08-24T14:59:21","date_gmt":"2023-08-24T06:59:21","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008154"},"modified":"2023-08-24T14:59:21","modified_gmt":"2023-08-24T06:59:21","slug":"spring-boot-druid-%e5%ae%9e%e7%8e%b0%e7%9b%91%e6%8e%a7-mysql-%e6%80%a7%e8%83%bd%ef%bc%8c%e7%ae%80%e5%8d%95%e5%8f%88%e5%ae%9e%e7%94%a8%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008154","title":{"rendered":"Spring Boot + Druid \u5b9e\u73b0\u76d1\u63a7 MySQL \u6027\u80fd\uff0c\u7b80\u5355\u53c8\u5b9e\u7528\uff01"},"content":{"rendered":"<p>\u6765\u6e90\uff1ablog.csdn.net\/lvoelife\/article\/details\/128092586<\/p>\n<h2><strong>1. \u57fa\u672c\u6982\u5ff5<\/strong><\/h2>\n<p>\u6211\u4eec\u90fd\u4f7f\u7528\u8fc7\u8fde\u63a5\u6c60\uff0c\u6bd4\u5982<code>C3P0\uff0cDBCP\uff0chikari\uff0c Druid<\/code>\uff0c\u867d\u7136HikariCP\u7684\u901f\u5ea6\u7a0d\u5feb\uff0c\u4f46Druid\u80fd\u591f\u63d0\u4f9b\u5f3a\u5927\u7684\u76d1\u63a7\u548c\u6269\u5c55\u529f\u80fd\uff0c\u4e5f\u662f\u963f\u91cc\u5df4\u5df4\u7684\u5f00\u6e90\u9879\u76ee\u3002<\/p>\n<p>Druid\u662f\u963f\u91cc\u5df4\u5df4\u5f00\u53d1\u7684\u53f7\u79f0\u4e3a\u76d1\u63a7\u800c\u751f\u7684\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff0c\u5728\u529f\u80fd\u3001\u6027\u80fd\u3001\u6269\u5c55\u6027\u65b9\u9762\uff0c\u90fd\u8d85\u8fc7\u5176\u4ed6\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff0c\u5305\u62ec<code>DBCP\u3001C3P0\u3001BoneCP\u3001Proxool\u3001JBoss DataSource<\/code>\u7b49\u7b49\uff0c\u79d2\u6740\u4e00\u5207\u3002<\/p>\n<p>Druid\u53ef\u4ee5\u5f88\u597d\u7684\u76d1\u63a7DB\u6c60\u8fde\u63a5\u548cSQL\u7684\u6267\u884c\u60c5\u51b5\uff0c\u5929\u751f\u5c31\u662f\u9488\u5bf9\u76d1\u63a7\u800c\u751f\u7684DB\u8fde\u63a5\u6c60\u3002<\/p>\n<p>Spring Boot\u9ed8\u8ba4\u6570\u636e\u6e90<code>HikariDataSource<\/code>\u4e0e<code>JdbcTemplate<\/code>\u4e2d\u5df2\u7ecf\u4ecb\u7ecdSpring Boot 2.x\u9ed8\u8ba4\u4f7f\u7528Hikari\u6570\u636e\u6e90\uff0c\u53ef\u4ee5\u8bf4Hikari\u4e0eDriud\u90fd\u662f\u5f53\u524dJava Web\u4e0a\u6700\u4f18\u79c0\u7684\u6570\u636e\u6e90\u3002<\/p>\n<p>\u800cDruid\u5df2\u7ecf\u5728\u963f\u91cc\u5df4\u5df4\u90e8\u7f72\u4e86\u8d85\u8fc7600\u4e2a\u5e94\u7528\uff0c\u7ecf\u8fc7\u597d\u51e0\u5e74\u751f\u4ea7\u73af\u5883\u5927\u89c4\u6a21\u90e8\u7f72\u7684\u4e25\u82db\u8003\u9a8c\uff01<\/p>\n<ul>\n<li><strong>stat\uff1a<\/strong> Druid\u5185\u7f6e\u63d0\u4f9b\u4e00\u4e2a<code>StatFilter<\/code>\uff0c\u7528\u4e8e\u7edf\u8ba1\u76d1\u63a7\u4fe1\u606f\u3002<\/li>\n<li><strong>wall\uff1a<\/strong> Druid\u9632\u5fa1SQL\u6ce8\u5165\u653b\u51fb\u7684<code>WallFilter<\/code>\u5c31\u662f\u901a\u8fc7Druid\u7684SQL Parser\u5206\u6790\u3002Druid\u63d0\u4f9b\u7684<code>SQL Parser<\/code>\u53ef\u4ee5\u5728JDBC\u5c42\u62e6\u622aSQL\u505a\u76f8\u5e94\u5904\u7406\uff0c\u6bd4\u5982\u8bf4\u5206\u5e93\u5206\u8868\u3001\u5ba1\u8ba1\u7b49\u3002<\/li>\n<li><strong>log4j2\uff1a<\/strong> \u8fd9\u4e2a\u5c31\u662f \u65e5\u5fd7\u8bb0\u5f55\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u628asql\u8bed\u53e5\u6253\u5370\u5230log4j2\u4f9b\u6392\u67e5\u95ee\u9898\u3002<\/li>\n<\/ul>\n<h2><strong>2. \u76f8\u5173\u914d\u7f6e<\/strong><\/h2>\n<h4>2.1 \u6dfb\u52a0\u4f9d\u8d56<\/h4>\n<p>Spring Boot \u57fa\u7840\u5c31\u4e0d\u4ecb\u7ecd\u4e86\uff0c\u63a8\u8350\u770b\u8fd9\u4e2a\u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=9NY4EJ9yxXJ63F5UqROjgQ%3D%3D.RtDynQVhrUSdmkUqGzDQxENutOTViM%2FN7yHJnt%2Fwq1SFwjSb1om02gApENsqfsimTFDxHVu8QUIPPDZu8oe42w%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<pre><code>&lt;properties&gt;\n    &lt;java.version&gt;1.8&lt;\/java.version&gt;\n    &lt;alibabaDruidStarter.version&gt;1.2.11&lt;\/alibabaDruidStarter.version&gt;\n&lt;\/properties&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;\n    &lt;artifactId&gt;druid-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;version&gt;${alibabaDruidStarter.version}&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h4>2.2 \u914d\u7f6e\u5c5e\u6027<\/h4>\n<ul>\n<li><strong>\u914d\u7f6eDruid\u6570\u636e\u6e90\uff08\u8fde\u63a5\u6c60\uff09\uff1a<\/strong> \u5982\u540cc3p0\u3001dbcp\u6570\u636e\u6e90\u53ef\u4ee5\u8bbe\u7f6e\u6570\u636e\u6e90\u8fde\u63a5\u521d\u59cb\u5316\u5927\u5c0f\u3001\u6700\u5927\u8fde\u63a5\u6570\u3001\u7b49\u5f85\u65f6\u95f4\u3001\u6700\u5c0f\u8fde\u63a5\u6570 \u7b49\u4e00\u6837\uff0cDruid\u6570\u636e\u6e90\u540c\u7406\u53ef\u4ee5\u8fdb\u884c\u8bbe\u7f6e\u3002<\/li>\n<li><strong>\u914d\u7f6eDruid web\u76d1\u63a7filter\uff08WebStatFilter\uff09\uff1a<\/strong> \u8fd9\u4e2a\u8fc7\u6ee4\u5668\u7684\u4f5c\u7528\u5c31\u662f\u7edf\u8ba1web\u5e94\u7528\u8bf7\u6c42\u4e2d\u6240\u6709\u7684\u6570\u636e\u5e93\u4fe1\u606f\uff0c\u6bd4\u5982 \u53d1\u51fa\u7684sql\u8bed\u53e5\uff0csql\u6267\u884c\u7684\u65f6\u95f4\u3001\u8bf7\u6c42\u6b21\u6570\u3001\u8bf7\u6c42\u7684url\u5730\u5740\u3001\u4ee5\u53caseesion\u76d1\u63a7\u3001\u6570\u636e\u5e93\u8868\u7684\u8bbf\u95ee\u6b21\u6570\u7b49\u7b49\u3002<\/li>\n<li><strong>\u914d\u7f6eDruid\u540e\u53f0\u7ba1\u7406Servlet\uff08StatViewServlet\uff09\uff1a<\/strong> Druid\u6570\u636e\u6e90\u5177\u6709\u76d1\u63a7\u7684\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e2aweb\u754c\u9762\u65b9\u4fbf\u7528\u6237\u67e5\u770b\uff0c\u7c7b\u4f3c\u5b89\u88c5 \u8def\u7531\u5668 \u65f6\uff0c\u4eba\u5bb6\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u7684web\u9875\u9762\uff1b\u9700\u8981\u8bbe\u7f6eDruid\u7684\u540e\u53f0\u7ba1\u7406\u9875\u9762\u7684\u5c5e\u6027\uff0c\u6bd4\u5982 \u767b\u5f55\u8d26\u53f7\u3001\u5bc6\u7801\u7b49\u3002<\/li>\n<\/ul>\n<p>\u3010\u6ce8\u610f\u3011\uff1a<code>Druid Spring Boot Starter<\/code>\u914d\u7f6e\u5c5e\u6027\u7684\u540d\u79f0\u5b8c\u5168\u9075\u7167Druid\uff0c\u53ef\u4ee5\u901a\u8fc7Spring Boot\u914d\u7f6e\u6587\u4ef6\u6765\u914d\u7f6eDruid\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u548c\u76d1\u63a7\uff0c\u5982\u679c\u6ca1\u6709\u914d\u7f6e\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u5982\u4e0b\u5728<code>application.yml<\/code>\u914d\u7f6e\u76f8\u5173\u5c5e\u6027\uff1a<\/p>\n<pre><code># spring \u914d\u7f6e\nspring:\n  datasource:\n    driver-class-name: com.mysql.cj.jdbc.Driver\n    password: 123456\n    username: root\n    url: jdbc:mysql:\/\/localhost:3306\/superjson?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false\n    # \u8fde\u63a5\u6c60\u914d\u7f6e\n    druid:\n      # \u521d\u59cb\u5316\u5927\u5c0f\uff0c\u6700\u5c0f\uff0c\u6700\u5927\n      initial-size: 5\n      min-idle: 5\n      max-active: 20\n      # \u914d\u7f6e\u83b7\u53d6\u8fde\u63a5\u7b49\u5f85\u8d85\u65f6\u7684\u65f6\u95f4\n      max-wait: 60000\n      # \u914d\u7f6e\u95f4\u9694\u591a\u4e45\u624d\u8fdb\u884c\u4e00\u6b21\u68c0\u6d4b\uff0c\u68c0\u6d4b\u9700\u8981\u5173\u95ed\u7684\u7a7a\u95f2\u8fde\u63a5\uff0c\u5355\u4f4d\u6beb\u79d2\n      time-between-eviction-runs-millis: 60000\n      # \u914d\u7f6e\u4e00\u4e2a\u8fde\u63a5\u5728\u6c60\u4e2d\u6700\u5c0f\u751f\u5b58\u65f6\u95f4\n      min-evictable-idle-time-millis: 300000\n      validation-query: SELECT 1 FROM user\n      test-while-idle: true\n      test-on-borrow: false\n      test-on-return: false\n      # \u6253\u5f00 PSCache\uff0c\u5e76\u4e14\u6307\u5b9a\u6bcf\u4e2a\u8fde\u63a5\u4e0a PSCache \u7684\u5927\u5c0f\n      pool-prepared-statements: true\n      max-pool-prepared-statement-per-connection-size: 20\n      # \u914d\u7f6e\u76d1\u63a7\u7edf\u8ba1\u62e6\u622a\u7684 Filter\uff0c\u53bb\u6389\u540e\u76d1\u63a7\u754c\u9762 SQL \u65e0\u6cd5\u7edf\u8ba1\uff0cwall \u7528\u4e8e\u9632\u706b\u5899\n      filters: stat,wall,slf4j\n      # \u901a\u8fc7 connection-properties \u5c5e\u6027\u6253\u5f00 mergeSql \u529f\u80fd\uff1b\u6162 SQL \u8bb0\u5f55\n      connection-properties: druid.stat.mergeSql\/=true;druid.stat.slowSqlMillis\/=5000\n      # \u914d\u7f6e DruidStatFilter\n      web-stat-filter:\n        enabled: true\n        url-pattern: \/*\n        exclusions: .js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,\/druid\/*\n      # \u914d\u7f6e DruidStatViewServlet\n      stat-view-servlet:\n        url-pattern: \/druid\/*\n        # IP \u767d\u540d\u5355\uff0c\u6ca1\u6709\u914d\u7f6e\u6216\u8005\u4e3a\u7a7a\uff0c\u5219\u5141\u8bb8\u6240\u6709\u8bbf\u95ee\n        allow: 127.0.0.1\n        # IP \u9ed1\u540d\u5355\uff0c\u82e5\u767d\u540d\u5355\u4e5f\u5b58\u5728\uff0c\u5219\u4f18\u5148\u4f7f\u7528\n        deny: 192.168.31.253\n        # \u7981\u7528 HTML \u4e2d Reset All \u6309\u94ae\n        reset-enable: false\n        # \u767b\u5f55\u7528\u6237\u540d\/\u5bc6\u7801\n        login-username: root\n        login-password: 123456\n        # \u9700\u8981\u8bbe\u7f6eenabled=true,\u5426\u5219\u4f1a\u62a5\u51faThere was an unexpected error (type=Not Found, status=404).\u9519\u8bef\uff0c\u6216\u8005\u5c06druid-spring-boot-starter\u7684\u7248\u672c\u964d\u4f4e\u52301.1.10\u53ca\u4ee5\u4e0b\n        # \u662f\u5426\u542f\u7528StatViewServlet\uff08\u76d1\u63a7\u9875\u9762\uff09\u9ed8\u8ba4\u503c\u4e3afalse\uff08\u8003\u8651\u5230\u5b89\u5168\u95ee\u9898\u9ed8\u8ba4\u5e76\u672a\u542f\u52a8\uff0c\u5982\u9700\u542f\u7528\u5efa\u8bae\u8bbe\u7f6e\u5bc6\u7801\u6216\u767d\u540d\u5355\u4ee5\u4fdd\u969c\u5b89\u5168\uff09\n        enabled: true<\/code><\/pre>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u6587\u4ef6\u7684\u53c2\u6570\u53ef\u4ee5\u5728<code>com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties<\/code>\u548c <code>org.springframework.boot.autoconfigure.jdbc.DataSourcePropertie<\/code>\u4e2d\u627e\u5230\u3002<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=RB1Z%2FTKM7mSwqJgxMdm72w%3D%3D.6r7Dllm74OPtUnYC9qMeZ7j646DgLeFbEzgvQ8q4dTwK7omTD7uMtzAWSe8RlRHqq6%2BAaeGRAOLemlg4JRx4MQ%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<h4>2.3 \u914d\u7f6eFilter<\/h4>\n<p>\u53ef\u4ee5\u901a\u8fc7<code>spring.datasource.druid.filters=stat,wall,log4j ...<\/code>\u7684\u65b9\u5f0f\u6765\u542f\u7528\u76f8\u5e94\u7684\u5185\u7f6eFilter\uff0c\u4e0d\u8fc7\u8fd9\u4e9bFilter\u90fd\u662f\u9ed8\u8ba4\u914d\u7f6e\u3002\u5982\u679c\u9ed8\u8ba4\u914d\u7f6e\u4e0d\u80fd\u6ee1\u8db3\u9700\u6c42\uff0c\u53ef\u4ee5\u653e\u5f03\u8fd9\u79cd\u65b9\u5f0f\uff0c\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6765\u914d\u7f6eFilter\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code># \u914d\u7f6eStatFilter\nspring.datasource.druid.filter.stat.enabled=true\nspring.datasource.druid.filter.stat.db-type=h2\nspring.datasource.druid.filter.stat.log-slow-sql=true\nspring.datasource.druid.filter.stat.slow-sql-millis=2000\n\n# \u914d\u7f6eWallFilter\nspring.datasource.druid.filter.wall.enabled=true\nspring.datasource.druid.filter.wall.db-type=h2\nspring.datasource.druid.filter.wall.config.delete-allow=false\nspring.datasource.druid.filter.wall.config.drop-table-allow=false<\/code><\/pre>\n<p>\u76ee\u524d\u4e3a\u4ee5\u4e0bFilter\u63d0\u4f9b\u4e86\u914d\u7f6e\u652f\u6301\uff0c\u6839\u636e\uff08<code>spring.datasource.druid.filter.*<\/code>\uff09\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n<ul>\n<li>StatFilter<\/li>\n<li>WallFilter<\/li>\n<li>ConfigFilter<\/li>\n<li>EncodingConvertFilter<\/li>\n<li>Slf4jLogFilter<\/li>\n<li>Log4jFilter<\/li>\n<li>Log4j2Filter<\/li>\n<li>CommonsLogFilter<\/li>\n<\/ul>\n<p>\u4e0d\u60f3\u4f7f\u7528\u5185\u7f6e\u7684Filters\uff0c\u8981\u60f3\u4f7f\u81ea\u5b9a\u4e49Filter\u914d\u7f6e\u751f\u6548\u9700\u8981\u5c06\u5bf9\u5e94Filter\u7684enabled\u8bbe\u7f6e\u4e3atrue\uff0c<code>Druid Spring Boot Starter<\/code>\u9ed8\u8ba4\u7981\u7528StatFilter\uff0c\u53ef\u4ee5\u5c06\u5176enabled\u8bbe\u7f6e\u4e3atrue\u6765\u542f\u7528\u5b83\u3002<\/p>\n<h2><strong>3 \u76d1\u63a7\u9875\u9762<\/strong><\/h2>\n<p>\u542f\u52a8\u9879\u76ee\u540e\uff0c\u8bbf\u95ee<code>http:\/\/localhost:8081\/druid\/login.html<\/code>\u6765\u5230\u767b\u5f55\u9875\u9762\uff0c\u8f93\u5165\u7528\u6237\u540d\u5bc6\u7801\u767b\u5f55\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"384\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9oyn\" alt=\"\" title=\"\"><\/p>\n<p>\u6570\u636e\u6e90\u9875\u9762 \u662f\u5f53\u524dDataSource\u914d\u7f6e\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u4e0a\u8ff0\u914d\u7f6e\u7684Filter\u53ef\u4ee5\u5728\u91cc\u9762\u627e\u5230\uff0c\u5982\u679c\u6ca1\u6709\u914d\u7f6e Filter\uff08\u4e00\u4e9b\u4fe1\u606f\u4f1a\u65e0\u6cd5\u7edf\u8ba1\uff0c\u4f8b\u5982SQL\u76d1\u63a7\u4f1a\u65e0\u6cd5\u83b7\u53d6JDBC\u76f8\u5173\u7684SQL\u6267\u884c\u4fe1\u606f\uff09<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"349\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9oyp\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>SQL\u76d1\u63a7\u9875\u9762<\/strong><\/p>\n<p>\u7edf\u8ba1\u4e86\u6240\u6709SQL\u8bed\u53e5\u7684\u6267\u884c\u60c5\u51b5<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"90\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9oyq\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>URL\u76d1\u63a7\u9875\u9762<\/strong><\/p>\n<p>\u7edf\u8ba1\u4e86\u6240\u6709Controller\u63a5\u53e3\u7684\u8bbf\u95ee\u4ee5\u53ca\u6267\u884c\u60c5\u51b5<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"108\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9oyr\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>Spring\u76d1\u63a7\u9875\u9762<\/strong><\/p>\n<p>\u5229\u7528aop\u5bf9\u6307\u5b9a\u63a5\u53e3\u7684\u6267\u884c\u65f6\u95f4\uff0cjdbc\u6570\u8fdb\u884c\u8bb0\u5f55<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"106\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9oys\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>SQL\u9632\u706b\u5899\u9875\u9762<\/strong><\/p>\n<p>druid\u63d0\u4f9b\u4e86\u9ed1\u767d\u540d\u5355\u7684\u8bbf\u95ee\uff0c\u53ef\u4ee5\u6e05\u695a\u7684\u770b\u5230sql\u9632\u62a4\u60c5\u51b5\u3002<\/p>\n<p><strong>Session\u76d1\u63a7\u9875\u9762<\/strong><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u5f53\u524d\u7684session\u72b6\u51b5\uff0c\u521b\u5efa\u65f6\u95f4\u3001\u6700\u540e\u6d3b\u8dc3\u65f6\u95f4\u3001\u8bf7\u6c42\u6b21\u6570\u3001\u8bf7\u6c42\u65f6\u95f4\u7b49\u8be6\u7ec6\u53c2\u6570\u3002<\/p>\n<p><strong>JSONAPI\u9875\u9762<\/strong><\/p>\n<p>\u901a\u8fc7api\u7684\u5f62\u5f0f\u8bbf\u95eeDruid\u7684\u76d1\u63a7\u63a5\u53e3\uff0capi\u63a5\u53e3\u8fd4\u56deJson\u5f62\u5f0f\u6570\u636e\u3002<\/p>\n<h2><strong>4. sql\u76d1\u63a7<\/strong><\/h2>\n<p>\u914d\u7f6eDruid web\u76d1\u63a7filter\uff08<code>WebStatFilter<\/code>\uff09\u8fd9\u4e2a\u8fc7\u6ee4\u5668\uff0c\u4f5c\u7528\u5c31\u662f\u7edf\u8ba1web\u5e94\u7528\u8bf7\u6c42\u4e2d\u6240\u6709\u7684\u6570\u636e\u5e93\u4fe1\u606f\uff0c\u6bd4\u5982 \u53d1\u51fa\u7684sql\u8bed\u53e5\uff0csql\u6267\u884c\u7684\u65f6\u95f4\u3001\u8bf7\u6c42\u6b21\u6570\u3001\u8bf7\u6c42\u7684url\u5730\u5740\u3001\u4ee5\u53caseesion\u76d1\u63a7\u3001\u6570\u636e\u5e93\u8868\u7684\u8bbf\u95ee\u6b21\u6570\uff0c\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code>spring:\n  datasource:\n    druid:\n      ########## \u914d\u7f6eWebStatFilter\uff0c\u7528\u4e8e\u91c7\u96c6web\u5173\u8054\u76d1\u63a7\u7684\u6570\u636e ##########\n      web-stat-filter:\n        enabled: true                   # \u542f\u52a8 StatFilter\n        url-pattern: \/*                 # \u8fc7\u6ee4\u6240\u6709url\n        exclusions: &quot;*.js,*.gif,*.jpg,*.png,*.css,*.ico,\/druid\/*&quot; # \u6392\u9664\u4e00\u4e9b\u4e0d\u5fc5\u8981\u7684url\n        session-stat-enable: true       # \u5f00\u542fsession\u7edf\u8ba1\u529f\u80fd\n        session-stat-max-count: 1000    # session\u7684\u6700\u5927\u4e2a\u6570,\u9ed8\u8ba4100<\/code><\/pre>\n<h2><strong>5. \u6162sql\u8bb0\u5f55<\/strong><\/h2>\n<p>\u6709\u65f6\u5019\uff0c\u7cfb\u7edf\u4e2d\u6709\u4e9bSQL\u6267\u884c\u5f88\u6162\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u4e0b\u6765\uff0c\u53ef\u4ee5\u5f00\u542fDruid\u7684\u6162SQL\u8bb0\u5f55\u529f\u80fd\uff0c\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code>spring:\n  datasource:\n    druid:\n      filter:\n        stat:\n          enabled: true         # \u5f00\u542fDruidDataSource\u72b6\u6001\u76d1\u63a7\n          db-type: mysql        # \u6570\u636e\u5e93\u7684\u7c7b\u578b\n          log-slow-sql: true    # \u5f00\u542f\u6162SQL\u8bb0\u5f55\u529f\u80fd\n          slow-sql-millis: 2000 # \u9ed8\u8ba43000\u6beb\u79d2\uff0c\u8fd9\u91cc\u8d85\u8fc72s\uff0c\u5c31\u662f\u6162\uff0c\u8bb0\u5f55\u5230\u65e5\u5fd7<\/code><\/pre>\n<p>\u542f\u52a8\u540e\uff0c\u5982\u679c\u9047\u5230\u6267\u884c\u6162\u7684SQL\uff0c\u4fbf\u4f1a\u8f93\u51fa\u5230\u65e5\u5fd7\u4e2d<\/p>\n<h2><strong>6. spring \u76d1\u63a7<\/strong><\/h2>\n<p>\u8bbf\u95ee\u4e4b\u540espring\u76d1\u63a7\u9ed8\u8ba4\u662f\u6ca1\u6709\u6570\u636e\u7684\uff0c\u4f46\u9700\u8981\u5bfc\u5165SprngBoot\u7684AOP\u7684Starter\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>&lt;!--SpringBoot \u7684aop \u6a21\u5757--&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-aop&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u540c\u65f6\u9700\u8981\u5728application.yml\u6309\u5982\u4e0b\u914d\u7f6e:<\/p>\n<p>Spring\u76d1\u63a7AOP\u5207\u5165\u70b9\uff0c\u5982<code>com.springboot.template.dao.*<\/code>,\u914d\u7f6e\u591a\u4e2a\u82f1\u6587\u9017\u53f7\u5206\u9694<\/p>\n<pre><code>spring.datasource.druid.aop-patterns=&quot;com.springboot.template.dao.*&quot;<\/code><\/pre>\n<h2><strong>7. \u53bb\u5e7f\u544a(Ad)<\/strong><\/h2>\n<p>\u8bbf\u95ee\u76d1\u63a7\u9875\u9762\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u4f1a\u5728\u9875\u9762\u5e95\u90e8\uff08footer\uff09\u770b\u5230\u963f\u91cc\u5df4\u5df4\u7684\u5e7f\u544a\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"120\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9oyt\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u539f\u56e0\uff1a\u5f15\u5165\u7684druid\u7684jar\u5305\u4e2d\u7684common.js(\u91cc\u9762\u6709\u4e00\u6bb5js\u4ee3\u7801\u662f\u7ed9\u9875\u9762\u7684footer\u8ffd\u52a0\u5e7f\u544a\u7684)<\/p>\n<p>\u5982\u679c\u60f3\u53bb\u6389\uff0c\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<h4>1. \u76f4\u63a5\u624b\u52a8\u6ce8\u91ca\u8fd9\u6bb5\u4ee3\u7801<\/h4>\n<p>\u5982\u679c\u662f\u4f7f\u7528Maven\uff0c\u76f4\u63a5\u5230\u672c\u5730\u4ed3\u5e93\u4e2d\uff0c\u67e5\u627e\u8fd9\u4e2ajar\u5305\uff0c\u6ce8\u91ca\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>\/\/ this.buildFooter();<\/code><\/pre>\n<p>common.js\u7684\u4f4d\u7f6e\uff1a<\/p>\n<pre><code>com\/alibaba\/druid\/1.1.23\/druid-1.1.23.jar!\/support\/http\/resources\/js\/common.js<\/code><\/pre>\n<h4>2. \u4f7f\u7528\u8fc7\u6ee4\u5668\u8fc7\u6ee4<\/h4>\n<p>\u6ce8\u518c\u4e00\u4e2a\u8fc7\u6ee4\u5668\uff0c\u8fc7\u6ee4<code>common.js<\/code>\u7684\u8bf7\u6c42\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u76f8\u5173\u7684\u5e7f\u544a\u5185\u5bb9\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code>@Configuration\n@ConditionalOnWebApplication\n@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)\n@ConditionalOnProperty(name = &quot;spring.datasource.druid.stat-view-servlet.enabled&quot;,\nhavingValue = &quot;true&quot;, matchIfMissing = true)\npublic class RemoveDruidAdConfig {\n\n    \/**\n    * \u65b9\u6cd5\u540d: removeDruidAdFilterRegistrationBean\n    * \u65b9\u6cd5\u63cf\u8ff0 \u9664\u53bb\u9875\u9762\u5e95\u90e8\u7684\u5e7f\u544a\n    * @param properties com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties\n    * @return org.springframework.boot.web.servlet.FilterRegistrationBean\n    *\/\n    @Bean\n    public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {\n\n        \/\/ \u83b7\u53d6web\u76d1\u63a7\u9875\u9762\u7684\u53c2\u6570\n        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();\n        \/\/ \u63d0\u53d6common.js\u7684\u914d\u7f6e\u8def\u5f84\n        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : &quot;\/druid\/*&quot;;\n        String commonJsPattern = pattern.replaceAll(&quot;\/\/*&quot;, &quot;js\/common.js&quot;);\n\n        final String filePath = &quot;support\/http\/resources\/js\/common.js&quot;;\n\n        \/\/\u521b\u5efafilter\u8fdb\u884c\u8fc7\u6ee4\n        Filter filter = new Filter() {\n            @Override\n            public void init(FilterConfig filterConfig) throws ServletException {}\n\n            @Override\n            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {\n                chain.doFilter(request, response);\n                \/\/ \u91cd\u7f6e\u7f13\u51b2\u533a\uff0c\u54cd\u5e94\u5934\u4e0d\u4f1a\u88ab\u91cd\u7f6e\n                response.resetBuffer();\n                \/\/ \u83b7\u53d6common.js\n                String text = Utils.readFromResource(filePath);\n                \/\/ \u6b63\u5219\u66ff\u6362banner, \u9664\u53bb\u5e95\u90e8\u7684\u5e7f\u544a\u4fe1\u606f\n                text = text.replaceAll(&quot;&lt;a.*?banner\/&quot;&gt;&lt;\/a&gt;&lt;br\/&gt;&quot;, &quot;&quot;);\n                text = text.replaceAll(&quot;powered.*?shrek.wang&lt;\/a&gt;&quot;, &quot;&quot;);\n                response.getWriter().write(text);\n            }\n\n            @Override\n            public void destroy() {}\n        };\n\n        FilterRegistrationBean registrationBean = new FilterRegistrationBean();\n        registrationBean.setFilter(filter);\n        registrationBean.addUrlPatterns(commonJsPattern);\n        return registrationBean;\n    }\n}<\/code><\/pre>\n<p>\u4e24\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\uff0c\u5efa\u8bae\u4f7f\u7528\u7684\u662f\u7b2c\u4e00\u79cd\uff0c\u4ece\u6839\u6e90\u89e3\u51b3\u3002<\/p>\n<h2><strong>8. \u83b7\u53d6 Druid \u7684\u76d1\u63a7\u6570\u636e<\/strong><\/h2>\n<p>Druid\u7684\u76d1\u63a7\u6570\u636e\u53ef\u4ee5\u5728\u5f00\u542f<code>StatFilter<\/code>\u540e\uff0c\u901a\u8fc7<code>DruidStatManagerFacade<\/code>\u8fdb\u884c\u83b7\u53d6;<\/p>\n<p><code>DruidStatManagerFacade#getDataSourceStatDataList<\/code>\u8be5\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6240\u6709\u6570\u636e\u6e90\u7684\u76d1\u63a7\u6570\u636e\uff0c\u9664\u6b64\u4e4b\u5916<code>DruidStatManagerFacade<\/code>\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6309\u9700\u9009\u62e9\u4f7f\u7528\u3002<\/p>\n<pre><code>@RestController\n@RequestMapping(value = &quot;\/druid&quot;)\npublic class DruidStatController {\n\n    @GetMapping(&quot;\/stat&quot;)\n    public Object druidStat(){\n        \/\/ \u83b7\u53d6\u6570\u636e\u6e90\u7684\u76d1\u63a7\u6570\u636e\n        return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();\n    }\n}\n<\/code><\/pre>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=U5ucgQeyji6GVF5DaXzamg%3D%3D.n5FCBj0sJjSLIG2mB8vNE6FWc9l21IGa5qr5xG9mdrM%3D\" rel=\"noopener noreferrer\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=PfJ6MIN6Vo0wS%2F0IobvGDQ%3D%3D.jgMadu5VTnOdFokA0WeKGIVVXGSTKlrW3L3Qj1R0qzbRGZkZBI8RasQxanAACWb0Ul9XoHw%2BPseJtVJqhhSldQ%3D%3D\" rel=\"noopener noreferrer\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=di8sFwg0qdYRVWdFcxET%2Fw%3D%3D.CD8TN8VaSAdaM7gATu4KM1Qe%2FEmmzZ6gnu13jJ%2FcPmJKSIbygX9AXGOWzEA9vjga\" rel=\"noopener noreferrer\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=tVPun%2F8ZUfZMERqLYfLglg%3D%3D.PHZ%2BKIcN1jGLJTbEoTZqVKI4DLJmVG1NGRji5cq%2Bld15jt93n5ZakPajQXrNn7PX7dVD8sxYvMrm7pqhWTF7Ew%3D%3D\" rel=\"noopener noreferrer\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=%2Bysz%2FhgOw9MSv36iuQKBEg%3D%3D.9%2BGepImEGTWl2lB9FvnPwPJQuPp7aysYbNkM%2BnIlmtjvX04IsnS30B1jXX7MLDzxid7jDKukxfKsA1JQoCMgDHeo2iaZEAGeIWrWZrZiJMc%3D\" rel=\"noopener noreferrer\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044146004\">https:\/\/segmentfault.com\/a\/1190000044146004<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u6e90\uff1ablog.csdn.net\/lvoelife\/article\/details\/128092586 1.  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008154","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008154"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008154\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}