forked from sofastack/sofa-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support protostuff as serialization. (sofastack#531)
* support protostuff as serialization * fix parent pom * fix cr
- Loading branch information
1 parent
95bada5
commit a2d4473
Showing
13 changed files
with
870 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
125 changes: 125 additions & 0 deletions
125
...codec-protostuff/src/main/java/com/alipay/sofa/rpc/codec/protostuff/ProtostuffHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.