diff --git a/shardingjdbctest/.gitignore b/shardingjdbctest/.gitignore new file mode 100644 index 0000000..682d4ed --- /dev/null +++ b/shardingjdbctest/.gitignore @@ -0,0 +1,27 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +/.idea/ +/target/ diff --git a/shardingjdbctest/pom.xml b/shardingjdbctest/pom.xml new file mode 100644 index 0000000..f2d4cc5 --- /dev/null +++ b/shardingjdbctest/pom.xml @@ -0,0 +1,136 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.9.RELEASE + + + + com.ayi + woniu-web + 0.0.1-SNAPSHOT + woniu-web + create by woniu + + jar + + + 1.8 + + + + + com.google.guava + guava + 29.0-jre + + + + org.lionsoul + ip2region + 2.7.0 + + + + org.springframework.boot + spring-boot-starter-aop + + + cn.hutool + hutool-all + 5.3.3 + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-cache + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.2 + + + + + org.apache.shardingsphere + sharding-jdbc-spring-boot-starter + 4.0.0-RC1 + + + + com.alibaba + druid + 1.1.19 + + + + + + mysql + mysql-connector-java + 8.0.29 + + + + com.alipay.sofa + runtime-sofa-boot-starter + 3.18.0 + + + + org.springframework.boot + spring-boot-starter-validation + + + + + com.alibaba + fastjson + 1.2.76 + + + com.alibaba + easyexcel + 3.0.5 + + + org.testng + testng + RELEASE + compile + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/shardingjdbctest/src/main/java/com/woniu/WoNiuApplication.java b/shardingjdbctest/src/main/java/com/woniu/WoNiuApplication.java new file mode 100644 index 0000000..97882d9 --- /dev/null +++ b/shardingjdbctest/src/main/java/com/woniu/WoNiuApplication.java @@ -0,0 +1,22 @@ +package com.woniu; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@SpringBootApplication +@EnableTransactionManagement +@EnableAsync +@MapperScan("com.woniu.dao.UserDao") +public class WoNiuApplication { + + public static void main(String[] args) { + SpringApplication.run(WoNiuApplication.class, args); + } + + + +} diff --git a/shardingjdbctest/src/main/java/com/woniu/config/ThreadPoolTaskConfig.java b/shardingjdbctest/src/main/java/com/woniu/config/ThreadPoolTaskConfig.java new file mode 100644 index 0000000..d58d712 --- /dev/null +++ b/shardingjdbctest/src/main/java/com/woniu/config/ThreadPoolTaskConfig.java @@ -0,0 +1,57 @@ +package com.woniu.config; + + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.ThreadPoolExecutor; + +@Configuration +@EnableAsync +public class ThreadPoolTaskConfig { + + @Bean("threadPoolTaskExecutor")//自定义线程池名称 + public ThreadPoolTaskExecutor threadPoolTaskExecutor() { + + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + + //线程池创建的核心线程数,线程池维护线程的最少数量,即使没有任务需要执行,也会一直存活 + executor.setCorePoolSize(16); + + //如果设置allowCoreThreadTimeout=true(默认false)时,核心线程会超时关闭 + //executor.setAllowCoreThreadTimeOut(true); + + //阻塞队列 当核心线程数达到最大时,新任务会放在队列中排队等待执行 + executor.setQueueCapacity(124); + + //最大线程池数量,当线程数>=corePoolSize,且任务队列已满时。线程池会创建新线程来处理任务 + //任务队列已满时, 且当线程数=maxPoolSize,,线程池会拒绝处理任务而抛出异常 + executor.setMaxPoolSize(64); + + //当线程空闲时间达到keepAliveTime时,线程会退出,直到线程数量=corePoolSize + //允许线程空闲时间30秒,当maxPoolSize的线程在空闲时间到达的时候销毁 + //如果allowCoreThreadTimeout=true,则会直到线程数量=0 + executor.setKeepAliveSeconds(30); + + //spring 提供的 ThreadPoolTaskExecutor 线程池,是有setThreadNamePrefix() 方法的。 + //jdk 提供的ThreadPoolExecutor 线程池是没有 setThreadNamePrefix() 方法的 + executor.setThreadNamePrefix("自定义线程池-"); + + // rejection-policy:拒绝策略:当线程数已经达到maxSize的时候,如何处理新任务 + // CallerRunsPolicy():交由调用方线程运行,比如 main 线程;如果添加到线程池失败,那么主线程会自己去执行该任务,不会等待线程池中的线程去执行, (个人推荐) + // AbortPolicy():该策略是线程池的默认策略,如果线程池队列满了丢掉这个任务并且抛出RejectedExecutionException异常。 + // DiscardPolicy():如果线程池队列满了,会直接丢掉这个任务并且不会有任何异常 + // DiscardOldestPolicy():丢弃队列中最老的任务,队列满了,会将最早进入队列的任务删掉腾出空间,再尝试加入队列 + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + + //设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean,这样这些异步任务的销毁就会先于Redis线程池的销毁 + executor.setWaitForTasksToCompleteOnShutdown(true); + //设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住。 + executor.setAwaitTerminationSeconds(60); + + executor.initialize(); + return executor; + } +} \ No newline at end of file diff --git a/shardingjdbctest/src/main/java/com/woniu/config/UserAlgorithm.java b/shardingjdbctest/src/main/java/com/woniu/config/UserAlgorithm.java new file mode 100644 index 0000000..8ce5b98 --- /dev/null +++ b/shardingjdbctest/src/main/java/com/woniu/config/UserAlgorithm.java @@ -0,0 +1,32 @@ +package com.woniu.config; + + +import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm; +import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import java.util.Collection; + +@Component +public class UserAlgorithm implements PreciseShardingAlgorithm { + @Override + public String doSharding(Collection tables, PreciseShardingValue preciseShardingValue) { + if (!StringUtils.isEmpty(preciseShardingValue)) { + Long value = preciseShardingValue.getValue(); + Long index = getIndex(value); + for (String table : tables) { + if (table.endsWith(index+"")) { + return table; + } + } + } + return null; + } + + private Long getIndex(Long value) { + return value % 2 + 1; + } + + +} diff --git a/shardingjdbctest/src/main/java/com/woniu/constant/Constants.java b/shardingjdbctest/src/main/java/com/woniu/constant/Constants.java new file mode 100644 index 0000000..68656f5 --- /dev/null +++ b/shardingjdbctest/src/main/java/com/woniu/constant/Constants.java @@ -0,0 +1,8 @@ +package com.woniu.constant; + + +public class Constants { + + public static String ADDRESS = ""; + +} diff --git a/shardingjdbctest/src/main/java/com/woniu/dao/UserDao.java b/shardingjdbctest/src/main/java/com/woniu/dao/UserDao.java new file mode 100644 index 0000000..765bf59 --- /dev/null +++ b/shardingjdbctest/src/main/java/com/woniu/dao/UserDao.java @@ -0,0 +1,15 @@ +package com.woniu.dao; + +import com.woniu.vo.User; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + + +@Mapper +public interface UserDao { + + int insertUser(User user); + + List selectUser(User user); +} diff --git a/shardingjdbctest/src/main/java/com/woniu/thread/FastJonTest.java b/shardingjdbctest/src/main/java/com/woniu/thread/FastJonTest.java new file mode 100644 index 0000000..2bd23b6 --- /dev/null +++ b/shardingjdbctest/src/main/java/com/woniu/thread/FastJonTest.java @@ -0,0 +1,54 @@ +package com.woniu.thread; + +import com.alibaba.fastjson.JSONObject; +import com.woniu.dao.UserDao; +import com.woniu.vo.User; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + + + +@Slf4j +@SpringBootTest +@RunWith(SpringRunner.class) +public class FastJonTest { + + @Autowired + private UserDao userDao; + + @Test + public void testShardingJDBCInsert() { + User user = new User(); + for(int i=0; i<10; i++) { + user.setName("小明" + i); + user.setSex(1); + if(userDao.insertUser(user) == 1) { + log.info("插入成功!"); + } else { + log.info("插入失败!"); + } + } + + } + + + + @Test + public void testShardingJDBCSelect() { + User user = new User(); + user.setSex(1); + user.setId(888188847249162241L); + List userList = userDao.selectUser(user); + log.info("查询结果:{}", JSONObject.toJSONString(userList)); + } + + + + +} diff --git a/shardingjdbctest/src/main/java/com/woniu/vo/User.java b/shardingjdbctest/src/main/java/com/woniu/vo/User.java new file mode 100644 index 0000000..f79c77c --- /dev/null +++ b/shardingjdbctest/src/main/java/com/woniu/vo/User.java @@ -0,0 +1,10 @@ +package com.woniu.vo; + +import lombok.Data; + +@Data +public class User { + private Long id; + private String name; + private Integer sex; +} diff --git a/shardingjdbctest/src/main/resources/application.properties b/shardingjdbctest/src/main/resources/application.properties new file mode 100644 index 0000000..e2eb915 --- /dev/null +++ b/shardingjdbctest/src/main/resources/application.properties @@ -0,0 +1,52 @@ + +spring.application.name=www +server.port=8081 +#1800s +server.servlet.session.timeout=1800 +spring.jackson.time-zone=GMT+8 +spring.jackson.date-format=yyyy-MM-dd HH:mm:ss + +#Դ +spring.shardingsphere.datasource.names=m1,m2 + +spring.shardingsphere.datasource.m1.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8 +spring.shardingsphere.datasource.m1.username=root +spring.shardingsphere.datasource.m1.password=123456 +spring.shardingsphere.datasource.m1.driver\class\name=com.mysql.cj.jdbc.Driver +spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource + + +spring.shardingsphere.datasource.m2.url=jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8 +spring.shardingsphere.datasource.m2.username=root +spring.shardingsphere.datasource.m2.password=123456 +spring.shardingsphere.datasource.m2.driver\class\name=com.mysql.cj.jdbc.Driver +spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource + +# ָt_userݷֲݽڵ +spring.shardingsphere.sharding.tables.t_user.actual\data\nodes=m$->{1..2}.t_user_$->{1..2} + +# ָt_userɲΪSNOWFLAKE +spring.shardingsphere.sharding.tables.t_user.key\generator.column=id +spring.shardingsphere.sharding.tables.t_user.key\generator.type=SNOWFLAKE + +# ָt_userķֿԣֿ԰ƬͷƬ㷨 +spring.shardingsphere.sharding.tables.t_user.database-strategy.inline.sharding\column=sex +spring.shardingsphere.sharding.tables.t_user.database-strategy.inline.algorithm\expression=m$->{sex % 2 + 1} + + +# ָt_userķֱԣֱ԰ƬͷƬ㷨 +spring.shardingsphere.sharding.tables.t_user.table\strategy.inline.sharding\column=id +spring.shardingsphere.sharding.tables.t_user.table\strategy.inline.algorithm\expression=t_user_$->{id % 2 + 1} + + +# ̨־ +logging.level.root=info + +# sql־ +spring.shardingsphere.props.sql.show=true + +#mapperļɨ· +mybatis.mapper-locations=classpath:/mapper/*.xml + + + diff --git a/shardingjdbctest/src/main/resources/ip2region/ip2region.xdb b/shardingjdbctest/src/main/resources/ip2region/ip2region.xdb new file mode 100644 index 0000000..9f6502b Binary files /dev/null and b/shardingjdbctest/src/main/resources/ip2region/ip2region.xdb differ diff --git a/shardingjdbctest/src/main/resources/logback-spring.xml b/shardingjdbctest/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..9b029bd --- /dev/null +++ b/shardingjdbctest/src/main/resources/logback-spring.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + + UTF-8 + + + + + + + + + + + diff --git a/shardingjdbctest/src/main/resources/mapper/UserDaoMapper.xml b/shardingjdbctest/src/main/resources/mapper/UserDaoMapper.xml new file mode 100644 index 0000000..5d9efd7 --- /dev/null +++ b/shardingjdbctest/src/main/resources/mapper/UserDaoMapper.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + insert into t_user(name, sex) values (#{name}, #{sex}) + + + + + + + + + + + diff --git a/shardingjdbctest/src/main/resources/mybatis-config.xml b/shardingjdbctest/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..b099e5d --- /dev/null +++ b/shardingjdbctest/src/main/resources/mybatis-config.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/shardingjdbctest/woniu-web.iml b/shardingjdbctest/woniu-web.iml new file mode 100644 index 0000000..2a9a778 --- /dev/null +++ b/shardingjdbctest/woniu-web.iml @@ -0,0 +1,171 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file