Fory序列化与反序列化
你好呀,我的老朋友!我是老寇,跟我一起学习Fory序列化与反序列化
介绍
Fory官方地址
Apache Fory是一个速度极快的多语言序列化框架,由JIT(即时编译)和零拷贝提供支持,可提供高达 170 倍的性能和极致的易用性
Apache Fory 之前名为 Apache Fury。对于 0.11 之前的版本,请在包名称、导入和依赖项中使用“fury”而不是“fory”
注意:这篇文章主要用来记录之前写的代码,便于后面翻阅查看!!!
实践
依赖
<dependency><groupId>org.apache.fory</groupId><artifactId>fory-core</artifactId><version>0.12.0</version>
</dependency>
使用
public final class ForyFactory {public static final ForyFactory INSTANCE = new ForyFactory();private final ThreadSafeFory fory = new ForyBuilder().withLanguage(Language.JAVA)// enable reference tracking for shared/circular reference.// Disable it will have better performance if no duplicate reference..withRefTracking(false)// compress int for smaller size// .withIntCompressed(true)// compress long for smaller size// .withLongCompressed(true).withCompatibleMode(CompatibleMode.SCHEMA_CONSISTENT)// enable type forward/backward compatibility// disable it for small size and better performance.// .withCompatibleMode(CompatibleMode.COMPATIBLE)// enable async multi-threaded compilation..withAsyncCompilation(true).requireClassRegistration(true).buildThreadSafeFory();public <T> void register(Class<T> clazz) {fory.register(clazz);}public byte[] serialize(Object object) {if (object == null) {return new byte[0];}if (object instanceof String str) {return str.getBytes(StandardCharsets.UTF_8);}return fory.serialize(object);}public Object deserialize(byte[] bytes) {if (bytes == null) {return null;}return fory.deserialize(bytes);}}
Mybatis-Plus本地缓存SQL
我们使用fory进行序列化,替换掉原有的jdk序列化
public class ForySerialCaffeineJsqlParseCache extends AbstractCaffeineJsqlParseCache {static {ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.Alias.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.Alias.AliasColumn.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.AllValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.AnalyticExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.AnyComparisonExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.ArrayConstructor.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.ArrayExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.CaseExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.CastExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.CollateExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.ConnectByRootOperator.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.DateTimeLiteralExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.DateValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.DoubleValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.ExtractExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.FilterOverImpl.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.Function.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.HexValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.IntervalExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JdbcNamedParameter.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JdbcParameter.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonAggregateFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonFunctionExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonKeyValuePair.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.KeepExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.LongValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.MySQLGroupConcat.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.MySQLIndexHint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.NextValExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.NotExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.NullValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.NumericBind.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OracleHierarchicalExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OracleHint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OracleNamedFunctionParameter.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OrderByClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OverlapsCondition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.PartitionByClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.RangeExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.RowConstructor.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.RowGetExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.SQLServerHints.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.SignedExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.StringValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TimeKeyExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TimeValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TimestampValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TimezoneExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TranscodingFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TrimFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.UserVariable.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.VariableAssignment.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WhenClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowDefinition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowElement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowOffset.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowRange.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.XMLSerializeExpr.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Addition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseLeftShift.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseOr.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseRightShift.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseXor.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Concat.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Division.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.IntegerDivision.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Modulo.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.conditional.AndExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.conditional.OrExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.conditional.XorExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.Between.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ContainedBy.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.Contains.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.DoubleAnd.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.EqualsTo.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ExistsExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ExpressionList.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.FullTextSearch.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.GeometryDistance.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.GreaterThan.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.InExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.IsBooleanExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.IsDistinctExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.IsNullExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.JsonOperator.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.LikeExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.Matches.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.MemberOfExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.MinorThan.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.NamedExpressionList.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.SimilarToExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.TSQLLeftJoin.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.TSQLRightJoin.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.parser.ASTNodeAccessImpl.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.parser.Token.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Column.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Sequence.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Synonym.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Table.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.Block.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.Commit.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.DeclareStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.DeclareStatement.TypeDefExpr.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.DescribeStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ExplainStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ExplainStatement.Option.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.IfElseStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.OutputClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.PurgeStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ReferentialAction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ResetStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.RollbackStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.SavepointStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.SetStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ShowColumnsStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ShowStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.Statements.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.UnsupportedStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.UseStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.Alter.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDataType.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDropDefault.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDropNotNull.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterSession.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterSystemStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.RenameTableStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.sequence.AlterSequence.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.analyze.Analyze.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.comment.Comment.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.function.CreateFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.index.CreateIndex.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.procedure.CreateProcedure.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.schema.CreateSchema.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.sequence.CreateSequence.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.synonym.CreateSynonym.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.CheckConstraint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.ColDataType.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.ColumnDefinition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.CreateTable.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.ExcludeConstraint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.ForeignKeyIndex.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.Index.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.Index.ColumnParams.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.NamedConstraint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.RowMovement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.view.AlterView.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.view.CreateView.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.delete.Delete.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.drop.Drop.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.execute.Execute.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.grant.Grant.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.Insert.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.InsertConflictAction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.InsertConflictTarget.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.merge.Merge.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.merge.MergeDelete.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.merge.MergeInsert.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.merge.MergeUpdate.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.AllColumns.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.AllTableColumns.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Distinct.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ExceptOp.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Fetch.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.First.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ForClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.GroupByElement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.IntersectOp.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Join.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.KSQLJoinWindow.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.KSQLWindow.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.LateralSubSelect.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.LateralView.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Limit.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.MinusOp.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Offset.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.OptimizeFor.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.OrderByElement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ParenthesedFromItem.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ParenthesedSelect.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Pivot.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.PivotXml.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.PlainSelect.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.SelectItem.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.SetOperationList.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.SetOperationList.SetOperationType.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Skip.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.TableFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.TableStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Top.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.UnPivot.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.UnionOp.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Values.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Wait.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.WithIsolation.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.WithItem.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.show.ShowIndexStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.show.ShowTablesStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.truncate.Truncate.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.update.Update.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.update.UpdateSet.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.upsert.Upsert.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.util.cnfexpression.MultiAndExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.util.cnfexpression.MultiOrExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.BinaryExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ComparisonOperator.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.OldOracleJoinBinaryExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.Function.NullHandling.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.CreateFunctionalStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Select.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.SetOperation.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.util.cnfexpression.MultipleExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.InsertModifierPriority.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.OrderByElement.NullOrdering.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ForMode.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.MySqlSqlCacheFlags.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.PlainSelect.BigQuerySelectQualifier.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.update.UpdateModifierPriority.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.LikeExpression.KeyWord.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.delete.DeleteModifierPriority.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Partition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.ConflictActionType.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ForClause.ForOption.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.KSQLWindow.TimeUnit.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.First.Keyword.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowElement.Type.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowOffset.Type.class);}public ForySerialCaffeineJsqlParseCache(Cache<String, byte[]> cache) {super(cache);}@Overridepublic byte[] serialize(Object obj) {return ForyFactory.INSTANCE.serialize(obj);}@Overridepublic Object deserialize(String sql, byte[] bytes) {return ForyFactory.INSTANCE.deserialize(bytes);}}
static {JsqlParserGlobal.setJsqlParseCache(new ForySerialCaffeineJsqlParseCache(Caffeine.newBuilder().maximumSize(4096).expireAfterWrite(10, TimeUnit.MINUTES).build()));
}
Redis序列化
jackson序列化
public final class GlobalJsonJacksonCodec extends JsonJacksonCodec {/*** 实例.*/public static final GlobalJsonJacksonCodec INSTANCE = new GlobalJsonJacksonCodec();private GlobalJsonJacksonCodec() {super(objectMapper());}// @formatter:off/*** 解决查询缓存转换异常的问题.* @return ObjectMapper*/private static ObjectMapper objectMapper() {ObjectMapper objectMapper = new ObjectMapper();DateTimeFormatter dateTimeFormatter = DateUtils.getDateTimeFormatter(DateUtils.YYYY_B_MM_B_DD_HH_R_MM_R_SS);JavaTimeModule javaTimeModule = new JavaTimeModule();// Long类型转String类型javaTimeModule.addSerializer(Long.class, ToStringSerializer.instance);javaTimeModule.addSerializer(Long.TYPE, ToStringSerializer.instance);// LocalDateTimejavaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));// InstantjavaTimeModule.addSerializer(Instant.class, new CustomInstantSerializer(InstantSerializer.INSTANCE, false,false, dateTimeFormatter));javaTimeModule.addDeserializer(Instant.class, new CustomInstantDeserializer(InstantDeserializer.INSTANT, dateTimeFormatter));objectMapper.registerModule(javaTimeModule);// 所有属性访问器(字段、getter和setter),将自动检测所有字段属性objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// 对于所有非final类型,使用LaissezFaire子类型验证器来推断类型objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, NON_FINAL, JsonTypeInfo.As.PROPERTY);// 反序列化时,属性不存在的兼容处理objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);// 自动查找并注册相关模块objectMapper.findAndRegisterModules();return objectMapper;}// @formatter:onpublic static Jackson2JsonRedisSerializer<Object> getJsonRedisSerializer() {// Json序列化配置return new Jackson2JsonRedisSerializer<>(objectMapper(), Object.class);}}
/*** 自定义RedisTemplate.* @param lettuceConnectionFactory 工厂* @return RedisTemplate*/
@Bean("redisTemplate")
@ConditionalOnMissingBean(RedisTemplate.class)
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(lettuceConnectionFactory);Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = getJsonRedisSerializer();// string序列化StringRedisSerializer stringRedisSerializer = getStringRedisSerializer();// keyredisTemplate.setKeySerializer(stringRedisSerializer);// valueredisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash-keyredisTemplate.setHashKeySerializer(stringRedisSerializer);// hash-valueredisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// 初始化redisTemplate.afterPropertiesSet();return redisTemplate;
}@Bean("reactiveRedisTemplate")
@ConditionalOnMissingBean(ReactiveRedisTemplate.class)
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = getJsonRedisSerializer();StringRedisSerializer stringRedisSerializer = getStringRedisSerializer();RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext.<String, Object>newSerializationContext().key(stringRedisSerializer).value(jsonRedisSerializer).hashKey(stringRedisSerializer).hashValue(jsonRedisSerializer).build();return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}
使用fory序列化,替换掉原来jackson
public final class ForyRedisSerializer implements RedisSerializer<Object> {@Overridepublic byte[] serialize(Object obj) throws SerializationException {return ForyFactory.INSTANCE.serialize(obj);}@Overridepublic Object deserialize(byte[] bytes) throws SerializationException {return ForyFactory.INSTANCE.deserialize(bytes);}public static StringRedisSerializer getStringRedisSerializer() {return new StringRedisSerializer(StandardCharsets.UTF_8);}public static ForyRedisSerializer foryRedisSerializer() {// Json序列化配置return new ForyRedisSerializer();}}
@Bean("redisTemplate")
@ConditionalOnMissingBean(RedisTemplate.class)
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(lettuceConnectionFactory);// fory序列化ForyRedisSerializer foryRedisSerializer = foryRedisSerializer();// string序列化StringRedisSerializer stringRedisSerializer = getStringRedisSerializer();// keyredisTemplate.setKeySerializer(stringRedisSerializer);// valueredisTemplate.setValueSerializer(foryRedisSerializer);// hash-keyredisTemplate.setHashKeySerializer(stringRedisSerializer);// hash-valueredisTemplate.setHashValueSerializer(foryRedisSerializer);// 初始化redisTemplate.afterPropertiesSet();return redisTemplate;
}@Bean("reactiveRedisTemplate")
@ConditionalOnMissingBean(ReactiveRedisTemplate.class)
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {// fory序列化ForyRedisSerializer foryRedisSerializer = foryRedisSerializer();// string序列化StringRedisSerializer stringRedisSerializer = getStringRedisSerializer();RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext.<String, Object>newSerializationContext().key(stringRedisSerializer).value(foryRedisSerializer).hashKey(stringRedisSerializer).hashValue(foryRedisSerializer).build();return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}
Kafka序列化
Kafka默认使用StringSerializer,因此,我们替换成fory
public class ForyKafkaDeserializer implements Deserializer<Object> {@Overridepublic Object deserialize(String s, byte[] bytes) {return ForyFactory.INSTANCE.deserialize(bytes);}}
public class ForyKafkaSerializer implements Serializer<Object> {@Overridepublic byte[] serialize(String s, Object o) {return ForyFactory.INSTANCE.serialize(o);}}
spring:kafka:consumer:# 键的序列化方式key-deserializer: org.apache.kafka.common.serialization.StringDeserializer# 值的序列化方式value-deserializer: xxx.ForyKafkaDeserializerproducer:# 键的序列化方式key-serializer: org.apache.kafka.common.serialization.StringSerializer# 值的序列化方式value-serializer: xxx.ForyKafkaSerializer
Pulsar序列化
public final class ForySchema extends AbstractSchema<Object> {public static final ForySchema INSTANCE = new ForySchema();@Overridepublic byte[] encode(Object message) {return ForyFactory.INSTANCE.serialize(message);}@Overridepublic Object decode(byte[] bytes) {return ForyFactory.INSTANCE.deserialize(bytes);}@Overridepublic SchemaInfo getSchemaInfo() {return SchemaInfo.builder().name("Fory").type(SchemaType.BYTES).schema(new byte[0]).build();}@Overridepublic Object decode(ByteBuf byteBuf) {return ForyFactory.INSTANCE.deserialize(byteBuf.array());}}
@Component
@RequiredArgsConstructor
public class PulsarMessageHandler {private final ReactivePulsarTemplate<Object> reactivePulsarTemplate;public Mono<MessageId> handle() {return reactivePulsarTemplate.send("xxx", null, ForySchema.INSTANCE);}}
我是老寇,我们下次再见啦!