日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

玩转 Spring Boot 应用篇(序列号生成器服务实现)

發布時間:2024/8/1 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 玩转 Spring Boot 应用篇(序列号生成器服务实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

0.1.?背景

在微服務盛行的當下,模塊拆分粒度越來越細,若排查問題時,就需要一個能貫穿始終的?全局唯一的 ID?;在支付場景中的訂單編號,銀行流水號等生成均需要依賴序列號生成的工具。

本次基于 Spring Boot?+ Redis + Lua 來實現一個序列號生成器服務,并嘗試包裝成 Spring Boot Starter 進而徹底解決項目中序列號生成的難題。

  • 技術棧:Spring Boot 2.6.3 + Redis + Lua

  • 環境依賴:??JDK 1.8 + Maven 3.6.3

1.??搭建序列號生成服務

  • 項目結構一覽

  • 引入依賴

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>idgenerator</artifactId> <version>0.0.1</version> <name>idgenerator</name> <description>Id generator for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.21.0</version> <configuration> <!--默認關掉單元測試 --> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> </project>
  • 添加 Redis 相關配置

在 application.properties 文件中加入 redis 相關配置。

### Redis 緩存配置信息 # 主機名稱 spring.redis.host=127.0.0.1 # 端口號 spring.redis.port=6379 # 認證密碼 spring.redis.password= # 連接超時時間 spring.redis.timeout=500 # 默認數據庫 spring.redis.database=0
  • 編寫 Lua 腳本

在 resources 目錄下創建 redis-script-single.lua 文件,內容如下。

-- moudle tag local tag = KEYS[1]; if tag == nil then tag = 'default'; end -- if user do not pass shardId, default partition is 0. local partition if KEYS[2] == nil then partition = 0; else partition = KEYS[2] % 4096; endlocal seqKey = 'idgenerator_' .. tag .. '_' .. partition; local step = 1;local count; repeat count = tonumber(redis.call('INCRBY', seqKey, step)); until count < (1024 - step)-- count how many seq are generated in one millisecond if count == step then redis.call('PEXPIRE', seqKey, 1); endlocal now = redis.call('TIME'); -- second, microSecond, partition, seq return { tonumber(now[1]), tonumber(now[2]), partition, count }

重點關注 redis.call('INCRBY', seqKey, step)? 作用是對 seqKey 按照 step 步長進行遞增;以及??re?dis.call('PEXPIRE?',?seqKey, 1); 設置 seqKey 的失效時間,可依據需求是否需要。

  • Redis 腳本支持類定義(?ScriptConfiguration.java?)

創建 RedisScript 的子類 DefaultRedisScript 對象,內部設置了 lua 文件的位置以及腳本返回格式。

package com.example.idgenerator.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.data.redis.core.script.RedisScript; import org.springframework.scripting.support.ResourceScriptSource;import java.util.List;@Configuration public class ScriptConfiguration { @Bean public RedisScript<List> redisScript() { Resource resource = new ResourceScriptSource(new ClassPathResource("redis-script-single.lua")).getResource(); return RedisScript.of(resource, List.class); } }
  • 定義序列號 Service(IdGenService.java)

package com.example.idgenerator.service;/** * 序列號生成器 Service */ public interface IdGenService { String next(); }
  • 定義序列號 Service 實現(?RedisIdGenService.java?)

package com.example.idgenerator.service.impl;import com.example.idgenerator.service.IdGenService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.script.RedisScript; import org.springframework.stereotype.Service;import java.util.ArrayList; import java.util.List;@Service public class RedisIdGenService implements IdGenService { private Logger logger = LoggerFactory.getLogger(RedisIdGenService.class); @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisScript<List> redisScript; public String next() { List<String> keys = new ArrayList<>(); //keys.add("USER_MOUDLE"); //keys.add("1"); List<Long> result = stringRedisTemplate.execute(redisScript, keys); long id = buildId(result.get(0), result.get(1), result.get(2), result.get(3)); logger.info("序列號:" + id); return String.valueOf(id); } public long buildId(long second, long microSecond, long shardId, long seq) { long miliSecond = second * 1000L + microSecond / 1000L; return (miliSecond << 22) + (shardId << 10) + seq; } }
  • 定義序列號 API(?IdGenController.java?)

package com.example.idgenerator.controller;import com.example.idgenerator.service.IdGenService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class IdGenController { private Logger logger = LoggerFactory.getLogger(IdGenController.class); @Autowired private IdGenService idGenService; @GetMapping("/getId") public String getId() { String seq = idGenService.next(); logger.info("生成序列號:" + seq); return seq; } }
  • 啟動服務驗證

啟動服務,瀏覽器訪問??http://localhost:8080/getId,控制臺輸出:

至此,一個基于 Spring Boot 的序列號生成器服務就完成了,可以直接集成到項目中去使用,不過是提供 HTTP 的服務,若不直接提供 WEB 服務,考慮到使用方便,是否可以考慮封裝成 starter 呢?

2.??包裝成序列號生成器 starter

考慮到直觀,直接新建項目,項目名:idgenerator-spring-boot-starter,項目整體結構如下。

  • 添加依賴

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.idgenerator</groupId> <artifactId>idgenerator-spring-boot-starter</artifactId> <version>0.0.1</version> <name>idgenerator-spring-boot-starter</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> </project>
  • 添加 Redis 相關配置

### Redis 緩存配置信息 # 主機名稱 spring.redis.host=127.0.0.1 # 端口號 spring.redis.port=6379 # 認證密碼 spring.redis.password= # 連接超時時間 spring.redis.timeout=500 # 默認數據庫 spring.redis.database=0
  • 編寫 Lua 腳本

-- moudle tag local tag = KEYS[1]; if tag == nil then tag = 'default'; end -- if user do not pass shardId, default partition is 0. local partition if KEYS[2] == nil then partition = 0; else partition = KEYS[2] % 4096; endlocal seqKey = 'idgenerator_' .. tag .. '_' .. partition; local step = 1;local count; repeat count = tonumber(redis.call('INCRBY', seqKey, step)); until count < (1024 - step)-- count how many seq are generated in one millisecond if count == step then redis.call('PEXPIRE', seqKey, 1); endlocal now = redis.call('TIME'); -- second, microSecond, partition, seq return { tonumber(now[1]), tonumber(now[2]), partition, count }
  • 編寫 Service 以及實現

package org.idgenerator.service;/** * 序列號生成器 Service */ public interface IdGenService { String next(); } package org.idgenerator.service.impl;import org.idgenerator.service.IdGenService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.script.RedisScript; import org.springframework.scripting.support.ResourceScriptSource; import org.springframework.stereotype.Service;import java.util.ArrayList; import java.util.List;@Service public class RedisIdGenService implements IdGenService { private Logger logger = LoggerFactory.getLogger(RedisIdGenService.class); private StringRedisTemplate stringRedisTemplate; private RedisScript<List> redisScript; public RedisIdGenService(StringRedisTemplate stringRedisTemplate) { this.stringRedisTemplate = stringRedisTemplate; Resource luaResource = new ResourceScriptSource(new ClassPathResource("redis-script-single.lua")).getResource(); RedisScript<List> redisScript = RedisScript.of(luaResource,List.class); this.redisScript = redisScript; } public String next() { List<String> keys = new ArrayList<>(); //keys.add("USER_MOUDLE"); //keys.add("1"); List<Long> result = stringRedisTemplate.execute(redisScript, keys); long id = buildId(result.get(0), result.get(1), result.get(2), result.get(3)); logger.info("序列號:" + id); return String.valueOf(id); } public long buildId(long second, long microSecond, long shardId, long seq) { long miliSecond = second * 1000L + microSecond / 1000L; return (miliSecond << 22) + (shardId << 10) + seq; } }
  • 定義 IdGenAutoConfiguration 自動配置類

package org.idgenerator.autoconfigure;import org.idgenerator.service.IdGenService; import org.idgenerator.service.impl.RedisIdGenService; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.core.StringRedisTemplate;@Configuration @ConditionalOnClass({StringRedisTemplate.class}) public class IdGenAutoConfiguration { @Bean @ConditionalOnMissingBean(IdGenService.class) public IdGenService idGen(StringRedisTemplate stringRedisTemplate) { return new RedisIdGenService(stringRedisTemplate); } }
  • 定義 spring.factories 文件

在 resources 目錄下創建 META-INF 文件夾,然后創建 spring.factories 文件,文件內容如下。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.idgenerator.autoconfigure.IdGenAutoConfiguration
  • 編譯打包

3.??序列號生成器 starter 驗證

創建 ToyApp 項目,并引入第 2 步編譯之后的序列號生成器?starter。

  • pom.xml 詳細內容。

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>ToyApp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ToyApp</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.idgenerator</groupId> <artifactId>idgenerator-spring-boot-starter</artifactId> <systemPath> ${project.basedir}/lib/idgenerator-spring-boot-starter-0.0.1.jar </systemPath> <scope>system</scope> <version>0.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
  • 編寫測試類

@SpringBootTest class DemoIdApplicationTests { @Autowired private IdGenService idGenService; @Test public void idGenTest() { System.out.println("調用自定義序列號生成器 starter 生成的序列號為:" + idGenService.next()); } }

執行后控制臺輸出如下:

調用自定義序列號生成器 starter 生成的序列號為:6919868765123379201

至此,自定義序列號生成器 starter 就驗證通過了,收工。

4.??例行回顧

本文主要是基于 Spring Boot 封裝一個序列號生成器服務 + Starter?,只需通過封裝的 Starter,就可以很輕松的在項目中生成全局唯一的序列 ID?。

總結

以上是生活随笔為你收集整理的玩转 Spring Boot 应用篇(序列号生成器服务实现)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。