需求背景
分布式ID解决方案
1. Springboot集成,使用全局唯一ID(基于Snowflake算法)
准备内容
1. 确保本机上已安装好Zookeeper了(安装教程)
通过ZK注册中心(不能使用集群版ZK),项目结合使用,在项目启动时,给每个项目分配一个唯一且全局的ID身份标识值
代码演示
项目目录结构:
1. application.yml文件
server:
port: 9090
leaf:
zk:
list: localhost:3181
注意:我把zk的默认端口2181,改成了3181
2. leaf.properties文件
可以不同应用分组,定义不同的leaf.name,比如default1(某应用组合1),default2(某应用组合2)
leaf.name=default
3. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.md</groupId>
<artifactId>spring-boot2-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>spring-boot2-distributed-id</artifactId>
<packaging>jar</packaging>
<name>spring-boot2-distributed-id</name>
<description>Spring Boot, MVC, Rest API for App</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 构建成可运行的Web项目 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib-ext-spring</artifactId>
</dependency>
<dependency>
<groupId>com.md</groupId>
<artifactId>spring-boot2-distributed-id-core-leafid</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4. MyApplicationPreparedEvent.java文件
在项目启动时,把本项目(IP+端口)注册到ZK,获得应用唯一ID身份标识值
package com.md.demo.begin;
import java.net.InetAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import com.md.core.leafid.MdIdsGen;
/**
* 监听器:项目初始化时调用类
*
* ApplicationPreparedEvent:spring boot上下文context创建完成,但此时spring中的bean是没有完全加载完成的。
*
* @author Minbo
*/
public class MyApplicationPreparedEvent implements ApplicationListener<ApplicationPreparedEvent> {
private Logger logger = LoggerFactory.getLogger(MyApplicationPreparedEvent.class);
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
try {
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
logger.info("项目启动前,执行操作:");
// 1. 初始化分布式id生成器(利用本机IP和端口生成)
logger.info("1. 初始化分布式id生成器(利用本机IP和端口生成):");
String ip = InetAddress.getLocalHost().getHostAddress();
String port = environment.getProperty("server.port");
String zkAddress = environment.getProperty("leaf.zk.list");
MdIdsGen.init(zkAddress, ip, port);
} catch (Exception e) {
logger.error("初始化分布式id生成器,异常:" + e.getMessage(), e);
System.exit(0);
}
}
}
这样集成后,就可以在代码中,直接通过代码:MdIdsGen.getId(),来获得ID值了(Long型)
5. 获得ID案例:代码演示
InitRest.java代码
package com.md.demo.rest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.md.core.leafid.MdIdsGen;
import com.md.demo.util.JsonResult;
import com.md.demo.util.ResultCode;
/**
* @author Minbo
*/
@RestController
public class InitRest {
protected static Logger logger = LoggerFactory.getLogger(InitRest.class);
/**
* http://localhost:9090/hello
*
* @return
*/
@GetMapping("/hello")
public String hello() {
return "Hello greetings from spring-boot2-distributed-id";
}
/**
* http://localhost:9090/getNewId
*
* @return
*/
@GetMapping("/getNewId")
public JsonResult getNewId() {
Long id = MdIdsGen.getId();
return new JsonResult(ResultCode.SUCCESS, id);
}
}
操作结果:
http://localhost:9090/getNewId
完整源码下载
附加资料
下一章教程
SpringBoot从入门到精通教程(十四)- Druid连接池集成
该系列教程
至此,全部介绍就结束了
------------------------------------------------------
------------------------------------------------------
关于我(个人域名)
期望和大家一起学习,一起成长,共勉,O(∩_∩)O谢谢
欢迎交流问题,可加个人QQ 469580884,
或者,加我的群号 751925591,一起探讨交流问题
不讲虚的,只做实干家
Talk is cheap,show me the code