一、添加依赖
二、配置文件的编写
点击查看完整内容
server: port: 9001 #应用名称 spring: application: name: my-cloud-account main: allow-bean-definition-overriding: true cloud: nacos: discovery: server-addr: 8.142.19.202:8848 username: nacos password: Hq2 redis: client-type: jedis host: 8.142.19.202 password: 666666 port: 8000 jedis: pool: # 连接池最大连接数(使用负值表示没有限制) max-active: 100 # 连接池中的最大空闲连接 max-idle: 100 # 连接池中的最小空闲连接 min-idle: 100 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: 60000 # 数据源 ds0 第一个数据库 shardingsphere: datasource: #数据源名称 names: ds0 ds0: connectionTimeoutMilliseconds: 30000 driver-class-name: com.mysql.cj.jdbc.Driver idleTimeoutMilliseconds: 60000 jdbc-url: jdbc:mysql://8.142.19.202:3306/my_cloud_account?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true maintenanceIntervalMilliseconds: 30000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 minPoolSize: 50 type: com.zaxxer.hikari.HikariDataSource password: 666666 username: root props: # 打印执行的数据库以及语句 sql: show: true sharding: tables: traffic: # 指定traffic表的数据分布情况,配置数据节点,行表达式标识符使用 ${...} 或 $->{...},但前者与 Spring 本身的文件占位符冲突,所以在 Spring 环境中建议使用 $->{...} actual-data-nodes: ds0.traffic_$->{0..1} #水平分表策略+行表达式分片 table-strategy: inline: algorithm-expression: traffic_$->{ account_no % 2 } sharding-column: account_no #id生成策略 key-generator: column: id props: worker: id: ${workId} #id生成策略 type: SNOWFLAKE
三、避免生成的雪花id重复,自定义workId
package net.dbc.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; /** * @author DBC * @version 1.0.0 * @date 2022年10月08日 21:56:14 * @website dbc655.top */ @Configuration @Slf4j public class SnowFlakeWordIdConfig { /** * 动态指定sharding jdbc 的雪花算法中的属性work.id属性 * 通过调用System.setProperty()的方式实现,可用容器的 id 或者机器标识位 * workId最大值 1L << 100,就是1024,即 0<= workId < 1024 * {@link SnowflakeShardingKeyGenerator#getWorkerId()} * */ static { try { InetAddress inetAddress = Inet4Address.getLocalHost(); String hostAddressIp = inetAddress.getHostAddress(); String workId = Math.abs(hostAddressIp.hashCode()) % 1024 + ""; System.setProperty("workId", workId); log.info("workId:{}", workId); } catch (UnknownHostException e) { e.printStackTrace(); } } } // public static void main(String [] args){ // // InetAddress inetAddress = null; // try { // inetAddress = Inet4Address.getLocalHost(); // } catch (UnknownHostException e) { // e.printStackTrace(); // } // System.out.println(inetAddress.getHostAddress()); // System.out.println(inetAddress.getHostName()); // // }
可以看到对应的workId
封装自己的雪花ID生成工具类
package net.dbc.utils; import org.apache.shardingsphere.core.strategy.keygen.SnowflakeShardingKeyGenerator; /** * @author DBC * @version 1.0.0 * @date 2022年10月08日 22:08:54 * @website dbc655.top */ public class IDUtil { private static SnowflakeShardingKeyGenerator shardingKeyGenerator = new SnowflakeShardingKeyGenerator(); /** * 雪花算法生成器 * @return */ public static Comparable<?> geneSnowFlakeID(){ return shardingKeyGenerator.generateKey(); } }
简单的使用
// 生成唯一id accountDO.setAccountNo(Long.valueOf(IDUtil.geneSnowFlakeID().toString()));
本文作者为DBC,转载请注明。