druid監控設定、AjaxDomainFilter允許跨域響應設定成commpon公共jar包 教程 火推

qq1350048638發表於2018-02-24

原始碼下載:https://gitee.com/qq1350048638/lj_common

lj_common構成
這裡寫圖片描述


別的工程在pom 引入,
相同重複的工作 就都通過lj_common 來操作,相當於把lj_common看成一個jar包

 <dependency>
    <groupId>com.lj</groupId>
    <artifactId>lj_common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>

pom.xml
把相關包引入進來,重複的全部放到這裡,方便其他工程直接呼叫即可

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lj</groupId>
  <artifactId>lj_common</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>


  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.7.RELEASE</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.7</java.version>
  </properties>

  <dependencies>
        <!-- bean掃描、自動配置、@bean定義 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- aop -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!-- 健康監控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- jdbc\連線池 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- springboot核心mybatis -->
        <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>1.2.2</version>
        </dependency>

        <!-- mvc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!-- 熱部署 -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!-- 引入druid連線池 -->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.6</version>
        </dependency>

        <!-- mysql驅動 -->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- pagehelper -->
         <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper-spring-boot-starter</artifactId>
          <version>1.2.3</version>
        </dependency>
    </dependencies>
</project>

AjaxDomainFilter類
“GET,POST,DELETE,PUT”請求全部先走這裡並通過
httpResponse.setHeader 可設定域名請求通過白名單,及請求方式;

package com.lj.ovls.common.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

@WebFilter(servletNames={"dispatcherServlet"})
public class AjaxDomainFilter implements Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest request, 
        ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("--------AjaxDomainFilter-----------");
        //設定允許跨域響應的參�?
        HttpServletResponse httpResponse = (HttpServletResponse)response;
        httpResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT");
        //放過請求繼續處理
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}

com.lj.ovls.common.druid 包 有3個類

package com.lj.ovls.common.druid;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
public class DruidConfig {

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;


    @Bean
    public DataSource createDruid(){
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        try {
            datasource.setFilters("stat");//啟動sql監控
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return datasource;
    }

}

package com.lj.ovls.common.druid;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

import com.alibaba.druid.support.http.WebStatFilter;

@WebFilter(urlPatterns="/*",initParams={
    @WebInitParam(name="exclusions",value=".js,.css,.jpg,png,/druid/*")
})
public class DruidStatFilter extends WebStatFilter{

}

package com.lj.ovls.common.druid;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

import com.alibaba.druid.support.http.StatViewServlet;


@WebServlet(urlPatterns="/druid/*",initParams={
    @WebInitParam(name="loginUsername",value="xdl"),
    @WebInitParam(name="loginPassword",value="123"),
})
public class DruidStatServlet extends StatViewServlet{

    private static final long serialVersionUID = 1L;

}

配置好之後,只需要啟動對應的工程 即可展示出監控資訊
http://localhost:8881/druid/ 賬號xdl 密碼 123 可以自己設定
這裡寫圖片描述


ResponseResult類
用來設定響應結果給前端 json返回給前端

package com.lj.ovls.common.entity;

import java.io.Serializable;

/** * 統一響應結果物件 * @author */
public class ResponseResult implements Serializable{

    /*   * 處理狀態  */
    private int status;
    /* 訊息 */
    private String msg;
    /*   * 資料    */
    private Object data;
....get set 省略

}


相關文章