1. 程式人生 > >21、整合Druid數據源

21、整合Druid數據源

reset druid strac Nid 一個 界面 ring admin 127.0.0.1

1)、引入外部的數據源(Druid)

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>

2)、配置文件中切換默認的數據源

spring.datasource.url=jdbc:mysql://
127.0.0.1:3306/users?serverTimezone=GMT spring.datasource.username=root spring.datasource.password=1234 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

3)、配置其他屬性

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/users?serverTimezone=GMT
spring.datasource.username=root spring.datasource.password=1234 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #其他配置 # 下面為連接池的補充設置,應用到上面所有數據源中 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive
=20 # 配置獲取連接等待超時的時間 spring.datasource.maxWait=60000 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一個連接在池中最小生存的時間,單位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 配置監控統計攔截的filters,去掉後監控界面sql無法統計,wall用於防火墻 spring.datasource.filters=stat,wall spring.datasource.logSlowSql=true

技術分享圖片

4)、此時的配置並不能使用,需要將其加入容器
@Configuration
public class MyDruid {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return  new DruidDataSource();
    }
}

Debug查看:

技術分享圖片

5)、配置監聽

技術分享圖片

public class StatViewServlet extends ResourceServlet {
    private static final Log LOG = LogFactory.getLog(StatViewServlet.class);
    private static final long serialVersionUID = 1L;
    public static final String PARAM_NAME_RESET_ENABLE = "resetEnable";
    public static final String PARAM_NAME_JMX_URL = "jmxUrl";
    public static final String PARAM_NAME_JMX_USERNAME = "jmxUsername";
    public static final String PARAM_NAME_JMX_PASSWORD = "jmxPassword";
public abstract class ResourceServlet extends HttpServlet {
    private static final Log LOG = LogFactory.getLog(ResourceServlet.class);
    public static final String SESSION_USER_KEY = "druid-user";
    public static final String PARAM_NAME_USERNAME = "loginUsername";
    public static final String PARAM_NAME_PASSWORD = "loginPassword";
    public static final String PARAM_NAME_ALLOW = "allow";
    public static final String PARAM_NAME_DENY = "deny";
    public static final String PARAM_REMOTE_ADDR = "remoteAddress";

上面作為參考!!!!

@Configuration
public class MyDruid {


    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return  new DruidDataSource();
    }
    //配置Druid的監控
    //1、配置一個管理後臺的Servlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String,String> initParams = new HashMap<>();

        initParams.put("loginUsername","admin");
        initParams.put("loginPassword","123456");
        //允許訪問,默認所有都可訪問
        initParams.put("allow","");//默認就是允許所有訪問
        //不讓訪問
        initParams.put("deny","192.168.15.21");
        //設置初始化參數
        bean.setInitParameters(initParams);
        return bean;
    }


    //2、配置一個web監控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map<String,String> initParams = new HashMap<>();
        //排除攔截的請求
        initParams.put("exclusions","*.js,*.css,/druid/*");
        //設置初始化參數
        bean.setInitParameters(initParams);
         //攔截的請求
        bean.setUrlPatterns(Arrays.asList("/*"));

        return  bean;
    }

使用上述設置的賬號密碼進行登錄:

技術分享圖片

登錄成功之後:

技術分享圖片

執行一個查詢:

技術分享圖片

查看監控:

技術分享圖片

21、整合Druid數據源