The contents of this paper are as follows:
There are 760 words in the article. It takes about 2 minutes to read it.
Summary
In the previous article Spring Boot Engineering Integration Global Unique ID Generator Uid Generator, a global unique ID generator based on Snowflake algorithm developed by Baidu, is recommended for you. In this paper, we recommend another excellent global unique ID generator named Vesta.
Vesta It is the open source work of Yanpeng Dalai, based on Java development, its experience address Here . Vesta is a general ID generator. The Internet is commonly known as a unified emitter. It has several advantages:
- Global uniqueness
- Rough order
- Inverse solution
- Manufacturable
- Distributed
It also supports three release modes:
- Embedded Publishing Mode
- Central Server Publishing Mode
- REST Publishing Mode
According to the performance requirements of the business, it can produce two types of ID s: maximum peak type and minimum granularity type. Its implementation architecture enables it to have the quality attributes required by Internet products such as high performance, high availability and scalability. It is a universal high-performance emitter product.
This article will play Vesta in the Spring Book project!
Note: This article was first published in My Personal Blog: CodeSheep. Program Sheep Welcome Small railway station
Foundation construction
I will not go into the construction of Spring Boot infrastructure. After the project is created, the following dependencies need to be added to the pom:
<dependency> <groupId>com.robert.vesta</groupId> <artifactId>vesta-service</artifactId> <version>0.0.1</version> </dependency> <dependency> <groupId>com.robert.vesta</groupId> <artifactId>vesta-intf</artifactId> <version>0.0.1</version> </dependency>
The corresponding Jar package can be obtained by compiling the Vesta source code. Source code here
Vesta configuration import
- Add Vesta configuration files to the project resources directory
Introduce vesta-rest.properties and configure them as follows:
vesta.machine=1021 # machine ID vesta.genMethod=0 # Generation mode, 0 means using embedded publishing mode vesta.type=1 # ID type, 1 represents the smallest granularity type
Introduce vesta-rest-main.xml and configure it as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:ext/vesta/vesta-rest.properties"/> </bean> <bean id="idService" class="com.robert.vesta.service.factory.IdServiceFactoryBean" init-method="init"> <property name="providerType" value="PROPERTY"/> <property name="type" value="${vesta.type}"/> <property name="genMethod" value="${vesta.genMethod}"/> <property name="machineId" value="${vesta.machine}"/> </bean> </beans>
Okay, let's create a Config configuration class to load the vesta-rest-main.xml configuration file into the project
- Create the UidConfig configuration class
@Configuration @ImportResource( locations = { "classpath:ext/vesta/vesta-rest-main.xml" } ) public class UidConfig { }
Writing Vesta Service
There are several important tool interfaces related to ID generator, including:
- genId Generates Global Unique ID Number
- explainId decomposes the global unique ID number and obtains JSON data that can explain the meaning of ID number.
- makeId Handmade ID
Let's look at the code.
@Service public class UidService { @Resource private IdService idService; public long genId() { return idService.genId(); } public Id explainId( long id ) { return idService.expId(id); } public long makeId( long version, long type, long genMethod, long machine, long time, long seq ) { long madeId = -1; if (time == -1 || seq == -1) throw new IllegalArgumentException( "Both time and seq are required." ); else if (version == -1) { if (type == -1) { if (genMethod == -1) { if (machine == -1) { madeId = idService.makeId(time, seq); } else { madeId = idService.makeId(machine, time, seq); } } else { madeId = idService.makeId(genMethod, machine, time, seq); } } else { madeId = idService.makeId(type, genMethod, machine, time, seq); } } else { madeId = idService.makeId(version, type, genMethod, time, seq, machine); } return madeId; } }
Writing Test Controller
We write a test interface for each of the three tool interfaces provided in UidService:
@RestController public class UidController { @Autowired private UidService uidService; @RequestMapping("/genid") public long genId() { return uidService.genId(); } @RequestMapping("/expid") public Id explainId(@RequestParam(value = "id", defaultValue = "0") long id) { return uidService.explainId( id ); } @RequestMapping("/makeid") public long makeId( @RequestParam(value = "version", defaultValue = "-1") long version, @RequestParam(value = "type", defaultValue = "-1") long type, @RequestParam(value = "genMethod", defaultValue = "-1") long genMethod, @RequestParam(value = "machine", defaultValue = "-1") long machine, @RequestParam(value = "time", defaultValue = "-1") long time, @RequestParam(value = "seq", defaultValue = "-1") long seq) { return uidService.makeId( version, type, genMethod, machine, time, seq ); } }
experimental verification
- Experiment 1
First, we call the interface genid with the browser to return the generated global unique ID pipeline number. Everything is so simple and elegant:
- Experiment two
Because the global unique pipeline number generated by Vesta has the good characteristics of reversible solution, we can form a pipeline number and then call the expid interface to reverse the meaning of the pipeline number.
Epilogue
Due to limited ability, if there are mistakes or inappropriate, please also criticize and correct, learn and communicate together!
- My Personal Blog: CodeSheep Program Sheep
- My half-year technical blog