Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ad95896
fix tars define recursive
walkertest May 14, 2020
4eb9f01
[issue] setRefreshInterval and reportInterval No effect
TimmyYu May 15, 2020
b1bff89
update printHead Tars version and add CompletableFuture interface wit…
yukkiball May 21, 2020
9857959
update tars_java_user_guide
yukkiball May 25, 2020
f2aac43
Merge pull request #86 from yukkiball/master
TimmyYu May 25, 2020
9f63905
Merge pull request #83 from walkertest/master
TimmyYu May 25, 2020
b107e67
remove CompletableFuture usage
yukkiball May 25, 2020
d7d2c49
[format] format tars code
TimmyYu May 25, 2020
dda4398
add example logback config
TimmyYu May 25, 2020
b0817d8
[fix] fix property set report error
TimmyYu May 27, 2020
dfefe8d
Merge branch 'master' of https://github.com/TarsCloud/TarsJava
yukkiball May 27, 2020
0704908
add Struct toString method
yukkiball May 27, 2020
52baf69
add logback usage
yukkiball May 29, 2020
830715f
Merge pull request #89 from yukkiball/master
TimmyYu May 31, 2020
ee275b7
Merge remote-tracking branch 'remotes/upstream/v1.7.x'
TimmyYu May 31, 2020
3506230
[change] Use gson instead of fastjson
TimmyYu May 31, 2020
ba725eb
bool变量如果是is开头的特殊处理
Jun 4, 2020
5dfa548
Update README.md
TimmyYu Jun 7, 2020
95e1fa0
Update README.md
TimmyYu Jun 7, 2020
dbd1210
upload LICENSE.md
KatharineOzil Jun 9, 2020
47f338c
新增tars文件自动生成代码,如果是float带上F后缀;修改json协议version值;HelloServer json协议测试通过
Jun 9, 2020
43b37ce
Merge branch 'v1.7.x' into v1.7.x
jiaminzou888 Jun 9, 2020
73d2911
Merge branch 'master' into v1.7.x
jiaminzou888 Jun 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ target/
.project
.classpath
*.dat

examples/**/testapp/
157 changes: 157 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This project is the source code of the Tars RPC framework Java language.
<table>
<tr>
<td><b>Homepage:</b></td>
<td><a href="https://tars.tencent.com/base/tars_index/en/index.html">tarscloud.org</a></td>
<td><a href="https://tarscloud.org">tarscloud.org</a></td>
</tr>
<tr>
<td><b>中文版:</b></td>
Expand All @@ -21,7 +21,7 @@ This project is the source code of the Tars RPC framework Java language.

### Environmental dependence
- JDK1.8 or above
- Maven 2.2.1 or above
- Maven 3.5 or above


###
Expand Down
5 changes: 5 additions & 0 deletions core/client.pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<version>${maven_junit_version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 4 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<brave-opentracing.version>0.31.1</brave-opentracing.version>
<zipkin-reporter.version>2.7.4</zipkin-reporter.version>
<ch.qos.logback.version>1.2.3</ch.qos.logback.version>
<fastjson.version>1.2.62</fastjson.version>
<gson.version>2.8.6</gson.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -70,9 +70,9 @@
</dependency>
<!--tars buffer support json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
</dependencies>
</project>
3 changes: 3 additions & 0 deletions core/src/main/java/com/qq/tars/client/ObjectProxyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public <T> ObjectProxy<T> getObjectProxy(Class<T> api, String objName, String se
servantProxyConfig.setModuleName(communicator.getCommunicatorConfig().getModuleName(), communicator.getCommunicatorConfig().isEnableSet(), communicator.getCommunicatorConfig().getSetDivision());
servantProxyConfig.setLocator(communicator.getCommunicatorConfig().getLocator());
addSetDivisionInfo(servantProxyConfig, setDivision);
servantProxyConfig.setRefreshInterval(communicator.getCommunicatorConfig().getRefreshEndpointInterval());
servantProxyConfig.setReportInterval(communicator.getCommunicatorConfig().getReportInterval());
}

updateServantEndpoints(servantProxyConfig);
Expand Down Expand Up @@ -118,6 +120,7 @@ private ServantProxyConfig createServantProxyConfig(String objName, String setDi
cfg.setCharsetName(communicatorConfig.getCharsetName());
cfg.setConnections(communicatorConfig.getConnections());
cfg.setRefreshInterval(communicatorConfig.getRefreshEndpointInterval());
cfg.setReportInterval(communicator.getCommunicatorConfig().getReportInterval());
return cfg;
}

Expand Down
29 changes: 29 additions & 0 deletions core/src/main/java/com/qq/tars/common/util/JSON.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.qq.tars.common.util;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

import java.lang.reflect.Type;

public class JSON {
private static final Gson gson = new Gson();


public static String toJson(final Object obj) {
return gson.toJson(obj);
}

public static <T> T fromJson(final String str, Class<T> cls) {
return gson.fromJson(str, cls);
}


public static JsonElement toJsonTree(final Object obj) {
return gson.toJsonTree(obj);
}


public static <T> T fromJson(final String str, Type type) {
return gson.fromJson(str, type);
}
}
69 changes: 69 additions & 0 deletions core/src/main/java/com/qq/tars/protocol/util/EncodingUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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.qq.tars.protocol.util;

public class EncodingUtils {
public EncodingUtils() {
}

public static final boolean testBit(byte v, int position) {
return testBit((int)v, position);
}

public static final boolean testBit(short v, int position) {
return testBit((int)v, position);
}

public static final boolean testBit(int v, int position) {
return (v & 1 << position) != 0;
}

public static final boolean testBit(long v, int position) {
return (v & 1L << position) != 0L;
}

public static final byte clearBit(byte v, int position) {
return (byte)clearBit((int)v, position);
}

public static final short clearBit(short v, int position) {
return (short)clearBit((int)v, position);
}

public static final int clearBit(int v, int position) {
return v & ~(1 << position);
}

public static final long clearBit(long v, int position) {
return v & ~(1L << position);
}

public static final byte setBit(byte v, int position, boolean value) {
return (byte)setBit((int)v, position, value);
}

public static final short setBit(short v, int position, boolean value) {
return (short)setBit((int)v, position, value);
}

public static final int setBit(int v, int position, boolean value) {
return value ? v | 1 << position : clearBit(v, position);
}

public static final long setBit(long v, int position, boolean value) {
return value ? v | 1L << position : clearBit(v, position);
}
}
8 changes: 4 additions & 4 deletions core/src/main/java/com/qq/tars/protocol/util/TarsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class TarsHelper {

public static final short VERSION3 = 0x03;

public static final short VERSIONJSON = 0x09;
public static final short VERSIONJSON = 0x05;

public static final byte NORMAL = 0x00;

Expand Down Expand Up @@ -588,9 +588,9 @@ public static boolean isRoutekey(Annotation[] annotations) {

public static boolean isStruct(Class<?> clazz) {
boolean isStruct = clazz.isAnnotationPresent(TarsStruct.class);
if (isStruct) {
getStructInfo(clazz);
}
// if (isStruct) {
// getStructInfo(clazz);
// }
return isStruct;
}

Expand Down
42 changes: 25 additions & 17 deletions core/src/main/java/com/qq/tars/rpc/protocol/tars/TarsCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.qq.tars.rpc.protocol.tars;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.qq.tars.common.support.ClassLoaderManager;
import com.qq.tars.common.support.Holder;
import com.qq.tars.common.util.CollectionUtils;
import com.qq.tars.common.util.Constants;
import com.qq.tars.common.util.JSON;
import com.qq.tars.common.util.StringUtils;
import com.qq.tars.net.core.IoBuffer;
import com.qq.tars.net.core.Request;
Expand All @@ -38,10 +41,6 @@
import com.qq.tars.rpc.protocol.tars.support.AnalystManager;
import com.qq.tars.rpc.protocol.tup.UniAttribute;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -170,7 +169,7 @@ protected byte[] encodeJsonResult(TarsServantResponse response, String charsetNa
}

// 服务端接口响应
JSONObject object = new JSONObject();
JsonObject object = new JsonObject();

int ret = response.getRet();
Map<String, TarsMethodInfo> methodInfoMap = AnalystManager.getInstance().getMethodMapByName(request.getServantName());
Expand All @@ -179,7 +178,9 @@ protected byte[] encodeJsonResult(TarsServantResponse response, String charsetNa
TarsMethodParameterInfo returnInfo = methodInfo.getReturnInfo();
if (returnInfo != null && returnInfo.getType() != Void.TYPE && response.getResult() != null) {
try {
object.put(TarsHelper.STAMP_STRING, response.getResult());
JsonElement jsonElement = JSON.toJsonTree(response.getResult());
// System.out.println("requestId: " + request.getRequestId() + ", charset: " + request.getCharsetName() + ", ret: " + jsonElement.toString());
object.add(TarsHelper.STAMP_STRING, jsonElement);
} catch (Exception e) {
System.err.println("server encode json ret :" + response.getResult() + ", with ex:" + e);
}
Expand All @@ -192,7 +193,9 @@ protected byte[] encodeJsonResult(TarsServantResponse response, String charsetNa
value = request.getMethodParameters()[parameterInfo.getOrder() - 1];
if (value != null) {
try {
object.put(parameterInfo.getName(), TarsHelper.getHolderValue(value));
JsonElement jsonElement = JSON.toJsonTree(TarsHelper.getHolderValue(value));
// System.out.println("requestId: " + request.getRequestId() + ", charset: " + request.getCharsetName() + ", holder: " + jsonElement.toString());
object.add(parameterInfo.getName(), jsonElement);
} catch (Exception e) {
System.err.println("server encode json holder :" + value + ", with ex:" + e);
}
Expand All @@ -218,7 +221,7 @@ protected byte[] encodeWupResult(TarsServantResponse response, String charsetNam
unaOut.setEncodeName(charsetName);
if (response.getVersion() == TarsHelper.VERSION3) {
unaOut.useVersion3();
} else if(response.getVersion() == TarsHelper.VERSION2) {
} else if (response.getVersion() == TarsHelper.VERSION2) {
unaOut.setNewDataNull();
}

Expand Down Expand Up @@ -397,6 +400,7 @@ public ServantRequest decodeRequestBody(ServantRequest req) {
} else if (TarsHelper.VERSION2 == request.getVersion() || TarsHelper.VERSION3 == request.getVersion()) {
parameters = decodeRequestWupBody(data, request.getVersion(), request.getCharsetName(), methodInfo);
} else if (TarsHelper.VERSIONJSON == request.getVersion()) {
// System.out.println("requestId: " + request.getRequestId() + ", charset: " + request.getCharsetName() + ", data: " + new String(data, request.getCharsetName()));
parameters = decodeRequestJsonBody(data, request.getCharsetName(), methodInfo);
} else {
request.setRet(TarsHelper.SERVERDECODEERR);
Expand Down Expand Up @@ -444,7 +448,7 @@ protected Object[] decodeRequestBody(byte[] data, String charset, TarsMethodInfo
}

protected Object[] decodeRequestWupBody(byte[] data, int version, String charset,
TarsMethodInfo methodInfo) throws Exception {
TarsMethodInfo methodInfo) throws Exception {
//wup request
UniAttribute unaIn = new UniAttribute();
unaIn.setEncodeName(charsetName);
Expand Down Expand Up @@ -479,9 +483,9 @@ protected Object[] decodeRequestWupBody(byte[] data, int version, String charset
}

protected Object[] decodeRequestJsonBody(byte[] data, String charset,
TarsMethodInfo methodInfo) throws Exception {
TarsMethodInfo methodInfo) throws Exception {
// 解析json串
JSONObject jsonObject = JSON.parseObject(new String(data, charset));
JsonObject jsonObject = JSON.fromJson(new String(data, charset), JsonObject.class);

// 按字段反序列化
int i = 0;
Expand All @@ -492,18 +496,22 @@ protected Object[] decodeRequestJsonBody(byte[] data, String charset,

for (TarsMethodParameterInfo parameterInfo : parametersList) {
if (TarsHelper.isHolder(parameterInfo.getAnnotations())) {
if (jsonObject.containsKey(parameterInfo.getName())) {
value = new Holder<>(JSON.parseObject(jsonObject.get(parameterInfo.getName()).toString(),
parameterInfo.getType()));
if (jsonObject.has(parameterInfo.getName())) {
String reqStr = jsonObject.get(parameterInfo.getName()).toString();
// System.out.println("holder has " + parameterInfo.getName() + ", str: " + reqStr);
value = new Holder<>(JSON.fromJson(reqStr, parameterInfo.getType()));
} else {
// System.out.println("holder has no " + parameterInfo.getName());
// new response, can not use cache
value = new Holder<>(TarsHelper.getNewParameterStamp(parameterInfo.getType()));
}
} else {
if (jsonObject.containsKey(parameterInfo.getName())) {
value = JSON.parseObject(jsonObject.get(parameterInfo.getName()).toString(),
parameterInfo.getType());
if (jsonObject.has(parameterInfo.getName())) {
String reqStr = jsonObject.get(parameterInfo.getName()).toString();
// System.out.println("request has " + parameterInfo.getName() + ", str: " + reqStr);
value = JSON.fromJson(reqStr, parameterInfo.getType());
} else {
System.out.println("request has no " + parameterInfo.getName() + ", exception.");
throw new ProtocolException("no found parameter, the context[ROOT], "
+ "serviceName[" + methodInfo.getServiceName()
+ "], methodName[" + methodInfo.getMethodName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.qq.tars.support.property;

import com.qq.tars.client.Communicator;
import com.qq.tars.client.CommunicatorConfig;
import com.qq.tars.net.util.Utils;
import com.qq.tars.rpc.exc.TarsException;
import com.qq.tars.server.config.ConfigurationManager;
import com.qq.tars.support.log.LoggerFactory;
Expand Down Expand Up @@ -66,8 +68,20 @@ public static class PropertyReporter {
private Collection<Policy> policies;
private String propRptName;
private String moduleName;
private String ip = "";
private String setName = "";
private String setArea = "";
private String setID = "";

public PropertyReporter(String moduleName, String propRptName, Policy[] policies) {
CommunicatorConfig config = ConfigurationManager.getInstance().getServerConfig().getCommunicatorConfig();

this.moduleName = !config.isEnableSet() ? moduleName : String.format("%s.%s%s%s", moduleName, config.getSetName(), config.getSetArea(), config.getSetID());
if (config.isEnableSet()) {
this.setName = config.getSetName();
this.setArea = config.getSetArea();
this.setID = config.getSetID();
}
this.propRptName = propRptName;

if (ConfigurationManager.getInstance().getCommunicatorConfig().isEnableSet()) {
Expand All @@ -76,25 +90,30 @@ public PropertyReporter(String moduleName, String propRptName, Policy[] policies
} else {
this.moduleName = moduleName;
}
this.ip = ConfigurationManager.getInstance().getServerConfig().getLocalIP();

Map<String, Policy> map = new HashMap<String, Policy>();
for (Policy policy : policies)
map.put(policy.desc(), policy);
this.policies = map.values();

this.ip = Utils.getLocalIp();

}


public void report(int value) {
for (Policy policy : policies)
policy.set(value);
}

public ReportData getReportData() {
int len = 0;
StatPropMsgHead head = new StatPropMsgHead(moduleName, "", propRptName, null, null, null, null, 1);
StatPropMsgHead head = new StatPropMsgHead(moduleName, this.ip, propRptName, setName, setArea, setID, null, 1);
len += moduleName.length() + propRptName.length();
int size = policies.size();
len += PROPERTY_PROTOCOL_LEN + size;
List<StatPropInfo> statPropInfos = new ArrayList<StatPropInfo>();
List<StatPropInfo> statPropInfos = new ArrayList<StatPropInfo>(policies.size());
for (Policy policy : policies) {
String desc = policy.desc();
String value = policy.get();
Expand Down
Loading