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

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

相关文章

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl