java工具类
1.IdGeneratorSnowflake.java用于生成雪花算法,分为有参构造和无参构造
package com.sale.util.common; import cn.hutool.core.lang.Snowflake; import cn.hutool.core.net.NetUtil; import cn.hutool.core.util.IdUtil; import lombok.Data; import lombok.Synchronized; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; /** * @author wjn * @version 1.0 * @date 2020/12/10 18:13 */ @Slf4j @Component public class IdGeneratorSnowflake { private long workerId =0; private long datacenterId=1; private Snowflake snowflake= IdUtil.createSnowflake(workerId,datacenterId); @PostConstruct public void init(){ try { workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr()); log.info("当前机器的workerId:{}",workerId); }catch (Exception e){ e.printStackTrace(); log.info("当前机器的workerId获取失败",e); workerId=NetUtil.getLocalhostStr().hashCode(); } } public synchronized long snowflakeId(){ return snowflake.nextId(); } public synchronized long snowflakeId(long workerId,long datacenterId){ Snowflake snowflake= IdUtil.createSnowflake(workerId,datacenterId); return snowflake.nextId(); } public static void main(String[] args) { System.out.println(new IdGeneratorSnowflake().snowflakeId()); } }
2.service层
public interface DeliverGoodsService { String getIDBySnowFlake(); }
3.serviceimpl层
@Slf4j @Service @Transactional public class DeliverGoodsServiceImpl implements DeliverGoodsService { @Autowired private IdGeneratorSnowflake idGeneratorSnowflake; @Override public String getIDBySnowFlake() { ExecutorService executorService= Executors.newFixedThreadPool(5); for (int i=1;i<=20;i++){ executorService.submit(()->{ System.out.println(idGeneratorSnowflake.snowflakeId()); }); } executorService.shutdown(); return "hello snowflake"; } }
项目设计的工具类
<!--Java工具类库-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.2</version>
</dependency>
java工具类库包含时间戳生成,图片工具类,加密工具类,ip工具类等一系列工具类,非常强大
github开源地址 https://github.com/looly/hutool/
官方中文文档 https://www.hutool.cn/docs/#/
哔哩哔哩springcloudalibaba上硅谷视频视频 https://www.bilibili.com/video/BV18E411x7eT?from=search&seid=13488436241056256401
上硅谷github地址sql及脑图 https://github.com/acloudyh/springCloud