springboot配置druid监控
启动类上加上 @ServletComponentScan注解
如果 配置的多数据源 不用默认的 配置 @SpringBootApplication(exclude=DataSourceAutoConfiguration.class)加上这个注解
application.properties 配置文件
#服务端口设置 访问的 端口
server.port=9002
#spring.datasource.useGlobalDataSourceStat=true
#MVC\u914D\u7F6E
spring.mvc.view.prefix = classpath:/templates/
spring.mvc.view.suffix = .html
datasource.secondary.type: com.alibaba.druid.pool.DruidDataSource
datasource.secondary.driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
datasource.secondary.url: jdbc:sqlserver:xxx
datasource.secondary.username: xxx
datasource.secondary.password: xxx
datasource.secondary.initialSize: 5
datasource.secondary.minIdle: 5
datasource.secondary.maxActive: 20
datasource.secondary.maxWait: 60000
datasource.secondary.timeBetweenEvictionRunsMillis: 60000
datasource.secondary.minEvictableIdleTimeMillis: 300000
datasource.secondary.validationQuery: SELECT 1 FROM DUAL
datasource.secondary.testWhileIdle: true
datasource.secondary.testOnBorrow: false
datasource.secondary.testOnReturn: false
datasource.secondary.poolPreparedStatements: true
datasource.secondary.maxPoolPreparedStatementPerConnectionSize: 20
datasource.secondary.filters: stat,wall,log4j
datasource.secondary.connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
datasource.primary.type: com.alibaba.druid.pool.DruidDataSource
datasource.primary.driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
datasource.primary.url: jdbc:sqlserver:xxx
datasource.primary.username: xxx
datasource.primary.password: xxx
datasource.primary.initialSize: 5
datasource.primary.minIdle: 5
datasource.primary.maxActive: 20
datasource.primary.maxWait: 60000
datasource.primary.timeBetweenEvictionRunsMillis: 60000
datasource.primary.minEvictableIdleTimeMillis: 300000
datasource.primary.validationQuery: SELECT 1
datasource.primary.testWhileIdle: true
datasource.primary.testOnBorrow: false
datasource.primary.testOnReturn: false
datasource.primary.poolPreparedStatements: true
datasource.primary.maxPoolPreparedStatementPerConnectionSize: 20
datasource.primary.filters: stat,wall,log4j
datasource.primary.connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
单数据源配置
DruidDBConfig 类
import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DruidDBConfig {
private Logger logger = LoggerFactory.getLogger(DruidDBConfig.class);
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
private String filters;
@Value("${spring.datasource.connectionProperties}")
private String connectionProperties;
@Bean //声明其为Bean实例
@Primary //在同样的DataSource中,首先使用被标注的DataSource
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
//configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}
DruidStatFilter 类
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略资源
}
)
public class DruidStatFilter extends WebStatFilter {
}
DruidStatViewServlet 类
@WebServlet(urlPatterns="/druid/*",
initParams={
@WebInitParam(name="allow",value=""),// IP白名单(没有配置或者为空,则允许所有访问)
/* @WebInitParam(name="deny",value="192.168.1.73"),*/// IP黑名单 (存在共同时,deny优先于allow)
@WebInitParam(name="loginUsername",value="admin"),// 用户名
@WebInitParam(name="loginPassword",value="123456"),// 密码
@WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
})
public class DruidStatViewServlet extends StatViewServlet {
private static final long serialVersionUID = -2688872071445249539L;
}
多数据源 配置
在源配置类中 配置
@Autowired
WallFilter wallFilter;
@Bean(name = "wallConfig")
WallConfig wallFilterConfig(){
WallConfig wc = new WallConfig ();
//允许sql批量操作
wc.setMultiStatementAllow(true);
wc.setNoneBaseStatementAllow(true);//允许非基本语句的其他语句
return wc;
}
@Bean(name = "wallFilter")
@DependsOn("wallConfig")
WallFilter wallFilter(WallConfig wallConfig){
WallFilter wfilter = new WallFilter();
wfilter.setConfig(wallConfig);
return wfilter;
}
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "datasource.primary")
@Primary
public DataSource druidDataSource() {
DruidDataSource datasource = new DruidDataSource();
List<Filter> filters = new ArrayList<>();
filters.add(wallFilter);
datasource.setProxyFilters(filters);
return datasource;
}