大熊猫分布式组件开发系列教程(二)

2023-11-01 06:50

本文主要是介绍大熊猫分布式组件开发系列教程(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

上节我们看到base基础库的base-common的一些基础注解,这些注解后续会有应用。接下来我们看一下base集成的一些内容。

我们都知道查询的时候用的查询条件和排序比较多,因此我定义了两个类来实现根据传入的参数就可实现sql或者hql的拼接。

SortParam实体,用来排序条件的拼装

package com.panda.base.jpa.dao;
public class SortParam {private String key;private Order order = Order.ASC;public String getKey() {return key;}public void setKey(String key) {this.key = key;}public Order getOrder() {return order;}public void setOrder(Order order) {this.order = order;}public SortParam() {}public SortParam(String key, Order order) {this.key = key;this.order = order;}public enum Order {ASC, DESC;}
}

QueryParam用来查询条件的sql拼装

 

package com.panda.base.jpa.dao;
public class QueryParam {private String key;private Object value;private Logic logic = Logic.Contain;/*** 括号内部的连接方式*/private LinkMode innerLinkMode = LinkMode.And;public QueryParam(String key, Object value, Logic logic) {this.key = key;this.value = value;this.logic = logic;}public QueryParam(String key, Object value, Logic logic, LinkMode innerLinkMode) {this.key = key;this.value = value;this.logic = logic;this.innerLinkMode = innerLinkMode;}public QueryParam(String key, Object value) {this.key = key;this.value = value;this.logic = Logic.Equal;}public QueryParam() {}public LinkMode getInnerLinkMode() {return innerLinkMode;}public void setInnerLinkMode(LinkMode innerLinkMode) {this.innerLinkMode = innerLinkMode;}public String getKey() {return key;}public void setKey(String key) {this.key = key;}public Object getValue() {return value;}public void setValue(Object value) {this.value = value;}public Logic getLogic() {return logic;}public void setLogic(Logic logic) {this.logic = logic;}public enum Logic {Equal,NotEqual,Contain,StartWith,EndWith,In,NotIn,GreaterThan,GreaterThanAndEqual,LessThanAndEqual,LessThan,IsNull,IsNotNull}public enum LinkMode {And, Or;}
}

再来看看base-util下封装了哪些东西

工具包中含有一些加解密的工具类,另外还加了一个缓存实体和缓存组,缓存组的概念我这里解释一下。

/*** 概念:* 组缓存,在传统redis、memcached 这一类的缓存容器中,都是key-value类型的* 所谓的组缓存,就是在key-value的外层加一个标识* 用list来做比较,在redis中,list的结构是这样的,并且list的某一项不能设置存活时间:*  abc*      1*      2*      3** 在GroupCache中结构是这样的   :* abc*  a 1 10*  b 2 5*  c 3 1* 第一行:a代表key,1代表value,10代表存活10秒,存活时间以秒为单位*/

CacheEntity

package com.panda.base.utils.cache;
import java.io.Serializable;
/*** 缓存实体*/
public class CacheEntity<T,V> implements Serializable {private static final long serialVersionUID = 2082223810638865724L;private T key; // keyprivate V value;// 值private Long timestamp;// 缓存的时候存的时间戳,用来计算该元素是否过期private int expire = Integer.MAX_VALUE; // 默认长期有效private Group group;// 容器public CacheEntity(T key, V value, Long timestamp, int expire, Group group) {this.key = key;this.value = value;this.timestamp = timestamp;this.expire = expire;this.group = group;}public void setTimestamp(Long timestamp) {this.timestamp = timestamp;}public Long getTimestamp() {return timestamp;}public T getKey() {return key;}public void setKey(T key) {this.key = key;}public V getValue() {return value;}public void setValue(V value) {this.value = value;}public int getExpire() {return expire;}public void setExpire(int expire) {this.expire = expire;}/*** 获取剩余时间** @return*/public int ttl() {return this.expire - getTime();}/*** 获取当前时间和元素的相差时间(存活时间)** @return*/private int getTime() {Long current = System.currentTimeMillis();Long value = current - this.timestamp;return (int) (value / 1000) + 1;}/*** 是否有效** @return*/public boolean isExpire() {if (getTime() > this.expire) {// 失效了就移除group.delete(key);return false;}return true;}
}

Group

package com.panda.base.utils.cache;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
/*** 组操作,内部数据有序队列*/
public class Group<T, V> {private ArrayBlockingQueue<CacheEntity<T, V>> queue;// 缓存队列private Integer capacity;public Group(int capacity) {queue = new ArrayBlockingQueue<>(capacity);this.capacity = capacity;}/*** 尾部进。 如果容量已满,则移除距过期时间最短的** @param object* @param second*/public void push(T key, V object, int second) {// 放入队列if (!queue.offer(new CacheEntity(key, object, System.currentTimeMillis(), second, this))) {// 做一次清除getCacheEntities();if (!queue.offer(new CacheEntity(key, object, System.currentTimeMillis(), second, this))) {int ttf = Integer.MAX_VALUE;CacheEntity maxVtfCacheEntity = null;Iterator<CacheEntity<T, V>> iterator = queue.iterator();while (iterator.hasNext()) {CacheEntity cacheEntity = iterator.next();if (cacheEntity.ttl() < ttf) {ttf = cacheEntity.ttl();maxVtfCacheEntity = cacheEntity;}}if (maxVtfCacheEntity != null && this.queue.remove(maxVtfCacheEntity))queue.offer(new CacheEntity(key, object, System.currentTimeMillis(), second, this));}}}/*** 尾部进. 默认失效时间10分钟** @param object*/public void push(T key, V object) {push(key, object, 600);}/*** 返回并移除头部出** @return*/public V poll() {CacheEntity<T, V> entity = queue.poll();// 如果有效期超过,返回nullif (!entity.isExpire()) {return null;}return entity.getValue();}/*** 返回头部元素并放到末尾** @return*/public V rPoll() {CacheEntity<T, V> entity = queue.poll();// 如果有效期超过,返回nullif (!entity.isExpire()) {return null;}V object = entity.getValue();queue.offer(entity);return object;}/*** 通过key寻找有效的缓存实体** @param key* @return*/private CacheEntity find(T key) {synchronized (queue) {Iterator<CacheEntity<T, V>> iterator = queue.iterator();while (iterator.hasNext()) {CacheEntity entity = iterator.next();if (key.equals(entity.getKey())) {return entity;}}return null;}}/*** 删除key** @param key*/public void delete(T key) {synchronized (queue) {CacheEntity<T, V> entity = find(key);if (entity != null) {queue.remove(entity);}}}/*** 根据key获取** @param key* @return*/public V getValue(T key) {CacheEntity<T, V> entity = find(key);if (entity != null && entity.isExpire()) {return entity.getValue();}return null;}/*** 获取有效的缓存实体* 移除所有过期的缓存实体。** @return*/private List<CacheEntity<T, V>> getCacheEntities() {List<CacheEntity<T, V>> keys = new ArrayList<CacheEntity<T, V>>();Iterator<CacheEntity<T, V>> iterator = queue.iterator();while (iterator.hasNext()) {CacheEntity cacheEntity = iterator.next();if (cacheEntity.isExpire()) {keys.add(cacheEntity);}}return keys;}/*** 获取key列表** @return*/public List<T> getKeys() {List<T> keys = new ArrayList<T>();List<CacheEntity<T, V>> caches = getCacheEntities();for (CacheEntity<T, V> cacheEntity : caches) {keys.add(cacheEntity.getKey());}return keys;}/*** 获取值列表** @return*/public List<V> getValues() {List<V> values = new ArrayList<V>();List<CacheEntity<T, V>> caches = getCacheEntities();for (CacheEntity<T, V> cacheEntity : caches) {values.add(cacheEntity.getValue());}return values;}/*** 查看元素存活时间,-1 失效,0 长期有效** @param key* @return*/public int ttl(T key) {CacheEntity entity = find(key);if (entity != null) {return entity.ttl();}return -1;}/*** 返回头部的元素** @return*/public V peek() {CacheEntity<T, V> entity = queue.peek();if (entity != null) {return entity.getValue();}return null;}/*** 设置元素存活时间** @param key* @param second*/public void expire(T key, int second) {CacheEntity entity = find(key);if (entity != null) {entity.setTimestamp(System.currentTimeMillis());entity.setExpire(second);}}/*** 查看key是否存在** @param key* @return*/public boolean exist(T key) {CacheEntity<T, V> entity = find(key);if (entity == null) return false;return entity.isExpire();}/*** 查看组是否为空** @return*/public boolean isEmpty() {return queue.isEmpty();}/*** 获取存活元素的大小** @return*/public int size() {return getCacheEntities().size();}/*** 获取容量** @return*/public Integer getCapacity() {return capacity;}
}

GroupCacheFactory简单的内存缓存实现,现group概念,一个group里面是个有序的集合, 集合支持key-value expire弥补redis list的不足。

 

package com.panda.base.utils.cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedHashMap;
import java.util.Map;
public class GroupCacheFactory {private final static Logger log = LoggerFactory.getLogger(GroupCacheFactory.class);// 数据容器private Map<String, Object> container;public GroupCacheFactory() {container = new LinkedHashMap<String, Object>();}/*** 如果组存在就返回,不存在就创建,保证不为null** @param key* @return*/public Group group(String key, int capacity) {Group group = null;Object entry = container.get(key);if (entry != null) {group = (Group) entry;} else {group = new Group(capacity);container.put(key, group);}return group;}/*** 如果组存在就返回,不存在就创建,默认容量1000** @param key* @return*/public Group group(String key) {return this.group(key, 100000);}public void clearExpiredCache() {container.keySet().forEach(key -> {log.debug("Cache:   key=" + key + "  size=" + ((Group) container.get(key)).size());});}
}

接着就是jwt权限认证的一个实体类和获取token加解密的生成方法。

1.JwtSubject

package com.panda.base.utils.jwt;
import com.alibaba.fastjson.JSON;
import java.time.LocalDateTime;
public class JwtSubject {private Long usId;private String uuId;private String userRole;private String clientId;private LocalDateTime loginTime;private JwtSubject() {}public JwtSubject(Long usId, String userRole, String clientId) {this(usId, null, userRole, clientId);}public JwtSubject(String uuId, String userRole, String clientId) {this(null, uuId, userRole, clientId);}public JwtSubject(Long usId, String uuId, String userRole, String clientId) {this.usId = usId;this.uuId = uuId;this.userRole = userRole;this.clientId = clientId;this.loginTime = LocalDateTime.now();}public String getUuId() {return uuId;}public void setUuId(String uuId) {this.uuId = uuId;}public String getUserRole() {return userRole;}public void setUserRole(String userRole) {this.userRole = userRole;}public String getClientId() {return clientId;}public void setClientId(String clientId) {this.clientId = clientId;}public LocalDateTime getLoginTime() {return loginTime;}public void setLoginTime(LocalDateTime loginTime) {this.loginTime = loginTime;}public static JwtSubject toSubject(String subStr) {return JSON.parseObject(subStr, JwtSubject.class);}@Overridepublic String toString() {return JSON.toJSONString(this);}public Long getUsId() {return usId;}public void setUsId(Long usId) {this.usId = usId;}
}

根据jwt实体生成token以及对token反解密,JwtUtils

 

package com.panda.base.utils.jwt;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.panda.base.utils.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
public class JwtUtils {private final static Logger log = LoggerFactory.getLogger(JwtUtils.class);/*** @param userId   用户自增ID,没有可传null* @param uuId     用户uuId,没有可传null* @param userRole 用户身份分类,例如: ADMIN,USER* @param clientId 客户端标识。* @param expired  过期时间,毫秒。* @return*/public static String token(Long userId, String uuId, String userRole, String clientId, long expired) {JwtSubject jwtSubject = new JwtSubject(userId, uuId, userRole, clientId);return jwtToken(jwtSubject, expired);}public static String token(Long userId, String uuId, String userRole, String clientId) {return token(userId, uuId, userRole, clientId, TimeUtils.YEAR * 10);}public final static String SECRET = "fadffdaldfkjafdja_k32423535";public static JwtSubject verify(String jwtToken) {try {JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).withIssuer("auth0").build();JWT jwt = (JWT) verifier.verify(jwtToken);return JwtSubject.toSubject(jwt.getSubject());} catch (InvalidClaimException e) {log.error("InvalidClaimException e"+e.getMessage());} catch (JWTVerificationException e) {log.error("JWTVerificationException e"+e.getMessage());} catch (UnsupportedEncodingException e) {log.error("UnsupportedEncodingException e"+e.getMessage());}return null;}public static JwtSubject decode(String token) {JWT jwt = JWT.decode(token);return JwtSubject.toSubject(jwt.getSubject());}public static String jwtToken(JwtSubject subject, long expireTime) {try {/* 获取一分钟前的时间,作为token的产生时间,防止服务器时间不一致,导致token校验失败 */Calendar beforeTime = Calendar.getInstance();beforeTime.add(Calendar.MINUTE, -1);// 1分钟之前的时间Date beforeD = beforeTime.getTime();/* 计算token过期 */Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(calendar.getTimeInMillis() + expireTime);String token = JWT.create().withSubject(subject.toString()).withIssuedAt(beforeD).withExpiresAt(calendar.getTime()).withIssuer("auth0").sign(Algorithm.HMAC256(SECRET));return token;} catch (JWTCreationException exception) {log.error("生成jwtToken异常", exception);} catch (UnsupportedEncodingException e) {log.error("生成jwtToken异常", e);}return null;}public static void main(String[] args) {System.out.println("long max is " + Long.MAX_VALUE);System.out.println("int max is " + Integer.MAX_VALUE);System.out.println("short max is " + Short.MAX_VALUE);JwtSubject subject = new JwtSubject(32434L, "234242342535435345345345253243", "ANONYMITY", "default");String token = jwtToken(subject, TimeUtils.YEAR * 10);System.out.println(token);System.out.println(verify(token));}
}

运行main方法我们看运行结果

基于这些我们在后边的权限框架中就可以专注业务实现了。

本节内容就到这,下一篇我会讲讲每个业务层的基础封装以及auth权限框架编写sso单点登录系统。源码可以关注“蛋皮皮”公众号回复“大熊猫分布式组件”进行获取。

这篇关于大熊猫分布式组件开发系列教程(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/321233

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

Java Scanner类解析与实战教程

《JavaScanner类解析与实战教程》JavaScanner类(java.util包)是文本输入解析工具,支持基本类型和字符串读取,基于Readable接口与正则分隔符实现,适用于控制台、文件输... 目录一、核心设计与工作原理1.底层依赖2.解析机制A.核心逻辑基于分隔符(delimiter)和模式匹

Redis实现分布式锁全过程

《Redis实现分布式锁全过程》文章介绍Redis实现分布式锁的方法,包括使用SETNX和EXPIRE命令确保互斥性与防死锁,Redisson客户端提供的便捷接口,以及Redlock算法通过多节点共识... 目录Redis实现分布式锁1. 分布式锁的基本原理2. 使用 Redis 实现分布式锁2.1 获取锁

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10