diff --git a/strategydesign/.gitignore b/strategydesign/.gitignore new file mode 100644 index 0000000..682d4ed --- /dev/null +++ b/strategydesign/.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/strategydesign/pom.xml b/strategydesign/pom.xml new file mode 100644 index 0000000..34daee9 --- /dev/null +++ b/strategydesign/pom.xml @@ -0,0 +1,181 @@ + + + 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.github.rholder + guava-retrying + 2.0.0 + + + + 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 + + + + com.alibaba + druid-spring-boot-starter + 1.2.18 + + + + + + + + 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.github.ben-manes.caffeine + caffeine + 2.9.3 + + + + 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/strategydesign/src/main/java/com/woniu/WoNiuApplication.java b/strategydesign/src/main/java/com/woniu/WoNiuApplication.java new file mode 100644 index 0000000..1ebf2e3 --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/WoNiuApplication.java @@ -0,0 +1,16 @@ +package com.woniu; + +import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + + +@SpringBootApplication(exclude = MybatisAutoConfiguration.class) +public class WoNiuApplication { + + public static void main(String[] args) { + SpringApplication.run(WoNiuApplication.class, args); + } + + +} diff --git a/strategydesign/src/main/java/com/woniu/config/MasterDataSourceConfig.java b/strategydesign/src/main/java/com/woniu/config/MasterDataSourceConfig.java new file mode 100644 index 0000000..c0f368b --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/config/MasterDataSourceConfig.java @@ -0,0 +1,45 @@ +package com.woniu.config; + +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; + +import javax.sql.DataSource; +/** + * 优雅的实现 mybatis多数据源 + * 在新增数据时除了给自己的库保留一份,还需要同步到别人的库 + */ +@Slf4j +@Configuration +@MapperScan(basePackages = {"com.woniu.dao.master"}, + sqlSessionFactoryRef = "MasterSqlSessionFactory") +public class MasterDataSourceConfig { + + @Bean(name = "MasterDataSource") + @Qualifier("MasterDataSource") + @ConfigurationProperties(prefix = "spring.datasource.master") + public DataSource clickHouseDataSource() { + return DruidDataSourceBuilder.create().build(); + } + + @Bean(name = "MasterSqlSessionFactory") + public SqlSessionFactory getSqlSessionFactory(@Qualifier("MasterDataSource") DataSource dataSource) throws Exception { + SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); + sessionFactoryBean.setDataSource(dataSource); + sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() + .getResources("classpath*:mapper/master/*.xml")); + log.info("------------------------------------------MasterDataSource 配置成功"); + return sessionFactoryBean.getObject(); + } + + + + +} diff --git a/strategydesign/src/main/java/com/woniu/config/SlaveDataSourceConfig.java b/strategydesign/src/main/java/com/woniu/config/SlaveDataSourceConfig.java new file mode 100644 index 0000000..4b4b395 --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/config/SlaveDataSourceConfig.java @@ -0,0 +1,48 @@ +package com.woniu.config; + + +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; + +import javax.sql.DataSource; + +/** + * 优雅的实现 mybatis多数据源 + * 在实际开发中会遇到多数据源的场景,比如说,在新增数据时除了给自己的库保留一份,还需要同步到别人的库 + */ +@Slf4j +@Configuration +@MapperScan(basePackages = {"com.woniu.dao.slave"}, + sqlSessionFactoryRef = "SlaveSqlSessionFactory") +public class SlaveDataSourceConfig { + + @Bean(name = "SlaveDataSource") + @Qualifier("SlaveDataSource") + @ConfigurationProperties(prefix = "spring.datasource.slave") + public DataSource clickHouseDataSource() { + return DruidDataSourceBuilder.create().build(); + } + + @Bean(name = "SlaveSqlSessionFactory") + public SqlSessionFactory getSqlSessionFactory(@Qualifier("SlaveDataSource") DataSource dataSource) throws Exception { + SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); + sessionFactoryBean.setDataSource(dataSource); + sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() + .getResources("classpath*:mapper/slave/*.xml")); + log.info("------------------------------------------SlaveDataSource 配置成功"); + return sessionFactoryBean.getObject(); + } + + + +} + diff --git a/strategydesign/src/main/java/com/woniu/constant/Constants.java b/strategydesign/src/main/java/com/woniu/constant/Constants.java new file mode 100644 index 0000000..68656f5 --- /dev/null +++ b/strategydesign/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/strategydesign/src/main/java/com/woniu/dao/master/MasterUserDao.java b/strategydesign/src/main/java/com/woniu/dao/master/MasterUserDao.java new file mode 100644 index 0000000..e3f07cf --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/dao/master/MasterUserDao.java @@ -0,0 +1,13 @@ +package com.woniu.dao.master; + +import com.woniu.vo.User; +import org.apache.ibatis.annotations.Mapper; + + +@Mapper +public interface MasterUserDao { + + int insertUser(User user); + + +} diff --git a/strategydesign/src/main/java/com/woniu/dao/slave/SlaveUserDao.java b/strategydesign/src/main/java/com/woniu/dao/slave/SlaveUserDao.java new file mode 100644 index 0000000..a6df118 --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/dao/slave/SlaveUserDao.java @@ -0,0 +1,12 @@ +package com.woniu.dao.slave; + +import com.woniu.vo.User; +import org.apache.ibatis.annotations.Mapper; + + +@Mapper +public interface SlaveUserDao { + + int insertUser(User user); + +} diff --git a/strategydesign/src/main/java/com/woniu/retry/GuavaRetryTest.java b/strategydesign/src/main/java/com/woniu/retry/GuavaRetryTest.java new file mode 100644 index 0000000..2522c64 --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/retry/GuavaRetryTest.java @@ -0,0 +1,46 @@ +package com.woniu.retry; + +import com.github.rholder.retry.*; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; + +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +//Guava Retry重试原来这么简单!根据返回值判断是否需要重试! +@Slf4j +public class GuavaRetryTest { + + private Integer num = 1; + + @Test + public void guavaRetry() { + + Retryer retryer = RetryerBuilder.newBuilder() + //无论出现什么异常,都进行重试 + .retryIfException() + //返回结果为 error时,进行重试 + .retryIfResult(result -> Objects.equals(result, "error")) + //重试等待策略:等待 2s 后再进行重试 + .withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS)) + //重试停止策略:重试达到 3 次 + .withStopStrategy(StopStrategies.stopAfterAttempt(3)) + .withRetryListener(new RetryListener() { + @Override + public void onRetry(Attempt attempt) { + System.out.println("RetryListener: 第" + attempt.getAttemptNumber() + "次调用"); + } + }) + .build(); + try { + retryer.call(() -> testGuavaRetry()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private String testGuavaRetry() { + log.info("重试次数:{}", num++); + return "error"; + } +} diff --git a/strategydesign/src/main/java/com/woniu/service/UserService.java b/strategydesign/src/main/java/com/woniu/service/UserService.java new file mode 100644 index 0000000..082e81a --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/service/UserService.java @@ -0,0 +1,34 @@ +package com.woniu.service; + +import com.woniu.dao.master.MasterUserDao; +import com.woniu.dao.slave.SlaveUserDao; +import com.woniu.vo.User; +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; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class UserService { + + + @Autowired + private MasterUserDao masterUserDao; + + @Autowired + private SlaveUserDao slaveUserDao; + + @Test + public void testUser() { + User user = new User(); + user.setId(1l); + user.setSex(1); + user.setName("蜗牛"); + masterUserDao.insertUser(user); + slaveUserDao.insertUser(user); + } + + +} diff --git a/strategydesign/src/main/java/com/woniu/strategy/CodeEnum.java b/strategydesign/src/main/java/com/woniu/strategy/CodeEnum.java new file mode 100644 index 0000000..8e87878 --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/strategy/CodeEnum.java @@ -0,0 +1,34 @@ +package com.woniu.strategy; + + +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +public enum CodeEnum { + + USER("user"), + AGE("age"), + INTEREST("interest"), + ; + + private String code; + + public String getCode() { + return code; + } + + CodeEnum(String code) { + this.code = code; + } + + private static final Map map = Arrays.stream(CodeEnum.values()).collect(Collectors.toMap(CodeEnum::getCode, Function.identity())); + + + public static CodeEnum of(String code) { + return map.get(code); + } + + +} diff --git a/strategydesign/src/main/java/com/woniu/strategy/SearchTransformService.java b/strategydesign/src/main/java/com/woniu/strategy/SearchTransformService.java new file mode 100644 index 0000000..e5f6e8f --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/strategy/SearchTransformService.java @@ -0,0 +1,66 @@ +package com.woniu.strategy; + +import cn.hutool.core.lang.Assert; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +//知道策略模式!但不会在项目里使用? +@Service +public class SearchTransformService implements InitializingBean { + +// @Autowired +// private UserTransferService userTransferService; + +// @Autowired +// private AgeTransferService ageTransferService; + +// @Autowired +// private InterestTransferService interestTransferService; + + /** + * 根据不同的编码进行转换 + * @param code + * @return + */ + public String transform2(String code) { +// if(code.equals("user")){ +// return userTransferService.transfer(); +// }else if(code.equals("age")){ +// return ageTransferService.transfer(); +// }else if(code.equals("interest")){ +// return interestTransferService.transfer(); +// } + return ""; + } + + @Autowired + private List transferServiceList; + + private Map transferServiceMap; + + @Override + // 项目启动时将实现类放入到map中去 + public void afterPropertiesSet() throws Exception { + transferServiceMap = transferServiceList + .stream() + .collect(Collectors.toMap(TransferService::transCode, Function.identity())); + } + + /** + * 根据不同的编码进行转换 + * @param code + * @return + */ + public String transform(String code) { + TransferService transferService = transferServiceMap.get(CodeEnum.of(code)); + Assert.notNull(transferService, "找不到对应的转换器"); + return transferService.transfer(); + } + +} diff --git a/strategydesign/src/main/java/com/woniu/strategy/TransferService.java b/strategydesign/src/main/java/com/woniu/strategy/TransferService.java new file mode 100644 index 0000000..24d198d --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/strategy/TransferService.java @@ -0,0 +1,8 @@ +package com.woniu.strategy; + + +public interface TransferService { + String transfer(); + + CodeEnum transCode(); +} diff --git a/strategydesign/src/main/java/com/woniu/strategy/service/AgeTransferService.java b/strategydesign/src/main/java/com/woniu/strategy/service/AgeTransferService.java new file mode 100644 index 0000000..b53a6dd --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/strategy/service/AgeTransferService.java @@ -0,0 +1,20 @@ +package com.woniu.strategy.service; + +import com.woniu.strategy.CodeEnum; +import com.woniu.strategy.TransferService; +import org.springframework.stereotype.Service; + +@Service +public class AgeTransferService implements TransferService { + @Override + public String transfer() { + return "age"; + } + + @Override + public CodeEnum transCode() { + return CodeEnum.AGE; + } +} + + diff --git a/strategydesign/src/main/java/com/woniu/strategy/service/InterestTransferService.java b/strategydesign/src/main/java/com/woniu/strategy/service/InterestTransferService.java new file mode 100644 index 0000000..808d0a8 --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/strategy/service/InterestTransferService.java @@ -0,0 +1,19 @@ +package com.woniu.strategy.service; + + +import com.woniu.strategy.CodeEnum; +import com.woniu.strategy.TransferService; +import org.springframework.stereotype.Service; + +@Service +public class InterestTransferService implements TransferService { + @Override + public String transfer() { + return "interest"; + } + + @Override + public CodeEnum transCode() { + return CodeEnum.INTEREST; + } +} diff --git a/strategydesign/src/main/java/com/woniu/strategy/service/UserTransferService.java b/strategydesign/src/main/java/com/woniu/strategy/service/UserTransferService.java new file mode 100644 index 0000000..fa69689 --- /dev/null +++ b/strategydesign/src/main/java/com/woniu/strategy/service/UserTransferService.java @@ -0,0 +1,18 @@ +package com.woniu.strategy.service; + +import com.woniu.strategy.CodeEnum; +import com.woniu.strategy.TransferService; +import org.springframework.stereotype.Service; + +@Service +public class UserTransferService implements TransferService { + @Override + public String transfer() { + return "user"; + } + + @Override + public CodeEnum transCode() { + return CodeEnum.USER; + } +} diff --git a/strategydesign/src/main/java/com/woniu/vo/User.java b/strategydesign/src/main/java/com/woniu/vo/User.java new file mode 100644 index 0000000..f79c77c --- /dev/null +++ b/strategydesign/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/strategydesign/src/main/resources/application.properties b/strategydesign/src/main/resources/application.properties new file mode 100644 index 0000000..77c6ac7 --- /dev/null +++ b/strategydesign/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/strategydesign/src/main/resources/application.yml b/strategydesign/src/main/resources/application.yml new file mode 100644 index 0000000..f747dcc --- /dev/null +++ b/strategydesign/src/main/resources/application.yml @@ -0,0 +1,14 @@ +spring: + datasource: + type: com.alibaba.druid.pool.DruidDataSource + driverClassName: com.mysql.cj.jdbc.Driver + # 主库数据源 + master: + url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + username: root + password: 123456 + slave: + url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + username: root + password: 123456 + diff --git a/strategydesign/src/main/resources/ip2region/ip2region.xdb b/strategydesign/src/main/resources/ip2region/ip2region.xdb new file mode 100644 index 0000000..9f6502b Binary files /dev/null and b/strategydesign/src/main/resources/ip2region/ip2region.xdb differ diff --git a/strategydesign/src/main/resources/logback-spring.xml b/strategydesign/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..9b029bd --- /dev/null +++ b/strategydesign/src/main/resources/logback-spring.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + + UTF-8 + + + + + + + + + + + diff --git a/strategydesign/src/main/resources/mapper/master/UserDaoMapper.xml b/strategydesign/src/main/resources/mapper/master/UserDaoMapper.xml new file mode 100644 index 0000000..e38bbbe --- /dev/null +++ b/strategydesign/src/main/resources/mapper/master/UserDaoMapper.xml @@ -0,0 +1,12 @@ + + + + + + + + insert into t_user_1(id,name, sex) values (#{id},#{name}, #{sex}) + + + + diff --git a/strategydesign/src/main/resources/mapper/slave/UserDaoMapper.xml b/strategydesign/src/main/resources/mapper/slave/UserDaoMapper.xml new file mode 100644 index 0000000..6500acb --- /dev/null +++ b/strategydesign/src/main/resources/mapper/slave/UserDaoMapper.xml @@ -0,0 +1,9 @@ + + + + + + insert into t_user_1(id,name, sex) values (#{id},#{name}, #{sex}) + + + diff --git a/strategydesign/src/main/resources/mybatis-config.xml b/strategydesign/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..b099e5d --- /dev/null +++ b/strategydesign/src/main/resources/mybatis-config.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/strategydesign/woniu-web.iml b/strategydesign/woniu-web.iml new file mode 100644 index 0000000..0a5cabd --- /dev/null +++ b/strategydesign/woniu-web.iml @@ -0,0 +1,154 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file