搞定全局ID生成器:SpringBoot2.x 集成百度 uidgenerator

沙海 2021年4月15日01:13:22杂谈 Java评论33字数 5697阅读18分59秒阅读模式
摘要

搞定全局ID生成器:SpringBoot2.x 集成百度 uidgenerator GitHub笔记

搞定全局ID生成器:SpringBoot2.x 集成百度 uidgenerator

GitHub笔记 文章源自JAVA秀-https://www.javaxiu.com/12267.html

因为升级 使用springboot2.x java 11 的关系,根据官方文档和网上其他作者配置的怎么也配置不成功,最后自己一步一步升级引入依赖,修改增加接口注入来源,最后成功。文章源自JAVA秀-https://www.javaxiu.com/12267.html

升级成功后的源码地址文章源自JAVA秀-https://www.javaxiu.com/12267.html

https://github.com/foxiswho/java-spring-boot-uid-generator-baidu文章源自JAVA秀-https://www.javaxiu.com/12267.html

部分升级说明

这里的升级,是升级 官方 代码依赖文章源自JAVA秀-https://www.javaxiu.com/12267.html

官方代码地址:https://github.com/baidu/uid-generator文章源自JAVA秀-https://www.javaxiu.com/12267.html

  • 升级spring boot 版本:2.0.7.RELEASE文章源自JAVA秀-https://www.javaxiu.com/12267.html

  • 升级 mybatis,mybatis-spring 版本文章源自JAVA秀-https://www.javaxiu.com/12267.html

  • 升级 mysql-connector-java 版本:8.0.12文章源自JAVA秀-https://www.javaxiu.com/12267.html

  • 升级 junit 版本文章源自JAVA秀-https://www.javaxiu.com/12267.html

创建数据库存

导入官网数据库SQL文章源自JAVA秀-https://www.javaxiu.com/12267.html

https://github.com/baidu/uid-generator/blob/master/src/main/scripts/WORKER_NODE.sql文章源自JAVA秀-https://www.javaxiu.com/12267.html

也就是一张表文章源自JAVA秀-https://www.javaxiu.com/12267.html

我这里是在demo库中,创建了这张表文章源自JAVA秀-https://www.javaxiu.com/12267.html

DROP TABLE IF EXISTS WORKER_NODE;CREATE TABLE WORKER_NODE(ID BIGINT NOT NULL AUTO_INCREMENT COMMENT 'auto increment id',HOST_NAME VARCHAR(64) NOT NULL COMMENT 'host name',PORT VARCHAR(64) NOT NULL COMMENT 'port',TYPE INT NOT NULL COMMENT 'node type: ACTUAL or CONTAINER',LAUNCH_DATE DATE NOT NULL COMMENT 'launch date',MODIFIED TIMESTAMP NOT NULL COMMENT 'modified time',CREATED TIMESTAMP NOT NULL COMMENT 'created time',PRIMARY KEY(ID)) COMMENT='DB WorkerID Assigner for UID Generator',ENGINE = INNODB;

如果报错,基本上是时间问题,因为mysql 低版本控制比较严格,解决方法有多种方式文章源自JAVA秀-https://www.javaxiu.com/12267.html

方式一:文章源自JAVA秀-https://www.javaxiu.com/12267.html

直接把TIMESTAMP改成DATETIME 即可文章源自JAVA秀-https://www.javaxiu.com/12267.html

方式二:文章源自JAVA秀-https://www.javaxiu.com/12267.html

执行SQL 语句前先执行:文章源自JAVA秀-https://www.javaxiu.com/12267.html

set sql_mode="NO_ENGINE_SUBSTITUTION";

mysql 配置信息更改

因为升级到8.x ,配置文件部分也要跟着修改文章源自JAVA秀-https://www.javaxiu.com/12267.html

uid-generator 下,测试文件夹下的资源包uid/mysql.properties文章源自JAVA秀-https://www.javaxiu.com/12267.html

以下修改为文章源自JAVA秀-https://www.javaxiu.com/12267.html

mysql.driver=com.mysql.cj.jdbc.Driver

修改完成后,配置好数据库相关参数,这样单元测试即可执行成功文章源自JAVA秀-https://www.javaxiu.com/12267.html

案例

计划将全局生成唯一ID作为一个服务提供者,供其他微服务使用调用文章源自JAVA秀-https://www.javaxiu.com/12267.html

这里创建了一个项目,项目中包含两个子项目一个是uid-generator官方本身,当然你也可以不需要放到本项目中,直接使用官方的自行打包即可,一个是uid-provider 服务提供者文章源自JAVA秀-https://www.javaxiu.com/12267.html

以下说明的主要是服务提供者文章源自JAVA秀-https://www.javaxiu.com/12267.html

创建 子项目 uid-provider

POM配置文件如下文章源自JAVA秀-https://www.javaxiu.com/12267.html

<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">    <parent>        <artifactId>java-spring-boot-uid-generator-baiduartifactId>        <groupId>com.foxwho.demogroupId>        <version>1.0-SNAPSHOTversion>    parent>    <modelVersion>4.0.0modelVersion>    <artifactId>uid-providerartifactId>    <dependencies>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-webartifactId>        dependency>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-testartifactId>            <scope>testscope>        dependency>        <dependency>            <groupId>org.mybatis.spring.bootgroupId>            <artifactId>mybatis-spring-boot-starterartifactId>            <version>1.3.2version>        dependency>                <dependency>            <groupId>mysqlgroupId>            <artifactId>mysql-connector-javaartifactId>            <scope>runtimescope>            <version>8.0.12version>        dependency>                <dependency>            <groupId>com.alibabagroupId>            <artifactId>druid-spring-boot-starterartifactId>            <version>1.1.16version>        dependency>        <dependency>            <groupId>org.projectlombokgroupId>            <artifactId>lombokartifactId>            <version>${lombok.version}version>            <optional>trueoptional>        dependency>        <dependency>            <groupId>com.foxwho.demogroupId>            <artifactId>uid-generatorartifactId>            <version>1.0-SNAPSHOTversion>        dependency>    dependencies>project>

复制 mapper

先在uid-provider项目资源包路径下创建mapper文件夹,然后到官方uid-generator资源包路径下META-INF/mybatis/mapper/WORKER_NODE.xml 复制WORKER_NODE.xml文件,粘贴到该文件夹mapper内文章源自JAVA秀-https://www.javaxiu.com/12267.html

cache id 配置文件

先在uid-provider项目资源包路径下创建uid文件夹,然后到官方uid-generator 测试 [注意:这里是测试资源包] 资源包路径下uid/cached-uid-spring.xml 复制cached-uid-spring.xml文件,粘贴到该文件夹uid内文章源自JAVA秀-https://www.javaxiu.com/12267.html

最后根据需要 配置参数,可以看官方说明文章源自JAVA秀-https://www.javaxiu.com/12267.html

创建 spring boot 启动入口

主要就是加上注解@MapperScan("com.baidu.fsg.uid")让mybatis能扫描到Mapper类的包的路径文章源自JAVA秀-https://www.javaxiu.com/12267.html

package com.foxwho.demo;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;@SpringBootApplication@MapperScan("com.baidu.fsg.uid")public class ConsumerApplication {    public static void main(String[] args) {        new SpringApplicationBuilder(ConsumerApplication.class).run(args);    }}

创建配置

package com.foxwho.demo.config;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;@Configuration@ImportResource(locations = { "classpath:uid/cached-uid-spring.xml" })public class UidConfig {}

创建服务接口文章源自JAVA秀-https://www.javaxiu.com/12267.html

package com.foxwho.demo.service;import com.baidu.fsg.uid.UidGenerator;import org.springframework.stereotype.Service;import javax.annotation.Resource;@Servicepublic class UidGenService {    @Resource(name = "cachedUidGenerator")    private UidGenerator uidGenerator;    public long getUid() {        return uidGenerator.getUID();    }}

主要说明一下@Resource(name = "cachedUidGenerator") 以往错误都是少了这里,没有标明注入来源文章源自JAVA秀-https://www.javaxiu.com/12267.html

控制器

package com.foxwho.demo.controller;import com.foxwho.demo.service.UidGenService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UidController {    @Autowired    private UidGenService uidGenService;    @GetMapping("/uidGenerator")    public String UidGenerator() {        return String.valueOf(uidGenService.getUid());    }    @GetMapping("/")    public String index() {        return "index";    }}

项目配置文件

server.port=8080spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=falsespring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.cj.jdbc.Drivermybatis.mapper-locations=classpath:mapper/*.xmlmybatis.configuration.map-underscore-to-camel-case=true

启动项目

从启动入口,启动文章源自JAVA秀-https://www.javaxiu.com/12267.html

访问浏览器文章源自JAVA秀-https://www.javaxiu.com/12267.html

http://localhost:8080/uidGenerator文章源自JAVA秀-https://www.javaxiu.com/12267.html

页面输出文章源自JAVA秀-https://www.javaxiu.com/12267.html

13128615512260612文章源自JAVA秀-https://www.javaxiu.com/12267.html

文章源自JAVA秀-https://www.javaxiu.com/12267.html

作者:风.foxwho文章源自JAVA秀-https://www.javaxiu.com/12267.html

https://urlify.cn/Bzy222文章源自JAVA秀-https://www.javaxiu.com/12267.html

文章源自JAVA秀-https://www.javaxiu.com/12267.html
继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定