zhihu-spider之Druid——zhihu-spider开源项目使用技术详解(其三)

2023-10-30 11:10

本文主要是介绍zhihu-spider之Druid——zhihu-spider开源项目使用技术详解(其三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

zhihu-spider之Druid——zhihu-spider开源项目使用技术详解(其三)

1.Druid简介

Druid是一个JDBC组件,它包括三部分:
  • DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系。

  • DruidDataSource 高效可管理的数据库连接池。

  • SQLParser

Druid可以做什么?
  • 可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。

  • 替换DBCP和C3P0。Druid提供了一个高效、功能强大、可扩展性好的数据库连接池。

  • 数据库密码加密。直接把数据库密码写在配置文件中,这是不好的行为,容易导致安全问题。DruidDruiver和DruidDataSource都支持PasswordCallback。

  • SQL执行日志,Druid提供了不同的LogFilter,能够支持Common-Logging、Log4j和JdkLog,你可以按需要选择相应的LogFilter,监控你应用的数据库访问情况。

  • 扩展JDBC,如果你要对JDBC层有编程的需求,可以通过Druid提供的Filter-Chain机制,很方便编写JDBC层的扩展插件。

  官方地址:https://github.com/alibaba/druid/wiki/%E9%A6%96%E9%A1%B5

  github地址:https://github.com/alibaba/druid

  Druid常见问题:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

2.Druid配置

//gradle依赖如下compile "com.alibaba:druid:1.0.11"
// compile "com.alibaba:druid:1.0.29"//maven依赖如下<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.11</version>
</dependency>

(1).自定义数据源使用(较低版本druid-1.0.11在spring boot中的使用)

1>.配置文件application.yml中的配置

spring:datasource:druid:driverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/zhihu_spider?useUnicode=true&characterEncoding=utf-8username: rootpassword: 555222filters: statconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

2>. 读取配置文件中配置的bean配置:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import lombok.Data;/*** 读取数据源配置信息* * @author sunzc**         2017年7月5日 下午9:57:27*/
@Component
@ConfigurationProperties(prefix = "spring.datasource.druid")
@Data
public class DatasourceProperties {private String driverClassName;private String url;private String username;private String password;private String filters;private String connectionProperties;
}

3>.配置数据源

import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.wei.you.zhihu.spider.config.property.DatasourceProperties;
import lombok.extern.slf4j.Slf4j;/*** 配置数据源* * @author sunzc**         2017年6月2日 下午8:07:07*/
@Configuration
@Slf4j
public class DatasourceConfiguration {@Autowiredprivate DatasourceProperties property;@Beanpublic DataSource dataSource() {DruidDataSource druidDataSource = new DruidDataSource();/*** 驱动配置*/druidDataSource.setDriverClassName(property.getDriverClassName());druidDataSource.setUrl(property.getUrl());druidDataSource.setUsername(property.getUsername());druidDataSource.setPassword(property.getPassword());/*** 其它链接池配置自行完成*/druidDataSource.setConnectionProperties(property.getConnectionProperties());try {druidDataSource.setFilters(property.getFilters());} catch (SQLException e) {log.error("druid configuration initialization filter", e);}return druidDataSource;}
}

(2).Spring数据源中的druid数据源类型使用(较高版本druid-1.0.29在spring boot中的使用)

只需要在application.yml中配置以下属性即可

spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/zhihu_spider?useUnicode=true&characterEncoding=utf-8username: rootpassword: 555222filters: stat,wall,log4jconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

3.配置监控统计功能

1>.配置Servlet

  SpringBoot项目中基于注解的配置如下

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;import com.alibaba.druid.support.http.StatViewServlet;/*** 配置Servlet* * @author sunzc**         2017年6月10日 上午9:40:55*/
@SuppressWarnings("serial")
@WebServlet(urlPatterns = "/monitor/sql/*", initParams={@WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)@WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)@WebInitParam(name="loginUsername",value="shanhy"),// 用户名@WebInitParam(name="loginPassword",value="shanhypwd"),// 密码@WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能})
public class DruidStatViewServlet extends StatViewServlet {
}

2>.配置Filter

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;import com.alibaba.druid.support.http.WebStatFilter;/*** 配置Filter* * @author sunzc**         2017年6月10日 上午9:40:46*/
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",initParams={@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/monitor/sql/*")// 忽略资源
})
public class DruidStatFilter extends WebStatFilter {
}

4.访问地址:http://localhost:8080/monitor/sql/sql.html

查看数据源及SQL统计等访问结果示例如下

这里写图片描述

5.项目的开源地址

https://github.com/sdc1234/zhihu-spider

这篇关于zhihu-spider之Druid——zhihu-spider开源项目使用技术详解(其三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/307522

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删