chore(project): 添加项目配置文件和忽略规则
- 添加 Babel 配置文件支持 ES6+ 语法转换 - 添加 ESLint 忽略规则和配置文件 - 添加 Git 忽略规则文件 - 添加 Travis CI 配置文件 - 添加 1.4.2 版本变更日志文件 - 添加 Helm 图表辅助模板文件 - 添加 Helm 忽略规则文件
This commit is contained in:
34
serializer/seata-serializer-kryo/pom.xml
Normal file
34
serializer/seata-serializer-kryo/pom.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<groupId>io.seata</groupId>
|
||||
<artifactId>seata-serializer</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>seata-serializer-kryo</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>seata-serializer-kryo ${project.version}</name>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>seata-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>seata-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.esotericsoftware</groupId>
|
||||
<artifactId>kryo</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.javakaffee</groupId>
|
||||
<artifactId>kryo-serializers</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.seata.serializer.kryo;
|
||||
|
||||
import com.esotericsoftware.kryo.Kryo;
|
||||
import com.esotericsoftware.kryo.io.Input;
|
||||
import com.esotericsoftware.kryo.io.Output;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author jsbxyyx
|
||||
*/
|
||||
public class KryoInnerSerializer {
|
||||
|
||||
private final Kryo kryo;
|
||||
|
||||
public KryoInnerSerializer(Kryo kryo) {
|
||||
this.kryo = Objects.requireNonNull(kryo);
|
||||
}
|
||||
|
||||
public Kryo getKryo() {
|
||||
return kryo;
|
||||
}
|
||||
|
||||
public <T> byte[] serialize(T t) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Output output = new Output(baos);
|
||||
kryo.writeClassAndObject(output, t);
|
||||
output.close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public <T> T deserialize(byte[] bytes) {
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
||||
Input input = new Input(bais);
|
||||
input.close();
|
||||
return (T) kryo.readClassAndObject(input);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.seata.serializer.kryo;
|
||||
|
||||
import io.seata.common.loader.LoadLevel;
|
||||
import io.seata.core.serializer.Serializer;
|
||||
import io.seata.core.protocol.AbstractMessage;
|
||||
|
||||
/**
|
||||
* @author jsbxyyx
|
||||
*/
|
||||
@LoadLevel(name = "KRYO")
|
||||
public class KryoSerializer implements Serializer {
|
||||
|
||||
@Override
|
||||
public <T> byte[] serialize(T t) {
|
||||
if (!(t instanceof AbstractMessage)) {
|
||||
throw new IllegalArgumentException("message is illegal");
|
||||
}
|
||||
KryoInnerSerializer kryoSerializer = KryoSerializerFactory.getInstance().get();
|
||||
try {
|
||||
return kryoSerializer.serialize(t);
|
||||
} finally {
|
||||
KryoSerializerFactory.getInstance().returnKryo(kryoSerializer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T deserialize(byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) {
|
||||
throw new IllegalArgumentException("bytes is null");
|
||||
}
|
||||
KryoInnerSerializer kryoSerializer = KryoSerializerFactory.getInstance().get();
|
||||
try {
|
||||
return kryoSerializer.deserialize(bytes);
|
||||
} finally {
|
||||
KryoSerializerFactory.getInstance().returnKryo(kryoSerializer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.seata.serializer.kryo;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.BitSet;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import com.esotericsoftware.kryo.Kryo;
|
||||
import com.esotericsoftware.kryo.pool.KryoFactory;
|
||||
import com.esotericsoftware.kryo.pool.KryoPool;
|
||||
import com.esotericsoftware.kryo.serializers.DefaultSerializers;
|
||||
import de.javakaffee.kryoserializers.ArraysAsListSerializer;
|
||||
import de.javakaffee.kryoserializers.BitSetSerializer;
|
||||
import de.javakaffee.kryoserializers.GregorianCalendarSerializer;
|
||||
import de.javakaffee.kryoserializers.JdkProxySerializer;
|
||||
import de.javakaffee.kryoserializers.RegexSerializer;
|
||||
import de.javakaffee.kryoserializers.URISerializer;
|
||||
import de.javakaffee.kryoserializers.UUIDSerializer;
|
||||
import io.seata.core.serializer.SerializerClassRegistry;
|
||||
|
||||
/**
|
||||
* @author jsbxyyx
|
||||
*/
|
||||
public class KryoSerializerFactory implements KryoFactory {
|
||||
|
||||
private static final KryoSerializerFactory FACTORY = new KryoSerializerFactory();
|
||||
|
||||
private KryoPool pool = new KryoPool.Builder(this).softReferences().build();
|
||||
|
||||
private KryoSerializerFactory() {}
|
||||
|
||||
public static KryoSerializerFactory getInstance() {
|
||||
return FACTORY;
|
||||
}
|
||||
|
||||
public KryoInnerSerializer get() {
|
||||
return new KryoInnerSerializer(pool.borrow());
|
||||
}
|
||||
|
||||
public void returnKryo(KryoInnerSerializer kryoSerializer) {
|
||||
if (kryoSerializer == null) {
|
||||
throw new IllegalArgumentException("kryoSerializer is null");
|
||||
}
|
||||
pool.release(kryoSerializer.getKryo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Kryo create() {
|
||||
Kryo kryo = new Kryo();
|
||||
kryo.setRegistrationRequired(false);
|
||||
|
||||
// register serializer
|
||||
kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
|
||||
kryo.register(GregorianCalendar.class, new GregorianCalendarSerializer());
|
||||
kryo.register(InvocationHandler.class, new JdkProxySerializer());
|
||||
kryo.register(BigDecimal.class, new DefaultSerializers.BigDecimalSerializer());
|
||||
kryo.register(BigInteger.class, new DefaultSerializers.BigIntegerSerializer());
|
||||
kryo.register(Pattern.class, new RegexSerializer());
|
||||
kryo.register(BitSet.class, new BitSetSerializer());
|
||||
kryo.register(URI.class, new URISerializer());
|
||||
kryo.register(UUID.class, new UUIDSerializer());
|
||||
|
||||
// register commonly class
|
||||
SerializerClassRegistry.getRegisteredClasses().keySet().forEach(kryo::register);
|
||||
return kryo;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
io.seata.serializer.kryo.KryoSerializer
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 1999-2019 Seata.io Group.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.seata.serializer.kryo;
|
||||
|
||||
import io.seata.core.exception.TransactionExceptionCode;
|
||||
import io.seata.core.model.BranchStatus;
|
||||
import io.seata.core.model.BranchType;
|
||||
import io.seata.core.protocol.ResultCode;
|
||||
import io.seata.core.protocol.transaction.BranchCommitRequest;
|
||||
import io.seata.core.protocol.transaction.BranchCommitResponse;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author jsbxyyx
|
||||
*/
|
||||
public class KryoSerializerTest {
|
||||
|
||||
private static KryoSerializer kryoCodec;
|
||||
|
||||
@BeforeAll
|
||||
public static void before() {
|
||||
kryoCodec = new KryoSerializer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBranchCommitRequest() {
|
||||
|
||||
BranchCommitRequest branchCommitRequest = new BranchCommitRequest();
|
||||
branchCommitRequest.setBranchType(BranchType.AT);
|
||||
branchCommitRequest.setXid("xid");
|
||||
branchCommitRequest.setResourceId("resourceId");
|
||||
branchCommitRequest.setBranchId(20190809);
|
||||
branchCommitRequest.setApplicationData("app");
|
||||
|
||||
byte[] bytes = kryoCodec.serialize(branchCommitRequest);
|
||||
BranchCommitRequest t = kryoCodec.deserialize(bytes);
|
||||
|
||||
assertThat(t.getTypeCode()).isEqualTo(branchCommitRequest.getTypeCode());
|
||||
assertThat(t.getBranchType()).isEqualTo(branchCommitRequest.getBranchType());
|
||||
assertThat(t.getXid()).isEqualTo(branchCommitRequest.getXid());
|
||||
assertThat(t.getResourceId()).isEqualTo(branchCommitRequest.getResourceId());
|
||||
assertThat(t.getBranchId()).isEqualTo(branchCommitRequest.getBranchId());
|
||||
assertThat(t.getApplicationData()).isEqualTo(branchCommitRequest.getApplicationData());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBranchCommitResponse() {
|
||||
|
||||
BranchCommitResponse branchCommitResponse = new BranchCommitResponse();
|
||||
branchCommitResponse.setTransactionExceptionCode(TransactionExceptionCode.BranchTransactionNotExist);
|
||||
branchCommitResponse.setBranchId(20190809);
|
||||
branchCommitResponse.setBranchStatus(BranchStatus.PhaseOne_Done);
|
||||
branchCommitResponse.setMsg("20190809");
|
||||
branchCommitResponse.setXid("20190809");
|
||||
branchCommitResponse.setResultCode(ResultCode.Failed);
|
||||
|
||||
byte[] bytes = kryoCodec.serialize(branchCommitResponse);
|
||||
BranchCommitResponse t = kryoCodec.deserialize(bytes);
|
||||
|
||||
assertThat(t.getTransactionExceptionCode()).isEqualTo(branchCommitResponse.getTransactionExceptionCode());
|
||||
assertThat(t.getBranchId()).isEqualTo(branchCommitResponse.getBranchId());
|
||||
assertThat(t.getBranchStatus()).isEqualTo(branchCommitResponse.getBranchStatus());
|
||||
assertThat(t.getMsg()).isEqualTo(branchCommitResponse.getMsg());
|
||||
assertThat(t.getResultCode()).isEqualTo(branchCommitResponse.getResultCode());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user