Skip to content

Commit

Permalink
修改traceId的传递方式
Browse files Browse the repository at this point in the history
  • Loading branch information
fangyanp committed Jan 25, 2018
1 parent 08e0a20 commit 2fd5237
Show file tree
Hide file tree
Showing 41 changed files with 101 additions and 96 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Eagle是一个分布式的RPC框架,支持灵活的配置,支持分布式追
> * cd eagle-benchmark
> * mvn clean install
> * cd eagle-benchmark-server/target
> * tar -zxvf eagle-benchmark-server-1.5-assembly.tar.gz
> * cd eagle-benchmark-server-1.5
> * tar -zxvf eagle-benchmark-server-1.6-assembly.tar.gz
> * cd eagle-benchmark-server-1.6
> * bin/start.sh
> * cd eagle-benchmark/eagle-benchmark-client
> * 在linux上运行 sh benchmark.sh,在window上运行 benchmark.cmd
Expand All @@ -44,45 +44,45 @@ Eagle是一个分布式的RPC框架,支持灵活的配置,支持分布式追
<dependency>
<groupId>org.jfaster.eagle</groupId>
<artifactId>eagle-core</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jfaster.eagle</groupId>
<artifactId>eagle-registry-zookeeper</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jfaster.eagle</groupId>
<artifactId>eagle-transport-netty</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jfaster.eagle</groupId>
<artifactId>eagle-spring-support</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
```
如果是springBoot,添加如下:
```xml
<dependency>
<groupId>org.jfaster.eagle</groupId>
<artifactId>eagle-core</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jfaster.eagle</groupId>
<artifactId>eagle-registry-zookeeper</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jfaster.eagle</groupId>
<artifactId>eagle-transport-netty</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jfaster.eagle</groupId>
<artifactId>spring-boot-starter-eagle</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
```
## 分布式调用追踪
Expand Down Expand Up @@ -843,8 +843,8 @@ log4j.appender.CONSOLE.layout.ConversionPattern=%d [%T] %-5p %c{1}:%L - %m%n
# 后台管理界面
> eagle 提供可视化的后台管理,方便查看和修改配置。
> 启动后台的步骤
* tar -zxvf eagle-ui-1.5.tar.gz
* cd eagle-ui-1.5
* tar -zxvf eagle-ui-1.6.tar.gz
* cd eagle-ui-1.6
* vim conf/eagle.conf 修改用户名、密码、jvm参数、日志路径、端口号等
* sh bin/eagle.sh start

Expand Down
2 changes: 1 addition & 1 deletion eagle-benchmark/eagle-benchmark-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>eagle-benchmark</artifactId>
<groupId>org.jfaster.eagle</groupId>
<version>1.5</version>
<version>1.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion eagle-benchmark/eagle-benchmark-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>eagle-benchmark</artifactId>
<groupId>org.jfaster.eagle</groupId>
<version>1.5</version>
<version>1.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion eagle-benchmark/eagle-benchmark-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>eagle-benchmark</artifactId>
<groupId>org.jfaster.eagle</groupId>
<version>1.5</version>
<version>1.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion eagle-benchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<modelVersion>4.0.0</modelVersion>
<groupId>org.jfaster.eagle</groupId>
<version>1.5</version>
<version>1.6</version>
<artifactId>eagle-benchmark</artifactId>
<packaging>pom</packaging>

Expand Down
2 changes: 1 addition & 1 deletion eagle-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>eagle</artifactId>
<groupId>org.jfaster.eagle</groupId>
<version>1.5</version>
<version>1.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import eagle.jfaster.org.cluster.LoadBalance;
import eagle.jfaster.org.cluster.ReferCluster;
import eagle.jfaster.org.config.common.MergeConfig;
import eagle.jfaster.org.exception.EagleFrameException;
import eagle.jfaster.org.exception.MockException;
import eagle.jfaster.org.logging.InternalLogger;
import eagle.jfaster.org.logging.InternalLoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
}
EagleRequest request = new EagleRequest();
request.setInterfaceName(interfaceName);
String opaque = TraceContext.getOpaque();
if(Strings.isNullOrEmpty(opaque)){
opaque = OpaqueGenerator.getDistributeOpaque();
TraceContext.setOpaque(opaque);
String traceId = TraceContext.getTraceId();
if(!Strings.isNullOrEmpty(traceId)){
request.setAttachment(TraceContext.TRACE_KEY,traceId);
}
request.setOpaque(opaque);
request.setOpaque(OpaqueGenerator.getOpaque());
request.setParameters(args);
request.setMethodName(method.getName());
request.setNeedCompress(compress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import eagle.jfaster.org.util.ReflectUtil;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by fangyanpeng1 on 2017/8/7.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public interface Codec {

ByteBuffer encode(Object message,Serialization serialization) throws IOException;

Object decode(ByteBuffer buffer,Serialization serialization,String opaque,short magicCode) throws IOException;
Object decode(ByteBuffer buffer,Serialization serialization,int opaque,short magicCode) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
* 编解码器
*
* 请求协议:magicCode(2个字节) + opaque(32个字节)
* 请求协议:magicCode(2个字节) + opaque(4个字节)
* + interfaceName长度(2个字节) +interfaceName
* + methodName长度(2个字节) + methodName
* + parameterDesc长度(4个字节) + parameterDesc
Expand Down Expand Up @@ -83,7 +83,7 @@ private ByteBuffer encodeResponse(Response response,Serialization serialization)
if(response.isNeedCompress()){
magicCode |= EAGLE_COMPRESS_TYPE;
}
int dataLen = 38;//totalLen+magic+opaque 4+2+32
int dataLen = 10;//totalLen+magic+opaque 4+2+4
ByteBuffer content;
if(response.getException() != null){
content = encodeResponseCommon(response.getException(),dataLen,magicCode,response.getOpaque(),serialization,false,EAGLE_RESPONSE_EXCEPTION);
Expand All @@ -93,14 +93,14 @@ private ByteBuffer encodeResponse(Response response,Serialization serialization)
content = ByteBuffer.allocate(dataLen);
content.putInt(dataLen);
content.putShort(magicCode);
content.put(response.getOpaque().getBytes());
content.putInt(response.getOpaque());
}
content.flip();
return content;

}

private ByteBuffer encodeResponseCommon(Object res,int dataLen,short magicCode,String opaque,Serialization serialization,boolean compress,short flag) throws IOException {
private ByteBuffer encodeResponseCommon(Object res,int dataLen,short magicCode,int opaque,Serialization serialization,boolean compress,short flag) throws IOException {
magicCode |= flag;
String className = res.getClass().getName();
byte[] nameData = className.getBytes(CHARSET_UTF8);
Expand All @@ -115,7 +115,7 @@ private ByteBuffer encodeResponseCommon(Object res,int dataLen,short magicCode,S
ByteBuffer content = ByteBuffer.allocate(dataLen);
content.putInt(dataLen);
content.putShort(magicCode);
content.put(opaque.getBytes(CHARSET_UTF8));
content.putInt(opaque);
content.putShort((short) nameData.length);
content.put(nameData);
content.putInt(valData.length);
Expand All @@ -132,7 +132,7 @@ private ByteBuffer encodeRequest(Request request,Serialization serialization) th
if(request.isNeedCompress()){
magicCode |= EAGLE_COMPRESS_TYPE;
}
int dataLen = 42;//totalLen+magic+opaque+attachments 4+2+32+4
int dataLen = 14;//totalLen+magic+opaque+attachments 4+2+4+4
String interfaceName = request.getInterfaceName();
byte[] iNameData = interfaceName.getBytes(CHARSET_UTF8);
dataLen += iNameData.length + 2;
Expand Down Expand Up @@ -181,11 +181,11 @@ private ByteBuffer encodeRequest(Request request,Serialization serialization) th
return content;
}

private ByteBuffer encodeReqCommon(int dataLen,short magicCode,String opaque,byte[] iNameData,byte[] mNameData){
private ByteBuffer encodeReqCommon(int dataLen,short magicCode,int opaque,byte[] iNameData,byte[] mNameData){
ByteBuffer content = ByteBuffer.allocate(dataLen);
content.putInt(dataLen);
content.putShort(magicCode);
content.put(opaque.getBytes(CHARSET_UTF8));
content.putInt(opaque);
content.putShort((short) iNameData.length);
content.put(iNameData);
content.putShort((short) mNameData.length);
Expand Down Expand Up @@ -230,7 +230,7 @@ public static byte[] encodeAttachments(Map<String, String> map) {
}

@Override
public Object decode(ByteBuffer buffer, Serialization serialization, String opaque, short magicCode) throws IOException {
public Object decode(ByteBuffer buffer, Serialization serialization, int opaque, short magicCode) throws IOException {
try {
if(isRequest(magicCode)){
return decodeRequest(buffer,serialization,opaque,magicCode);
Expand All @@ -242,7 +242,7 @@ public Object decode(ByteBuffer buffer, Serialization serialization, String opaq
}
}

private Object decodeRequest(ByteBuffer buffer,Serialization serialization,String opaque,short magicCode)
private Object decodeRequest(ByteBuffer buffer,Serialization serialization,int opaque,short magicCode)
throws IOException, ClassNotFoundException {
EagleRequest request = new EagleRequest();
request.setOpaque(opaque);
Expand Down Expand Up @@ -318,7 +318,7 @@ private Map<String, String> decodeRequestAttachments(ByteBuffer buffer) throws I
return attachments;
}

public Object decodeResponse(ByteBuffer buffer, Serialization serialization, String opaque, short magicCode)
public Object decodeResponse(ByteBuffer buffer, Serialization serialization, int opaque, short magicCode)
throws ClassNotFoundException, IOException {
EagleResponse response = new EagleResponse();
response.setOpaque(opaque);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public interface Request {

String getOpaque();
int getOpaque();

String getInterfaceName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface Response {
boolean isNeedCompress();

//请求的唯一标识
String getOpaque();
int getOpaque();

//附加信息
Map<String, String> getAttachments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class EagleRequest implements Request {

@Setter
private String opaque;
private int opaque;

@Setter
private String interfaceName;
Expand All @@ -52,7 +52,7 @@ public class EagleRequest implements Request {
private Map<String,String> attachments;

@Override
public String getOpaque() {
public int getOpaque() {
return opaque;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class EagleResponse implements Response {
private Exception exception;

@Setter
private String opaque;
private int opaque;

@Setter
private boolean needCompress;
Expand All @@ -61,7 +61,7 @@ public boolean isNeedCompress() {
}

@Override
public String getOpaque() {
public int getOpaque() {
return opaque;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@
*/
public class TraceContext {

private static InheritableThreadLocal<String> opaqueManager = new InheritableThreadLocal<>();
public static String TRACE_KEY="traceId";

public static String getOpaque(){
return opaqueManager.get();
private static InheritableThreadLocal<String> traceIdManager = new InheritableThreadLocal<>();

public static String getTraceId(){
return traceIdManager.get();
}

public static void setOpaque(String opaque){
opaqueManager.set(opaque);
public static void setTraceId(String traceId){
traceIdManager.set(traceId);
}

public static void clear(){
opaqueManager.remove();
traceIdManager.remove();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
public class TraceIdPatternConverter extends PatternConverter {
@Override
protected String convert(LoggingEvent loggingEvent) {
return Strings.isNullOrEmpty(TraceContext.getOpaque()) ? "N/A" : TraceContext.getOpaque();
return Strings.isNullOrEmpty(TraceContext.getTraceId()) ? "N/A" : TraceContext.getTraceId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
public class LogbackPatternConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent iLoggingEvent) {
return Strings.isNullOrEmpty(TraceContext.getOpaque()) ? "N/A" : TraceContext.getOpaque();
return Strings.isNullOrEmpty(TraceContext.getTraceId()) ? "N/A" : TraceContext.getTraceId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class EagleHeartBeatFactory implements HeartBeatFactory {
@Override
public Request createRequest() {
EagleRequest request = new EagleRequest();
request.setOpaque(OpaqueGenerator.getDistributeOpaque());
request.setOpaque(OpaqueGenerator.getOpaque());
request.setInterfaceName(HEARTBEAT_INTERFACE_NAME);
request.setMethodName(HEARTBEAT_METHOD_NAME);
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static boolean isVoidValue(short magicCode){
return (magicCode & EAGLE_RESPONSE_TYPE) == EAGLE_RESPONSE_VOID;
}

public static EagleResponse buildExceptionResponse(String opaque,Exception e){
public static EagleResponse buildExceptionResponse(int opaque,Exception e){
EagleResponse response = new EagleResponse();
response.setOpaque(opaque);
response.setException(e);
Expand Down
2 changes: 1 addition & 1 deletion eagle-example/eagle-example-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.jfaster.eagle</groupId>
<artifactId>eagle-example-spring</artifactId>
<version>1.5</version>
<version>1.6</version>
<packaging>jar</packaging>

<name>eagle-example-spring</name>
Expand Down
2 changes: 1 addition & 1 deletion eagle-example/eagle-example-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>org.jfaster.eagle</groupId>
<artifactId>eagle-example-springboot</artifactId>
<version>1.5</version>
<version>1.6</version>
<packaging>jar</packaging>

<name>eagle-example-springboot</name>
Expand Down
2 changes: 1 addition & 1 deletion eagle-example/eagle-example-springmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>eagle-example</artifactId>
<groupId>org.jfaster.eagle</groupId>
<version>1.5</version>
<version>1.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eagle-example-springmvc</artifactId>
Expand Down
Loading

0 comments on commit 2fd5237

Please sign in to comment.