Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cf25a7

Browse files
committed
version6:step2-日志链路追踪实现
1 parent bbe8833 commit 9cf25a7

File tree

32 files changed

+357
-20
lines changed

32 files changed

+357
-20
lines changed
0 Bytes
Binary file not shown.

version6/krpc-common/src/main/java/common/serializer/mycoder/MyDecoder.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import common.exception.SerializeException;
55
import common.message.MessageType;
66
import common.serializer.myserializer.Serializer;
7+
import common.trace.TraceContext;
78
import io.netty.buffer.ByteBuf;
89
import io.netty.channel.ChannelHandlerContext;
910
import io.netty.handler.codec.ByteToMessageDecoder;
1011
import lombok.extern.slf4j.Slf4j;
12+
import org.slf4j.MDC;
1113

1214
import java.util.Arrays;
1315
import java.util.List;
@@ -27,32 +29,44 @@ protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, L
2729
if (in.readableBytes() < 6) { // messageType + serializerType + length
2830
return;
2931
}
30-
//1.读取消息类型
32+
//1.读取traceMsg
33+
int traceLength=in.readInt();
34+
byte[] traceBytes=new byte[traceLength];
35+
in.readBytes(traceBytes);
36+
serializeTraceMsg(traceBytes);
37+
//2.读取消息类型
3138
short messageType = in.readShort();
3239
// 现在还只支持request与response请求
3340
if (messageType != MessageType.REQUEST.getCode() &&
3441
messageType != MessageType.RESPONSE.getCode()) {
3542
log.warn("暂不支持此种数据, messageType: {}", messageType);
3643
return;
3744
}
38-
//2.读取序列化的方式&类型
45+
//3.读取序列化的方式&类型
3946
short serializerType = in.readShort();
4047
Serializer serializer = Serializer.getSerializerByCode(serializerType);
4148
if (serializer == null) {
4249
log.error("不存在对应的序列化器, serializerType: {}", serializerType);
4350
throw new SerializeException("不存在对应的序列化器, serializerType: " + serializerType);
4451
}
45-
//3.读取序列化数组长度
52+
//4.读取序列化数组长度
4653
int length = in.readInt();
4754
if (in.readableBytes() < length) {
4855
return; // 数据不完整,等待更多数据
4956
}
50-
//4.读取序列化数组
57+
//5.读取序列化数组
5158
byte[] bytes = new byte[length];
5259
in.readBytes(bytes);
5360
log.debug("Received bytes: {}", Arrays.toString(bytes));
5461
Object deserialize = serializer.deserialize(bytes, messageType);
5562

5663
out.add(deserialize);
5764
}
65+
//解析并存储traceMsg
66+
private void serializeTraceMsg(byte[] traceByte){
67+
String traceMsg=new String(traceByte);
68+
String[] msgs=traceMsg.split(";");
69+
if(!msgs[0].equals("")) TraceContext.setTraceId(msgs[0]);
70+
if(!msgs[1].equals("")) TraceContext.setParentSpanId(msgs[1]);
71+
}
5872
}

version6/krpc-common/src/main/java/common/serializer/mycoder/MyEncoder.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import common.message.RpcRequest;
66
import common.message.RpcResponse;
77
import common.serializer.myserializer.Serializer;
8+
import common.trace.TraceContext;
89
import io.netty.buffer.ByteBuf;
910
import io.netty.channel.ChannelHandlerContext;
1011
import io.netty.handler.codec.MessageToByteEncoder;
1112
import lombok.AllArgsConstructor;
1213
import lombok.extern.slf4j.Slf4j;
14+
import org.slf4j.MDC;
1315

1416
/**
1517
* @ClassName MyEncoder
@@ -26,7 +28,15 @@ public class MyEncoder extends MessageToByteEncoder {
2628
@Override
2729
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
2830
log.debug("Encoding message of type: {}", msg.getClass());
29-
//1.写入消息类型
31+
//1.写入trace消息头
32+
String traceMsg= TraceContext.getTraceId() +";"+TraceContext.getSpanId();
33+
byte[] traceBytes=traceMsg.getBytes();
34+
// 1.1写入traceMsg长度
35+
out.writeInt(traceBytes.length);
36+
// 1.2写入traceBytes
37+
out.writeBytes(traceBytes);
38+
39+
//2.写入消息类型
3040
if (msg instanceof RpcRequest) {
3141
out.writeShort(MessageType.REQUEST.getCode());
3242
} else if (msg instanceof RpcResponse) {
@@ -35,16 +45,16 @@ protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws
3545
log.error("Unknown message type: {}", msg.getClass());
3646
throw new IllegalArgumentException("Unknown message type: " + msg.getClass());
3747
}
38-
//2.写入序列化方式
48+
//3.写入序列化方式
3949
out.writeShort(serializer.getType());
4050
//得到序列化数组
4151
byte[] serializeBytes = serializer.serialize(msg);
4252
if (serializeBytes == null || serializeBytes.length == 0) {
4353
throw new IllegalArgumentException("Serialized message is empty");
4454
}
45-
//3.写入长度
55+
//4.写入长度
4656
out.writeInt(serializeBytes.length);
47-
//4.写入序列化数组
57+
//5.写入序列化数组
4858
out.writeBytes(serializeBytes);
4959
}
5060
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package common.trace;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.slf4j.MDC;
5+
6+
import java.util.Map;
7+
8+
/**
9+
* @author wxx
10+
* @version 1.0
11+
* @create 2025/2/18 18:37
12+
*/
13+
@Slf4j
14+
public class TraceContext {
15+
16+
public static void setTraceId(String traceId) {
17+
MDC.put("traceId",traceId);
18+
}
19+
20+
public static String getTraceId() {
21+
return MDC.get("traceId");
22+
}
23+
24+
public static void setSpanId(String spanId) {
25+
MDC.put("spanId",spanId);
26+
}
27+
28+
public static String getSpanId() {
29+
return MDC.get("spanId");
30+
}
31+
public static void setParentSpanId(String parentSpanId) {
32+
MDC.put("parentSpanId",parentSpanId);
33+
}
34+
35+
public static String getParentSpanId() {
36+
return MDC.get("parentSpanId");
37+
}
38+
public static void setStartTimestamp(String startTimestamp) {
39+
MDC.put("startTimestamp",startTimestamp);
40+
}
41+
42+
public static String getStartTimestamp() {
43+
return MDC.get("startTimestamp");
44+
}
45+
public static Map<String,String> getCopy(){
46+
return MDC.getCopyOfContextMap();
47+
}
48+
public static void clone(Map<String,String> context){
49+
for(Map.Entry<String,String> entry:context.entrySet()){
50+
System.out.println(entry.getKey()+":"+entry.getValue());
51+
MDC.put(entry.getKey(),entry.getValue());
52+
}
53+
}
54+
public static void clear() {
55+
MDC.clear();
56+
}
57+
}
586 Bytes
Binary file not shown.
357 Bytes
Binary file not shown.
2.91 KB
Binary file not shown.

version6/krpc-consumer/src/main/java/com/kama/consumer/ConsumerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@Slf4j
1919
public class ConsumerTest {
2020

21-
private static final int THREAD_POOL_SIZE = 20;
21+
private static final int THREAD_POOL_SIZE = 30;
2222
private static final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
2323

2424
public static void main(String[] args) throws InterruptedException {
0 Bytes
Binary file not shown.

version6/krpc-core/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,20 @@
111111
<groupId>junit</groupId>
112112
<artifactId>junit</artifactId>
113113
</dependency>
114+
<dependency>
115+
<groupId>io.zipkin.zipkin2</groupId>
116+
<artifactId>zipkin</artifactId>
117+
<version>3.4.0</version>
118+
</dependency>
119+
<dependency>
120+
<groupId>io.zipkin.reporter2</groupId>
121+
<artifactId>zipkin-reporter</artifactId>
122+
<version>3.4.0</version>
123+
</dependency>
124+
<dependency>
125+
<groupId>io.zipkin.reporter2</groupId>
126+
<artifactId>zipkin-sender-okhttp3</artifactId>
127+
<version>3.4.0</version>
128+
</dependency>
114129
</dependencies>
115130
</project>

0 commit comments

Comments
 (0)