Browse Source

配置第三个数据库

zyl 2 months ago
parent
commit
1196dcf4d6

+ 9 - 0
liutongyi-admin/src/main/resources/application-druid.yml

@@ -19,6 +19,15 @@ spring:
         password: 123456
         # 配置检测连接是否有效
         validationQuery: SELECT 1
+      # 从库数据源
+      slave-two:
+        # 从数据源开关/默认关闭
+        enabled: true
+        url: jdbc:sqlserver://localhost:1433;DatabaseName=test
+        username: sa
+        password: 123456
+        # 配置检测连接是否有效
+        validationQuery: SELECT 1
       # 初始连接数
       initialSize: 5
       # 最小连接池数量

+ 3 - 0
liutongyi-admin/src/main/resources/mapper/TabSystemMsgMapper.xml

@@ -14,6 +14,9 @@
         <if test="tableEnglishName != null">
             and table_english_name like concat('%',#{tableEnglishName},'%')
         </if>
+        <if test="updateType != null">
+            update_type = #{updateType}
+        </if>
 
     </select>
 

+ 6 - 1
liutongyi-common/src/main/java/com/citygis/common/enums/DataSourceType.java

@@ -15,5 +15,10 @@ public enum DataSourceType
     /**
      * 从库
      */
-    SLAVE
+    SLAVE,
+
+    /**
+     * 从库1
+     */
+    SLAVE_TWO
 }

+ 42 - 46
liutongyi-framework/src/main/java/com/citygis/framework/config/DruidConfig.java

@@ -1,20 +1,5 @@
 package com.citygis.framework.config;
 
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-import javax.servlet.Filter;
-import javax.servlet.FilterChain;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.sql.DataSource;
-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 org.springframework.context.annotation.Primary;
 import com.alibaba.druid.pool.DruidDataSource;
 import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
 import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
@@ -23,19 +8,29 @@ import com.citygis.common.enums.DataSourceType;
 import com.citygis.common.utils.spring.SpringUtils;
 import com.citygis.framework.config.properties.DruidProperties;
 import com.citygis.framework.datasource.DynamicDataSource;
+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 org.springframework.context.annotation.Primary;
+
+import javax.servlet.*;
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * druid 配置多数据源
- * 
+ *
  * @author citygis
  */
 @Configuration
-public class DruidConfig
-{
+public class DruidConfig {
     @Bean
     @ConfigurationProperties("spring.datasource.druid.master")
-    public DataSource masterDataSource(DruidProperties druidProperties)
-    {
+    public DataSource masterDataSource(DruidProperties druidProperties) {
         DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
         return druidProperties.dataSource(dataSource);
     }
@@ -43,49 +38,52 @@ public class DruidConfig
     @Bean
     @ConfigurationProperties("spring.datasource.druid.slave")
     @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
-    public DataSource slaveDataSource(DruidProperties druidProperties)
-    {
+    public DataSource slaveDataSource(DruidProperties druidProperties) {
+        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
+        return druidProperties.dataSource(dataSource);
+    }
+
+
+    @Bean
+    @ConfigurationProperties("spring.datasource.druid.slave-two")
+    @ConditionalOnProperty(prefix = "spring.datasource.druid.slave-two", name = "enabled", havingValue = "true")
+    public DataSource slaveTwoDataSource(DruidProperties druidProperties) {
         DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
         return druidProperties.dataSource(dataSource);
     }
 
     @Bean(name = "dynamicDataSource")
     @Primary
-    public DynamicDataSource dataSource(DataSource masterDataSource)
-    {
+    public DynamicDataSource dataSource(DataSource masterDataSource) {
         Map<Object, Object> targetDataSources = new HashMap<>();
         targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
         setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
+        setDataSource(targetDataSources, DataSourceType.SLAVE_TWO.name(), "slaveTwoDataSource");
         return new DynamicDataSource(masterDataSource, targetDataSources);
     }
-    
+
     /**
      * 设置数据源
-     * 
+     *
      * @param targetDataSources 备选数据源集合
-     * @param sourceName 数据源名称
-     * @param beanName bean名称
+     * @param sourceName        数据源名称
+     * @param beanName          bean名称
      */
-    public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
-    {
-        try
-        {
+    public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName) {
+        try {
             DataSource dataSource = SpringUtils.getBean(beanName);
             targetDataSources.put(sourceName, dataSource);
-        }
-        catch (Exception e)
-        {
+        } catch (Exception e) {
         }
     }
 
     /**
      * 去除监控页面底部的广告
      */
-    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @SuppressWarnings({"rawtypes", "unchecked"})
     @Bean
     @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
-    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
-    {
+    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) {
         // 获取web监控页面的参数
         DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
         // 提取common.js的配置路径
@@ -93,16 +91,14 @@ public class DruidConfig
         String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
         final String filePath = "support/http/resources/js/common.js";
         // 创建filter进行过滤
-        Filter filter = new Filter()
-        {
+        Filter filter = new Filter() {
             @Override
-            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
-            {
+            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {
             }
+
             @Override
             public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
-                    throws IOException, ServletException
-            {
+                    throws IOException, ServletException {
                 chain.doFilter(request, response);
                 // 重置缓冲区,响应头不会被重置
                 response.resetBuffer();
@@ -113,9 +109,9 @@ public class DruidConfig
                 text = text.replaceAll("powered.*?shrek.wang</a>", "");
                 response.getWriter().write(text);
             }
+
             @Override
-            public void destroy()
-            {
+            public void destroy() {
             }
         };
         FilterRegistrationBean registrationBean = new FilterRegistrationBean();