首页 > 其他分享 >自定义starter

自定义starter

时间:2023-04-23 16:13:52浏览次数:33  
标签:自定义 redis springframework id org import annotation starter

将redis生成分布式唯一id的功能封装成starter供其他模块使用

1 编写业务类

package com.yangkun.redis;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;


public class RedisCache {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //redis生成唯一id
    public Long nextId(String keyWord){
        Long BEGIN_TIMESTAMP = LocalDateTime.of(2022,1,1,0,0,0).toEpochSecond(ZoneOffset.UTC);
        //时间戳
        LocalDateTime now = LocalDateTime.now();
        Long newSecond = now.toEpochSecond(ZoneOffset.UTC);
        long timeStamp = newSecond-BEGIN_TIMESTAMP;
        //生成序列号
        //获取当前日期,精确到天
        String date = now.format(DateTimeFormatter.ofPattern("yyy:yy:dd"));
        //自增长,redis根据key生成一个唯一id,keyWord根据业务传不同的值,比如订单模块的id就传 order,用户模块的id就传user,date是每天不同的,
        //因为long是64位,返回值long的低32位是redis自增位,如果每天都用同一个key的话,超过2的32次方后会生成重复的id,date每天变化,只要当天不超过2的32次方个id就不会重复
        long count = stringRedisTemplate.opsForValue().increment("icr:"+keyWord+":"+date);
        //返回格式: 最高位符号位(0),31位时间戳,32位redis自增位
        return timeStamp << 32 |count;
    }
}

2 编写配置类

package com.yangkun.redis;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import javax.annotation.Resource;
 
@Configuration //表示该类为配置类
// @EnableConfigurationProperties({xxx.class}) 有的类需要从配置文件获取值,可以开启这个注解,比如有的类用@ConfigurationProperties/@value从配置文件取值
public class AutoConfig {  @Bean public RedisCache getRedisCache(){ return new RedisCache(); } }

3 在resources\META-INF\spring.factories里配置 AutoConfig 配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.yangkun.redis.AutoConfig

4 执行maven-install命令在maven仓库里生成jar包

5 其他模块相关操作

 5.1 pom文件里加入依赖,再使用@Autowired/@Resource注入RedisCache类

5.2 注意: 需要在application.yml里配置redis,如果自定义starter里有需要从配置文件里取值的,需要在配置文件里定义好

标签:自定义,redis,springframework,id,org,import,annotation,starter
From: https://www.cnblogs.com/1--2/p/17346811.html

相关文章

  • 自定义注解获取当前登录信息
    登录注解/***@authorzhourui*@date2023/4/189:39*/importjava.lang.annotation.*;/***@authorzhourui*@date2023/4/1714:42*/@Documented@Target(ElementType.METHOD)@Retention(value=RetentionPolicy.RUNTIME)public@interfaceReLogin{}......
  • Vue 3.0自定义指令
    Vue2和Vue3在自定义指令上有一些差异,并不完全一致,下面的介绍主要是针对Vue3的介绍。1.作用域自定义指令有两种作用域,一种是局部的自定义指令,还有一种是全局的自定义指令。局部的自定义指令就只能在当前.vue文件中使用,全局的则可以在main.js里挂载之后,在所有的.vue文件......
  • shell自定义函数
    函数调用通常将函数看成是脚本中的一段代码,在使用函数前必须先定义该函数,使用时利用函数名直接调用。例:编写脚本func_script,内容如下。#!/bin/bashREPEAT=3fa(){echo"Nowfafunctionisstarting..."echo}fb(){i=0echo"Andnowthefbbebins."sleep......
  • 从数据库查询权限信息、自定义失败处理
    从数据库查询权限信息我们只需要根据用户id去查询到其所对应的权限信息即可。所以我们可以先定义个mapper,其中提供一个方法可以根据userid查询权限信息。MenuMapper持久层接口publicinterfaceMenuMapperextendsBaseMapper<Menu>{List<String>selectPermsByUser......
  • nginx自定义负载均衡及根据cpu运行自定义负载均衡
    1.nginx如何自定义负载均衡在Nginx中,可以通过配置文件自定义负载均衡策略。具体步骤如下:首先,在Nginx配置文件中定义一个upstream模块,并设置负载均衡策略和后端服务器列表,例如:upstreammyapp{serverbackend1.example.comweight=3;serverbackend2.example.com;se......
  • node-red 在功能模块下自定义节点
    在目录下node-red\packages\node_modules\@node-red\nodes\core\function下创建compare.js和compare.html demo.js demo.html确保 data-template-name与RED.nodes.registerType的名称要一致 然后npmrunstart就可以看到 注:https://blog.csdn.net/wmjjjj/artic......
  • qt-自定义界面
     https://blog.csdn.net/XCJandLL/article/details/125631053https://blog.csdn.net/qq_20792765/article/details/109840347https://blog.csdn.net/qq_20792765/article/details/109768594https://blog.csdn.net/qq_42964109/article/details/126884439https://blog.csd......
  • C++调用自定义源文件函数
    C++调用自定义源文件函数的步骤如下:在需要调用函数的源文件中包含自定义源文件的头文件。例如,如果需要调用名为myfunc.cpp的自定义源文件中的函数,则需要在调用该函数的源文件中包含myfunc.h头文件。编译自定义源文件。如果使用命令行编译,可以使用以下命令编译自定义源文件并生成......
  • 2、Pipeline语法及使用自定义工具的Maven工程
    Pipeline语法声明式pipeline的结构pipeline的定义有一个明确的、必须遵循的结构,它由一些directive(指令)和section(配置段)组成,每一个section又可包含其它的section、directive和step(执行步骤),以及一些condition(执行条件)的定义;◼Section:用于将那些在某个时间点需要一同运行的条目(i......
  • ruoyi框架自定义导出的Excel文件名
      https://www.cnblogs.com/an-drew/p/ruoyi-custom-excel.html#!comments这是ruoyi界面的导出按钮,点击导出后 默认导出的Excel文件名是: Unix时间戳+Controller传入的sheetName+.xlsx(对应下图中的文件名:1602402277993+info+.xlsx)  但是我想要的自定义E......