springboot with appache sharding 3.1 單庫分表

配置文件相關信息:java

 

#開發
server.port=7200
spring.application.name=BtspIsmpServiceOrderDev

eureka.client.serviceUrl.defaultZone=http://127.0.0.1:7761/eureka/
#表示eureka client間隔多久去拉取服務器註冊信息,默認爲30秒
eureka.client.registry-fetch-interval-seconds=10
#eureka客戶端須要多長時間發送心跳給eureka服務器,代表它仍然活着,默認30秒
eureka.instance.lease-renewal-interval-in-seconds=5
#eureka服務器在接受到實力的最後一次發出的心跳後,須要等待多久才能夠將此實例刪除
eureka.instance.lease-expiration-duration-in-seconds=30
eureka.instance.health-check-url-path=/actuator/health
eureka.instance.hostname=${spring.cloud.client.ip-address}
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address=true

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS

#log
logging.config=classpath:spring-logback.xml
logging.path=logs/btspismp/serviceorder

#出現錯誤時, 直接拋出異常
spring.mvc.throw-com.eshore.exception-if-no-handler-found=true
spring.main.allow-bean-definition-overriding=true

#文件上傳大小限制,默認使用tomcat的上傳文件大小限制,即1MB
spring.multipart.maxFileSize=10
spring.multipart.maxRequestSize=10
# 數據庫訪問配置

sharding.jdbc.datasource.names=btspismp
sharding.jdbc.datasource.btspismp.driver-class-name=net.sf.log4jdbc.DriverSpy
sharding.jdbc.datasource.btspismp.jdbc-url=jdbc:log4jdbc:mysql://192.168.115.31:3306/btspIsmp?characterEncoding=UTF-8&autoReconnect=true&useUnicode=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&useSSL=false
sharding.jdbc.datasource.btspismp.username=btspIsmp
sharding.jdbc.datasource.btspismp.password=iSMp123#@!
#全部數據節點
sharding.jdbc.config.sharding.tables.T_TMP_TEST.actual-data-nodes=btspismp.T_TMP_TEST_1,btspismp.T_TMP_TEST_2
#根據這個列分表
#sharding.jdbc.config.sharding.tables.T_TMP_TEST.table-strategy.inline.sharding-column=age
#sharding.jdbc.config.sharding.tables.T_TMP_TEST.table-strategy.inline.algorithm-expression=T_TMP_TEST_$->{age %3}
#分片列
sharding.jdbc.config.sharding.tables.T_TMP_TEST.table-strategy.standard.sharding-column=age
#IN,=
sharding.jdbc.config.sharding.tables.T_TMP_TEST.table-strategy.standard.precise-algorithm-class-name=com.eshore.ismp.config.TestTableShardingConfig
#BETWEEN
#sharding.jdbc.config.sharding.tables.T_TMP_TEST.table-strategy.standard.range-algorithm-class-name=com.eshore.ismp.config.OrderAcceptTableShardingConfig
#工單分表
sharding.jdbc.config.sharding.tables.T_ORDER_ACCEPT.actual-data-nodes=btspismp.T_ORDER_ACCEPT,btspismp.T_ORDER_ACCEPT_CRBT
sharding.jdbc.config.sharding.tables.T_ORDER_ACCEPT.table-strategy.standard.sharding-column=k_785_product_type
sharding.jdbc.config.sharding.tables.T_ORDER_ACCEPT.table-strategy.standard.precise-algorithm-class-name=com.eshore.ismp.config.OrderAcceptTableShardingConfig

sharding.jdbc.datasource.btspismp.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime=30000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.leak-detection-threshold=1000

#JPA
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

#緩存配置redis
spring.cache.type=redis
spring.data.redis.repositories.enabled=false

# Redis數據庫索引(默認爲0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host=192.168.115.31
# Redis服務器鏈接端口
spring.redis.port=6379
# Redis服務器鏈接密碼(默認爲空)
spring.redis.password=
# 鏈接池最大鏈接數(使用負值表示沒有限制)
spring.redis.lettuce.pool.max-active=8
# 鏈接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.lettuce.pool.max-wait=-1ms
# 鏈接池中的最大空閒鏈接
spring.redis.lettuce.pool.max-idle=8
# 鏈接池中的最小空閒鏈接
spring.redis.lettuce.pool.min-idle=0
# 鏈接超時時間(毫秒)
spring.redis.timeout=5000ms

  

分表算法:node

package com.eshore.ismp.config;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import groovy.util.logging.Slf4j;
import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.RangeShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import io.shardingsphere.api.algorithm.sharding.standard.RangeShardingAlgorithm;

@Component
public class OrderAcceptTableShardingConfig implements PreciseShardingAlgorithm<String>{
private Logger log=LoggerFactory.getLogger(this.getClass());

	@Override
	public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
		 for(String tableName:availableTargetNames) {
			 log.info("=====tableName:"+tableName);
		 }
		 log.info("=====shardingValue:"+shardingValue);
		 log.info("====logic table name:"+shardingValue.getLogicTableName());
		 log.info("====column name:"+shardingValue.getColumnName());
		 log.info("====sharding value String:"+shardingValue.toString());
		 int start=shardingValue.toString().indexOf("value=");
		 int end=shardingValue.toString().indexOf(")");
		 log.info("length"+shardingValue.toString().length()+",start:"+start+",end:"+end);
		 log.info("Value:"+shardingValue.toString().substring(start+6,end));
		 String valueString=shardingValue.toString().substring(start+6,end);
		 if(valueString.contentEquals("SWCL")) {
			 return "T_ORDER_ACCEPT_CRBT";
		 }else {
			 return "T_ORDER_ACCEPT";
		 }
	}

	

}

  

測試類:mysql

@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseTest {

    @Autowired
    private ITbTestService tbTestService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private OrderAcceptSpsAttrJdbcDao jdbcDao;
    @Autowired
    private IOrderAcceptSpsAttrValueService orderSpsAttrValService;
    @Autowired
    private OrderAcceptStatusChangRecordJdbcDao orderAcceptStatusChangRecordJdbcDao;
    @Autowired
    private IOrderAcceptService orderAcceptService;
    @Autowired
    private ITestService testService;

@Test
    public void insertOrder() {
    	OrderAccept accept=new OrderAccept();
    	accept.setK785ProductType("SWCL");
    	accept.setCreateTime(new Date());
        orderAcceptService.save(accept);
        accept=new OrderAccept();
        accept.setK785ProductType("1");
        accept.setCreateTime(new Date());
        orderAcceptService.save(accept);
        System.out.println("============END=========");
    }
    @Test
    public void insertTest() {
    	TestEntity entity=new TestEntity();
    	entity.setAge(1);
    	testService.save(entity);
    	entity=new TestEntity();
    	entity.setAge(2);
    	testService.save(entity);
        System.out.println("============END=========");
    }
}

  

 

其餘的具體DAO和service和其餘的同樣。web

相關文章
相關標籤/搜索