springboot+ShardingJDBC+mySql按時間分表添加和查詢

   上一篇寫了分庫分表以及不分庫只分表的策略,這一篇我是按照公司目前日誌是分表結構做了一個demo,以前數據庫分表,我沒有用中間件,自己硬生生做各種判斷,加各種循環,做各種表分析來查的,還是容易出錯或者不健壯,但是現在我用插件做這個demo,如果可以以後分表就可以用中間件操作了

1.在mysql中加入兩個表測試表當作日誌

2.三個測試字段,主鍵自增

3.然後老規矩還是pom

     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
      </parent>

       <!-- shardingjdbc -->
       <dependency>
           <groupId>io.shardingjdbc</groupId>
           <artifactId>sharding-jdbc-core</artifactId>
           <version>2.0.3</version>
       </dependency>

       <!-- druid數據庫連接池 -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
           <version>1.1.6</version>
       </dependency>

       <!-- MySQL的JDBC驅動包 -->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <scope>runtime</scope>
       </dependency>

4.配置文件配置數據源

server:
  port: 9999
spring:
  application:
    name: sharding-jdbc
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
    default-property-inclusion: non_null
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    ds0:
      url: jdbc:mysql://localhost:3306/mall_0?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
      driverClassName: com.mysql.cj.jdbc.Driver
      username: root
      password: root
    ds1:
      url: jdbc:mysql://xxxx:3307/mall_1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
      driverClassName: com.mysql.cj.jdbc.Driver
      username: root
      password: root

5.開始啓動注入分表規則

package com.itdf.config;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import javax.sql.DataSource;


import com.alibaba.druid.support.http.StatViewServlet;
import io.shardingjdbc.core.api.ShardingDataSourceFactory;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Lists;

import org.springframework.context.annotation.Primary;

/**
 * 配置分庫分表操作
 * 首先第一步,先到配置文件去獲取兩個數據源的連接信息
 * 第二步設置分庫分表的規則
 * 第三步將規則放入ShardingJDBC配置裏,然後可以設置分庫分表的sql顯示等等
 * 之後將數據源信息都分庫分表信息全部傳入ShardingJDBC配置,返回給我們DataSource資源
 *
 * @Author df
 * @Date 2019/8/28 14:17
 * @Version 1.0
 */
@Configuration
public class ShardingConfig {

    @Value("${spring.datasource.ds0.url}")
    private String ds0_url;

    @Value("${spring.datasource.ds0.driverClassName}")
    private String ds0_driverClassName;

    @Value("${spring.datasource.ds0.username}")
    private String ds0_username;

    @Value("${spring.datasource.ds0.password}")
    private String ds0_password;


    @Value("${spring.datasource.ds1.url}")
    private String ds1_url;

    @Value("${spring.datasource.ds1.driverClassName}")
    private String ds1_driverClassName;

    @Value("${spring.datasource.ds1.username}")
    private String ds1_username;

    @Value("${spring.datasource.ds1.password}")
    private String ds1_password;

    /**
     * shardingjdbc數據源
     *
     * @return
     */
    @Bean
    @Primary
    public DataSource dataSource() throws SQLException {
        // 封裝dataSource
        Map<String, DataSource> dataSourceMap = new HashMap<>();
        DruidDataSource dataSource0 = createDb0();
        dataSourceMap.put("ds0", dataSource0);
        DruidDataSource dataSource1 = createDb1();
        dataSourceMap.put("ds1", dataSource1);

        TableRuleConfiguration tableRuleConf = getUserTableRuleConfiguration(getTimeTable.getTimeStr(dataSource0));
        // 設置分庫分表規則
        //TableRuleConfiguration tableRuleConf = getUserTableRuleConfiguration();
        // 將規則寫入ShardingDataSource
        ShardingRuleConfiguration shardingRuleConf = new ShardingRuleConfiguration();
        shardingRuleConf.getTableRuleConfigs().add(tableRuleConf);
        Properties p = new Properties();
        p.setProperty("sql.show", Boolean.TRUE.toString());
        // 獲取數據源對象
        try {
            return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConf, new ConcurrentHashMap(), p);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根據時間規則設置分表
     *
     * @return
     */
    private TableRuleConfiguration getUserTableRuleConfiguration(String timeStr) {
        TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration();
        // 設置邏輯表
        tableRuleConfiguration.setLogicTable("test_log_");
        // 注入時先獲取所有的日誌表名稱,以便查詢時能找到對應的表結構操作
        tableRuleConfiguration.setActualDataNodes("ds0.test_log_${" + timeStr + "}");
        // 設置縱列名稱
        tableRuleConfiguration.setKeyGeneratorColumnName("test_id");
        // 因爲數據庫是test_log_201908也就是說201908日期是這種格式的,業務是添加按當前日期存儲哪種表,
        // 所以注入時要獲取當前日期作爲添加邏輯表後綴,以便添加時找到當前月的表進行插入操作        
        tableRuleConfiguration.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("test_id", "test_log_${" + getTimeTable.getCurrTime() + "}"));
        return tableRuleConfiguration;
    }

    /**
     * 注入第一個數據源
     *
     * @return
     */
    private DruidDataSource createDb0() {
        // 配置第一個數據源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(ds0_driverClassName);
        dataSource.setUrl(ds0_url);
        dataSource.setUsername(ds0_username);
        dataSource.setPassword(ds0_password);
        dataSource.setProxyFilters(Lists.newArrayList(statFilter()));
        // 每個分區最大的連接數
        dataSource.setMaxActive(20);
        // 每個分區最小的連接數
        dataSource.setMinIdle(5);
        return dataSource;
    }

    /**
     * 注入第二個數據源
     *
     * @return
     */
    private DruidDataSource createDb1() {
        // 配置第一個數據源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(ds1_driverClassName);
        dataSource.setUrl(ds1_url);
        dataSource.setUsername(ds1_username);
        dataSource.setPassword(ds1_password);
        dataSource.setProxyFilters(Lists.newArrayList(statFilter()));
        // 每個分區最大的連接數
        dataSource.setMaxActive(20);
        // 每個分區最小的連接數
        dataSource.setMinIdle(5);
        return dataSource;
    }

    @Bean
    public Filter statFilter() {
        StatFilter filter = new StatFilter();
        filter.setSlowSqlMillis(5000);
        filter.setLogSlowSql(true);
        filter.setMergeSql(true);
        return filter;
    }

    @Bean
    public ServletRegistrationBean statViewServlet() {
        //創建servlet註冊實體
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        //設置ip白名單
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
        //設置ip黑名單,如果allow與deny共同存在時,deny優先於allow
        servletRegistrationBean.addInitParameter("deny", "192.168.0.19");
        // 設置控制檯管理用戶
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "123456");
        // 是否可以重置數據
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }


}

6.用java8獲取當前時間,再獲取數據庫裏所有關於日誌表的表名進行操作

package com.itdf.config;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 獲取需要的表名
 *
 * @Author df
 * @Date 2019/8/30 15:44
 * @Version 1.0
 */
public class getTimeTable {

    public static String getTimeStr(DataSource dataSource) {
        String str = "";
        int intoNum = 0;
        PreparedStatement pstmt = null;
        try {
            pstmt = dataSource.getConnection().prepareStatement("select table_name from information_schema.tables where table_schema='mall_0' and table_name like 'test_log_%'");
            ResultSet resultSet = pstmt.executeQuery();
            while (resultSet.next()) {
                intoNum++;
                if (intoNum == 1) {
                    str = resultSet.getString("table_name").substring(9, 15);
                }
                //201908..201909需要按規定弄成這樣的格式
                if (resultSet.isLast()) {
                    str += ".." + resultSet.getString("table_name").substring(9, 15);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                pstmt.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println(str);
        return str;
    }


    /**
     * java8 LocalDate獲取當前時間
     *
     * @return
     */
    public static String getCurrTime() {
        LocalDate date = LocalDate.now();
        System.out.println("當前日期=" + date.toString());
        String dataStr = date.toString().replace("-", "");
        dataStr = dataStr.substring(0, 6);
        return dataStr;
    }
}

7.然後貼出service添加和查詢測試

    
    @Resource
    private ShardingConfig shardingConfig;

    @Override
    public void testInsert2(String test_name) throws SQLException, ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = sf.format(new Date());
        Connection connection = shardingConfig.dataSource().getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("insert into test_log_(test_name,test_time) values('" + test_name + "','" + format + "')");
        preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
    }

   
    // 根據時間查詢
    @Override
    public List getBytime(String time) throws SQLException {
        PreparedStatement pstmt = shardingConfig.dataSource().getConnection().prepareStatement("select * from test_log_ where test_time >= '" + time + " 00:00:00'");
        ResultSet resultSet = pstmt.executeQuery();
        List list = new ArrayList();
        while (resultSet.next()) {
            Map map = new HashMap();
            map.put("test_id", resultSet.getLong("test_id"));
            map.put("test_name", resultSet.getString("test_name"));
            map.put("test_time", resultSet.getTimestamp("test_time"));
            list.add(map);
        }
        resultSet.close();
        shardingConfig.dataSource().getConnection().close();
        return list;
    }

8.貼出controller

    @Autowired
    private userService userService;

    @RequestMapping("/add2")
    @ResponseBody
    public String add2(String test_name) {
        try {
            userService.testInsert2(test_name);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping("/getBytime")
    @ResponseBody
    public List getBytime(String time) {
        try {
            return userService.getBytime(time);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

9,訪問添加接口

10.查看數據庫,那麼插入成功,因爲當前時2019年8月,所以它存儲的是8月份的表哦

11.查看查詢接口,查詢成功,接口查詢是大於8月28號的,2019日誌表也有數據,也可以查詢出來

 

12.查看控制檯shardingjdbc的sql日誌也可發現。

好了,就到這裏了,很簡單把,shardingjdbc還是很方便的

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章