refactor(商城): 代码优化
parent
334e17e88d
commit
d104557153
@ -0,0 +1,42 @@
|
||||
# druid数据源配置
|
||||
spring:
|
||||
datasource:
|
||||
druid:
|
||||
initialSize: 10
|
||||
# 最小连接池数量
|
||||
minIdle: 10
|
||||
# 最大连接池数量
|
||||
maxActive: 50
|
||||
# 配置获取连接等待超时的时间
|
||||
# maxWait: 60000
|
||||
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
|
||||
timeBetweenEvictionRunsMillis: 60000
|
||||
# 配置一个连接在池中最小生存的时间,单位是毫秒
|
||||
minEvictableIdleTimeMillis: 300000
|
||||
# 配置一个连接在池中最大生存的时间,单位是毫秒
|
||||
maxEvictableIdleTimeMillis: 900000
|
||||
# 配置检测连接是否有效
|
||||
validationQuery: SELECT 1
|
||||
testWhileIdle: true
|
||||
testOnBorrow: false
|
||||
testOnReturn: false
|
||||
webStatFilter:
|
||||
enabled: true
|
||||
statViewServlet:
|
||||
enabled: true
|
||||
# 设置白名单,不填则允许所有访问
|
||||
allow:
|
||||
url-pattern: /druid/*
|
||||
# 控制台管理用户名和密码
|
||||
login-username:
|
||||
login-password:
|
||||
filter:
|
||||
stat:
|
||||
enabled: true
|
||||
# 慢SQL记录
|
||||
log-slow-sql: true
|
||||
slow-sql-millis: 1000
|
||||
merge-sql: true
|
||||
wall:
|
||||
config:
|
||||
multi-statement-allow: true
|
@ -1,71 +0,0 @@
|
||||
package com.wayn.mobile.framework.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
|
||||
import com.alibaba.druid.util.Utils;
|
||||
import com.wayn.mobile.framework.config.properties.DruidProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.sql.DataSource;
|
||||
import java.io.IOException;
|
||||
|
||||
@Configuration
|
||||
public class DruidConfig {
|
||||
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("spring.datasource.druid.master")
|
||||
public DataSource dataSource(DruidProperties druidProperties) {
|
||||
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||
return druidProperties.dataSource(dataSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除监控页面底部的广告
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
|
||||
public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) {
|
||||
// 获取web监控页面的参数
|
||||
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
|
||||
// 提取common.js的配置路径
|
||||
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
|
||||
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
|
||||
final String filePath = "support/http/resources/js/common.js";
|
||||
// 创建filter进行过滤
|
||||
Filter filter = new Filter() {
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
chain.doFilter(request, response);
|
||||
// 重置缓冲区,响应头不会被重置
|
||||
response.resetBuffer();
|
||||
// 获取common.js
|
||||
String text = Utils.readFromResource(filePath);
|
||||
// 正则替换banner, 除去底部的广告信息
|
||||
text = text.replaceAll("<a.*?banner\"></a><br/>", "");
|
||||
text = text.replaceAll("powered.*?shrek.wang</a>", "");
|
||||
response.getWriter().write(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
};
|
||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
|
||||
registrationBean.setFilter(filter);
|
||||
registrationBean.addUrlPatterns(commonJsPattern);
|
||||
return registrationBean;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.wayn.mobile.framework.config;
|
||||
|
||||
import com.wayn.mobile.framework.config.properties.HikariProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class HikariCpConfig {
|
||||
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource(HikariProperties hikariProperties) {
|
||||
return hikariProperties.dataSource();
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package com.wayn.mobile.framework.config.properties;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class HikariProperties {
|
||||
|
||||
@Value("${spring.datasource.master.url}")
|
||||
private String jdbcUrl;
|
||||
@Value("${spring.datasource.master.username}")
|
||||
private String username;
|
||||
@Value("${spring.datasource.master.password}")
|
||||
private String password;
|
||||
|
||||
|
||||
@Value("${spring.datasource.hikari.poolName}")
|
||||
private String poolName;
|
||||
|
||||
@Value("${spring.datasource.hikari.minimumIdle}")
|
||||
private int minimumIdle;
|
||||
|
||||
@Value("${spring.datasource.hikari.maximumPoolSize}")
|
||||
private int maximumPoolSize;
|
||||
|
||||
@Value("${spring.datasource.hikari.autoCommit}")
|
||||
private boolean autoCommit;
|
||||
|
||||
@Value("${spring.datasource.hikari.idleTimeout}")
|
||||
private int idleTimeout;
|
||||
|
||||
@Value("${spring.datasource.hikari.maxLifetime}")
|
||||
private int maxLifetime;
|
||||
|
||||
@Value("${spring.datasource.hikari.connectionTimeout}")
|
||||
private int connectionTimeout;
|
||||
|
||||
@Value("${spring.datasource.hikari.connectionTestQuery}")
|
||||
private String connectionTestQuery;
|
||||
|
||||
@Value("${spring.datasource.hikari.cachePrepStmts}")
|
||||
private boolean cachePrepStmts;
|
||||
@Value("${spring.datasource.hikari.prepStmtCacheSize}")
|
||||
private int prepStmtCacheSize;
|
||||
@Value("${spring.datasource.hikari.prepStmtCacheSqlLimit}")
|
||||
private int prepStmtCacheSqlLimit;
|
||||
@Value("${spring.datasource.hikari.useServerPrepStmts}")
|
||||
private boolean useServerPrepStmts;
|
||||
@Value("${spring.datasource.hikari.useLocalSessionState}")
|
||||
private boolean useLocalSessionState;
|
||||
@Value("${spring.datasource.hikari.rewriteBatchedStatements}")
|
||||
private boolean rewriteBatchedStatements;
|
||||
@Value("${spring.datasource.hikari.cacheResultSetMetadata}")
|
||||
private boolean cacheResultSetMetadata;
|
||||
@Value("${spring.datasource.hikari.elideSetAutoCommits}")
|
||||
private boolean elideSetAutoCommits;
|
||||
@Value("${spring.datasource.hikari.maintainTimeStats}")
|
||||
private boolean maintainTimeStats;
|
||||
|
||||
public HikariDataSource dataSource() {
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setJdbcUrl(jdbcUrl);
|
||||
config.setUsername(username);
|
||||
config.setPassword(password);
|
||||
|
||||
config.setPoolName(poolName);
|
||||
config.setMinimumIdle(minimumIdle);
|
||||
config.setMaximumPoolSize(maximumPoolSize);
|
||||
config.setAutoCommit(autoCommit);
|
||||
config.setIdleTimeout(idleTimeout);
|
||||
config.setMaxLifetime(maxLifetime);
|
||||
config.setConnectionTimeout(connectionTimeout);
|
||||
config.setConnectionTestQuery(connectionTestQuery);
|
||||
config.addDataSourceProperty("cachePrepStmts", cachePrepStmts);
|
||||
config.addDataSourceProperty("prepStmtCacheSize", prepStmtCacheSize);
|
||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", prepStmtCacheSqlLimit);
|
||||
config.addDataSourceProperty("useServerPrepStmts", useServerPrepStmts);
|
||||
config.addDataSourceProperty("useLocalSessionState", useLocalSessionState);
|
||||
config.addDataSourceProperty("rewriteBatchedStatements", rewriteBatchedStatements);
|
||||
config.addDataSourceProperty("cacheResultSetMetadata", cacheResultSetMetadata);
|
||||
config.addDataSourceProperty("elideSetAutoCommits", elideSetAutoCommits);
|
||||
config.addDataSourceProperty("maintainTimeStats", maintainTimeStats);
|
||||
|
||||
return new HikariDataSource(config);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
# hikari数据源配置
|
||||
spring:
|
||||
datasource:
|
||||
hikari:
|
||||
poolName: hikariCP
|
||||
minimumIdle: 10
|
||||
maximumPoolSize: 50
|
||||
autoCommit: true
|
||||
idleTimeout: 10000
|
||||
maxLifetime: 30000
|
||||
connectionTimeout: 30000
|
||||
connectionTestQuery: SELECT 1
|
||||
cachePrepStmts: true
|
||||
prepStmtCacheSize: 250
|
||||
prepStmtCacheSqlLimit: 2048
|
||||
useServerPrepStmts: true
|
||||
useLocalSessionState: true
|
||||
rewriteBatchedStatements: true
|
||||
cacheResultSetMetadata: true
|
||||
cacheServerConfiguration: true
|
||||
elideSetAutoCommits: true
|
||||
maintainTimeStats: false
|
Loading…
Reference in New Issue