diff --git a/ruoyi-admin/src/main/resources/application-dev.yml b/ruoyi-admin/src/main/resources/application-dev.yml index a4692649e..5e8bc3dbc 100644 --- a/ruoyi-admin/src/main/resources/application-dev.yml +++ b/ruoyi-admin/src/main/resources/application-dev.yml @@ -15,9 +15,6 @@ spring: login-username: ruoyi login-password: 123456 dynamic: - #是否开启读写分离 - ms: true - log: true druid: # 初始连接数 initialSize: 5 diff --git a/ruoyi-admin/src/main/resources/application-prod.yml b/ruoyi-admin/src/main/resources/application-prod.yml index 0822ec0fb..687122a2b 100644 --- a/ruoyi-admin/src/main/resources/application-prod.yml +++ b/ruoyi-admin/src/main/resources/application-prod.yml @@ -14,9 +14,6 @@ spring: login-username: ruoyi login-password: 123456 dynamic: - #是否开启读写分离 - ms: false - log: true druid: # 初始连接数 initialSize: 5 diff --git a/ruoyi-framework/src/main/java/com/baomidou/dynamic/datasource/plugin/MasterSlaveAutoRoutingPlugin.java b/ruoyi-framework/src/main/java/com/baomidou/dynamic/datasource/plugin/MasterSlaveAutoRoutingPlugin.java deleted file mode 100644 index 5f12b84ae..000000000 --- a/ruoyi-framework/src/main/java/com/baomidou/dynamic/datasource/plugin/MasterSlaveAutoRoutingPlugin.java +++ /dev/null @@ -1,135 +0,0 @@ -// -// Source code recreated from a .class file by IntelliJ IDEA -// (powered by FernFlower decompiler) -// - -package com.baomidou.dynamic.datasource.plugin; - -import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; -import com.baomidou.dynamic.datasource.ds.GroupDataSource; -import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties; -import com.baomidou.dynamic.datasource.support.HealthCheckAdapter; -import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; -import java.util.Map; -import java.util.Properties; -import javax.sql.DataSource; -import org.apache.ibatis.cache.CacheKey; -import org.apache.ibatis.executor.Executor; -import org.apache.ibatis.mapping.BoundSql; -import org.apache.ibatis.mapping.MappedStatement; -import org.apache.ibatis.mapping.SqlCommandType; -import org.apache.ibatis.plugin.Interceptor; -import org.apache.ibatis.plugin.Intercepts; -import org.apache.ibatis.plugin.Invocation; -import org.apache.ibatis.plugin.Plugin; -import org.apache.ibatis.plugin.Signature; -import org.apache.ibatis.session.ResultHandler; -import org.apache.ibatis.session.RowBounds; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Lazy; - -@Intercepts({@Signature( - type = Executor.class, - method = "query", - args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class} -), @Signature( - type = Executor.class, - method = "query", - args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class} -), @Signature( - type = Executor.class, - method = "update", - args = {MappedStatement.class, Object.class} -)}) -public class MasterSlaveAutoRoutingPlugin implements Interceptor { - private static final Logger log = LoggerFactory.getLogger(MasterSlaveAutoRoutingPlugin.class); - @Autowired - protected DataSource dynamicDataSource; - @Autowired - private DynamicDataSourceProperties properties; - @Lazy - @Autowired( - required = false - ) - private HealthCheckAdapter healthCheckAdapter; - - @Value("${spring.datasource.dynamic.log}") - private boolean masterLog; - - public MasterSlaveAutoRoutingPlugin() { - } - - public Object intercept(Invocation invocation) throws Throwable { - Object[] args = invocation.getArgs(); - MappedStatement ms = (MappedStatement)args[0]; - String pushedDataSource = null; - - Object var6; - try { - String dataSource = this.getDataSource(ms); - pushedDataSource = DynamicDataSourceContextHolder.push(dataSource); - var6 = invocation.proceed(); - if (masterLog){ - logMasterAndSlave(ms,pushedDataSource); - } - } finally { - if (pushedDataSource != null) { - DynamicDataSourceContextHolder.poll(); - } - - } - - return var6; - } - - public String getDataSource(MappedStatement mappedStatement) { - String currentDataSource = SqlCommandType.SELECT == mappedStatement.getSqlCommandType() ? "slave" : "master"; - String dataSource = null; - if (this.properties.isHealth()) { - DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource)this.dynamicDataSource; - Map currentGroupDataSources; - GroupDataSource groupDataSource; - if ("slave".equalsIgnoreCase(currentDataSource)) { - currentGroupDataSources = dynamicRoutingDataSource.getCurrentGroupDataSources(); - groupDataSource = (GroupDataSource)currentGroupDataSources.get("slave"); - String dsKey = groupDataSource.determineDsKey(); - boolean health = this.healthCheckAdapter.getHealth(dsKey); - if (health) { - dataSource = dsKey; - } else { - log.warn("从库无法连接, 请检查数据库配置, key: {}", dsKey); - } - } - - if (dataSource == null) { - currentGroupDataSources = dynamicRoutingDataSource.getCurrentGroupDataSources(); - groupDataSource = (GroupDataSource)currentGroupDataSources.get("master"); - dataSource = groupDataSource.determineDsKey(); - boolean health = this.healthCheckAdapter.getHealth(dataSource); - if (!health) { - log.warn("主库无法连接, 请检查数据库配置, key: {}", dataSource); - } - } - } else { - dataSource = currentDataSource; - } - - return dataSource; - } - - public Object plugin(Object target) { - return Plugin.wrap(target, this); - } - - public void setProperties(Properties properties) { - } - - public void logMasterAndSlave(MappedStatement ms,String pushedDataSource){ -// String logString = ms.getId().substring(ms.getId().lastIndexOf(".")+1); - log.info("查询方法:"+ms.getId()+"----"+"数据源:"+pushedDataSource); - } -} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/DruidConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/DruidConfig.java new file mode 100644 index 000000000..93895e44b --- /dev/null +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/DruidConfig.java @@ -0,0 +1,69 @@ +package com.ruoyi.framework.config; + +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; +import com.alibaba.druid.util.Utils; + +/** + * druid 配置多数据源 + * + * @author ruoyi + */ +@Configuration +public class DruidConfig +{ + /** + * 去除监控页面底部的广告 + */ + @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(javax.servlet.FilterConfig filterConfig) throws ServletException + { + } + @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("
", ""); + text = text.replaceAll("powered.*?shrek.wang", ""); + response.getWriter().write(text); + } + @Override + public void destroy() + { + } + }; + FilterRegistrationBean registrationBean = new FilterRegistrationBean(); + registrationBean.setFilter(filter); + registrationBean.addUrlPatterns(commonJsPattern); + return registrationBean; + } +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/DynamicDataSourceConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/DynamicDataSourceConfig.java deleted file mode 100644 index a007e6122..000000000 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/DynamicDataSourceConfig.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.ruoyi.framework.config; - -import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; -import com.alibaba.druid.util.Utils; -import com.baomidou.dynamic.datasource.plugin.MasterSlaveAutoRoutingPlugin; -import org.mybatis.spring.annotation.MapperScan; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import javax.servlet.*; -import java.io.IOException; - - -/** - * 在纯的读写分离环境,写操作全部是master,读操作全部是slave - * @author ding - */ -@Configuration -public class DynamicDataSourceConfig { - - @Bean - @ConditionalOnProperty(name = "spring.datasource.dynamic.ms", havingValue = "true") - public MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin(){ - System.out.println("-----------读写分离已开启------------"); - return new MasterSlaveAutoRoutingPlugin(); - } - - /** - * 去除监控页面底部的广告 - */ - @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(javax.servlet.FilterConfig filterConfig) throws ServletException - { - } - @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("
", ""); - text = text.replaceAll("powered.*?shrek.wang", ""); - response.getWriter().write(text); - } - @Override - public void destroy() - { - } - }; - FilterRegistrationBean registrationBean = new FilterRegistrationBean(); - registrationBean.setFilter(filter); - registrationBean.addUrlPatterns(commonJsPattern); - return registrationBean; - } - -}