|
@@ -5,15 +5,20 @@ import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
|
|
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
|
|
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
|
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import com.persagy.apm.common.context.poems.PoemsContext;
|
|
|
import com.persagy.apm.common.helper.SpringHelper;
|
|
|
import com.persagy.apm.mybatis.service.IDbService;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.sql.DataSource;
|
|
@@ -32,6 +37,7 @@ public class DynamicDataSourceHandler extends HandlerInterceptorAdapter {
|
|
|
*/
|
|
|
private static final String[] IGNORE_URL = {".html", ".js", ".css", "/swagger-resources"};
|
|
|
|
|
|
+
|
|
|
@Autowired
|
|
|
private DynamicRoutingDataSource dataSource;
|
|
|
@Autowired
|
|
@@ -40,13 +46,21 @@ public class DynamicDataSourceHandler extends HandlerInterceptorAdapter {
|
|
|
private DynamicDataSourceProperties defaultProperty;
|
|
|
@Autowired
|
|
|
private IDbService dbService;
|
|
|
+
|
|
|
+ @Value("#{'${dynamic.not-url:}'.split(',')}")
|
|
|
+ private List<String> notUrlList;
|
|
|
|
|
|
- @Override
|
|
|
+ @Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
String requestURI = request.getRequestURI();
|
|
|
if (StringUtils.containsAny(requestURI, IGNORE_URL)) {
|
|
|
return true;
|
|
|
}
|
|
|
+ for (String url : notUrlList) {
|
|
|
+ if(requestURI.contains(url)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
resetDataSource();
|
|
|
return true;
|
|
|
}
|
|
@@ -62,6 +76,11 @@ public class DynamicDataSourceHandler extends HandlerInterceptorAdapter {
|
|
|
}
|
|
|
// 默认为应用名
|
|
|
String dbNameDefault = SpringHelper.getString("spring.application.name");
|
|
|
+ String dynamicDbName = SpringHelper.getString("dynamic.db.name");
|
|
|
+ if(StringUtils.isNotBlank(dynamicDbName)) {
|
|
|
+ dbNameDefault=dynamicDbName;
|
|
|
+ }
|
|
|
+
|
|
|
// 将应用名中的中划线替换为下划线,按照公司要求应用名统一用中划线,数据库名统一用下划线
|
|
|
dbNameDefault = dbNameDefault.replace("-", "_");
|
|
|
String newDsName = dbNameDefault;
|