Skip to content

Commit

Permalink
Support protostuff as serialization. (sofastack#531)
Browse files Browse the repository at this point in the history
* support protostuff as serialization

* fix parent pom

* fix cr
  • Loading branch information
leizhiyuan authored May 22, 2019
1 parent 95bada5 commit a2d4473
Show file tree
Hide file tree
Showing 13 changed files with 870 additions and 0 deletions.
6 changes: 6 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
<artifactId>sofa-rpc-codec-protobuf</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-codec-protostuff</artifactId>
<version>${sofa.rpc.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-codec-jackson</artifactId>
Expand Down Expand Up @@ -391,6 +396,7 @@
<include>com.alipay.sofa:sofa-rpc-bootstrap-http</include>
<include>com.alipay.sofa:sofa-rpc-bootstrap-rest</include>
<include>com.alipay.sofa:sofa-rpc-codec-protobuf</include>
<include>com.alipay.sofa:sofa-rpc-codec-protostuff</include>
<include>com.alipay.sofa:sofa-rpc-codec-jackson</include>
<include>com.alipay.sofa:sofa-rpc-codec-sofa-hessian</include>
<include>com.alipay.sofa:sofa-rpc-fault-tolerance</include>
Expand Down
12 changes: 12 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<jackson.version>2.9.8</jackson.version>
<jackson.annotation.version>2.9.0</jackson.annotation.version>
<msgpack.version>0.6.11</msgpack.version>
<protostuff.version>1.5.9</protostuff.version>

<!--common-->
<httpcomponents.version>4.4.9</httpcomponents.version>
<httpcomponents.httpmime.version>4.5.5</httpcomponents.httpmime.version>
Expand Down Expand Up @@ -267,6 +269,16 @@
<artifactId>hessian</artifactId>
<version>${hessian.version}</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>${protostuff.version}</version>
</dependency>

<!-- zk client -->
<dependency>
Expand Down
110 changes: 110 additions & 0 deletions extension-impl/codec-protostuff/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-extension-impl</artifactId>
<version>5.6.0-SNAPSHOT</version>
</parent>

<artifactId>sofa-rpc-codec-protostuff</artifactId>

<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-api</artifactId>
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-extension-common</artifactId>
<version>${project.parent.version}</version>
</dependency>

<!-- 默认不主动依赖 -->
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/**</include>
</includes>
</testResource>
</testResources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<skip>${module.install.skip}</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>${module.deploy.skip}</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${skipTests}</skipTests>
<includes>
<!-- 这里需要根据自己的需要指定要跑的单元测试 -->
<include>**/*Test.java</include>
</includes>
<!-- 如无特殊需求,将forkMode设置为once -->
<forkMode>once</forkMode>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alipay.sofa.rpc.codec.protostuff;

import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.alipay.sofa.rpc.common.utils.ClassUtils;
import com.alipay.sofa.rpc.config.ConfigUniqueNameGenerator;
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;

/**
* @author leizhiyuan
*/
public class ProtostuffHelper {
/**
* 请求参数类型缓存 {service+method:class}
*/
private ConcurrentMap<String, Class> requestClassCache = new ConcurrentHashMap<String, Class>();

/**
* 返回结果类型缓存 {service+method:class}
*/
private ConcurrentMap<String, Class> responseClassCache = new ConcurrentHashMap<String, Class>();

/**
* 从缓存中获取请求值类
*
* @param service 接口名
* @param methodName 方法名
* @return 请求参数类
*/
public Class getReqClass(String service, String methodName) {

String key = buildMethodKey(service, methodName);
Class reqClass = requestClassCache.get(key);
if (reqClass == null) {
// 读取接口里的方法参数和返回值
String interfaceClass = ConfigUniqueNameGenerator.getInterfaceName(service);
Class clazz = ClassUtils.forName(interfaceClass, true);
loadProtoClassToCache(key, clazz, methodName);
}
return requestClassCache.get(key);
}

/**
* 从缓存中获取返回值类
*
* @param service 接口名
* @param methodName 方法名
* @return 请求参数类
*/
public Class getResClass(String service, String methodName) {
String key = service + "#" + methodName;
Class reqClass = responseClassCache.get(key);
if (reqClass == null) {
// 读取接口里的方法参数和返回值
String interfaceClass = ConfigUniqueNameGenerator.getInterfaceName(service);
Class clazz = ClassUtils.forName(interfaceClass, true);
loadProtoClassToCache(key, clazz, methodName);
}
return responseClassCache.get(key);
}

/**
* 拼装缓存的key
*
* @param serviceName 接口名
* @param methodName 方法名
* @return Key
*/
private String buildMethodKey(String serviceName, String methodName) {
return serviceName + "#" + methodName;
}

/**
* 加载protobuf接口里方法的参数和返回值类型到缓存,不需要传递
*
* @param key 缓存的key
* @param clazz 接口名
* @param methodName 方法名
*/
private void loadProtoClassToCache(String key, Class clazz, String methodName) {
Method pbMethod = null;
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (methodName.equals(method.getName())) {
pbMethod = method;
break;
}
}
if (pbMethod == null) {
throw new SofaRpcRuntimeException("Cannot found protobuf method: " + clazz.getName() + "." + methodName);
}
Class[] parameterTypes = pbMethod.getParameterTypes();
if (parameterTypes == null
|| parameterTypes.length != 1) {
throw new SofaRpcRuntimeException("class based protobuf: " + clazz.getName()
+ ", only support one protobuf parameter!");
}
Class reqClass = parameterTypes[0];
requestClassCache.put(key, reqClass);
Class resClass = pbMethod.getReturnType();
if (resClass == void.class) {
throw new SofaRpcRuntimeException("class based protobuf: " + clazz.getName()
+ ", only support return protobuf message!");
}
responseClassCache.put(key, resClass);
}
}
Loading

0 comments on commit a2d4473

Please sign in to comment.