Skip to content

Commit f1f1f56

Browse files
author
liushiming
committed
refector
1 parent 0ac3150 commit f1f1f56

File tree

13 files changed

+217
-231
lines changed

13 files changed

+217
-231
lines changed
Lines changed: 99 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
2-
* Copyright (c) 2016, Quancheng-ec.com All right reserved. This software is the
3-
* confidential and proprietary information of Quancheng-ec.com ("Confidential
4-
* Information"). You shall not disclose such Confidential Information and shall
5-
* use it only in accordance with the terms of the license agreement you entered
6-
* into with Quancheng-ec.com.
2+
* Copyright (c) 2016, Quancheng-ec.com All right reserved. This software is the confidential and
3+
* proprietary information of Quancheng-ec.com ("Confidential Information"). You shall not disclose
4+
* such Confidential Information and shall use it only in accordance with the terms of the license
5+
* agreement you entered into with Quancheng-ec.com.
76
*/
87
package com.quancheng.saluki.core.grpc.client;
98

@@ -13,8 +12,8 @@
1312
import com.google.protobuf.Message;
1413
import com.quancheng.saluki.core.common.Constants;
1514
import com.quancheng.saluki.core.common.GrpcURL;
16-
import com.quancheng.saluki.core.grpc.util.MethodDescriptorUtil;
17-
import com.quancheng.saluki.core.grpc.util.SerializerUtils;
15+
import com.quancheng.saluki.core.grpc.util.GrpcUtil;
16+
import com.quancheng.saluki.core.grpc.util.SerializerUtil;
1817
import com.quancheng.saluki.serializer.exception.ProtobufException;
1918

2019
import io.grpc.Channel;
@@ -26,137 +25,136 @@
2625
*/
2726
public interface GrpcRequest {
2827

29-
public Message getRequestArg() throws ProtobufException;
28+
public Message getRequestArg() throws ProtobufException;
3029

31-
public MethodDescriptor<Message, Message> getMethodDescriptor();
30+
public MethodDescriptor<Message, Message> getMethodDescriptor();
3231

33-
public Channel getChannel();
32+
public Channel getChannel();
3433

35-
public void returnChannel(Channel channel);
34+
public void returnChannel(Channel channel);
3635

37-
public String getServiceName();
36+
public String getServiceName();
3837

39-
public GrpcURL getRefUrl();
38+
public GrpcURL getRefUrl();
4039

41-
public MethodRequest getMethodRequest();
40+
public MethodRequest getMethodRequest();
4241

43-
public void setMethodRequest(MethodRequest methodRequest);
42+
public void setMethodRequest(MethodRequest methodRequest);
4443

45-
public static class Default implements GrpcRequest, Serializable {
44+
public static class Default implements GrpcRequest, Serializable {
4645

47-
private static final long serialVersionUID = 1L;
46+
private static final long serialVersionUID = 1L;
4847

49-
private final GrpcURL refUrl;
48+
private final GrpcURL refUrl;
5049

51-
private final GrpcProtocolClient.ChannelCall chanelPool;
50+
private final GrpcProtocolClient.ChannelCall chanelPool;
5251

53-
private MethodRequest methodRequest;
52+
private MethodRequest methodRequest;
5453

55-
public Default(GrpcURL refUrl, GrpcProtocolClient.ChannelCall chanelPool){
56-
super();
57-
this.refUrl = refUrl;
58-
this.chanelPool = chanelPool;
59-
}
60-
61-
@Override
62-
public Message getRequestArg() throws ProtobufException {
63-
Object arg = this.getMethodRequest().getArg();
64-
return SerializerUtils.Pojo2Protobuf(arg);
65-
}
54+
public Default(GrpcURL refUrl, GrpcProtocolClient.ChannelCall chanelPool) {
55+
super();
56+
this.refUrl = refUrl;
57+
this.chanelPool = chanelPool;
58+
}
6659

67-
@Override
68-
public MethodDescriptor<Message, Message> getMethodDescriptor() {
69-
Message argsReq = MethodDescriptorUtil.buildDefaultInstance(this.getMethodRequest().getRequestType());
70-
Message argsRep = MethodDescriptorUtil.buildDefaultInstance(this.getMethodRequest().getResponseType());
71-
return MethodDescriptorUtil.createMethodDescriptor(this.getServiceName(),
72-
this.getMethodRequest().getMethodName(), argsReq,
73-
argsRep);
74-
}
60+
@Override
61+
public Message getRequestArg() throws ProtobufException {
62+
Object arg = this.getMethodRequest().getArg();
63+
return SerializerUtil.pojo2Protobuf(arg);
64+
}
7565

76-
@Override
77-
public Channel getChannel() {
78-
return chanelPool.borrowChannel(refUrl);
79-
}
66+
@Override
67+
public MethodDescriptor<Message, Message> getMethodDescriptor() {
68+
Message argsReq = GrpcUtil.createDefaultInstance(this.getMethodRequest().getRequestType());
69+
Message argsRep = GrpcUtil.createDefaultInstance(this.getMethodRequest().getResponseType());
70+
return GrpcUtil.createMethodDescriptor(this.getServiceName(),
71+
this.getMethodRequest().getMethodName(), argsReq, argsRep);
72+
}
8073

81-
@Override
82-
public void returnChannel(Channel channel) {
83-
chanelPool.returnChannel(refUrl, channel);
84-
}
74+
@Override
75+
public Channel getChannel() {
76+
return chanelPool.borrowChannel(refUrl);
77+
}
8578

86-
@Override
87-
public String getServiceName() {
88-
return refUrl.getServiceInterface();
89-
}
79+
@Override
80+
public void returnChannel(Channel channel) {
81+
chanelPool.returnChannel(refUrl, channel);
82+
}
9083

91-
@Override
92-
public MethodRequest getMethodRequest() {
93-
return methodRequest;
94-
}
84+
@Override
85+
public String getServiceName() {
86+
return refUrl.getServiceInterface();
87+
}
9588

96-
@Override
97-
public void setMethodRequest(MethodRequest methodRequest) {
98-
this.methodRequest = methodRequest;
99-
}
89+
@Override
90+
public MethodRequest getMethodRequest() {
91+
return methodRequest;
92+
}
10093

101-
@Override
102-
public GrpcURL getRefUrl() {
103-
Object arg = this.methodRequest.getArg();
104-
return this.refUrl.addParameter(Constants.METHOD_KEY, this.methodRequest.getMethodName())//
105-
.addParameterAndEncoded(Constants.ARG_KEY, new Gson().toJson(arg));
106-
}
94+
@Override
95+
public void setMethodRequest(MethodRequest methodRequest) {
96+
this.methodRequest = methodRequest;
97+
}
10798

99+
@Override
100+
public GrpcURL getRefUrl() {
101+
Object arg = this.methodRequest.getArg();
102+
return this.refUrl.addParameter(Constants.METHOD_KEY, this.methodRequest.getMethodName())//
103+
.addParameterAndEncoded(Constants.ARG_KEY, new Gson().toJson(arg));
108104
}
109105

110-
public static class MethodRequest implements Serializable {
106+
}
111107

112-
private static final long serialVersionUID = 5280935790994972153L;
108+
public static class MethodRequest implements Serializable {
113109

114-
private final String methodName;
110+
private static final long serialVersionUID = 5280935790994972153L;
115111

116-
private final Class<?> requestType;
112+
private final String methodName;
117113

118-
private final Class<?> responseType;
114+
private final Class<?> requestType;
119115

120-
private final Object arg;
116+
private final Class<?> responseType;
121117

122-
private final int callType;
118+
private final Object arg;
123119

124-
private final int callTimeout;
120+
private final int callType;
125121

126-
public MethodRequest(String methodName, Class<?> requestType, Class<?> responseType, Object arg, int callType,
127-
int callTimeout){
128-
super();
129-
this.methodName = methodName;
130-
this.requestType = requestType;
131-
this.responseType = responseType;
132-
this.arg = arg;
133-
this.callType = callType;
134-
this.callTimeout = callTimeout;
135-
}
122+
private final int callTimeout;
136123

137-
public String getMethodName() {
138-
return methodName;
139-
}
124+
public MethodRequest(String methodName, Class<?> requestType, Class<?> responseType, Object arg,
125+
int callType, int callTimeout) {
126+
super();
127+
this.methodName = methodName;
128+
this.requestType = requestType;
129+
this.responseType = responseType;
130+
this.arg = arg;
131+
this.callType = callType;
132+
this.callTimeout = callTimeout;
133+
}
140134

141-
public Class<?> getRequestType() {
142-
return requestType;
143-
}
135+
public String getMethodName() {
136+
return methodName;
137+
}
144138

145-
public Class<?> getResponseType() {
146-
return responseType;
147-
}
139+
public Class<?> getRequestType() {
140+
return requestType;
141+
}
148142

149-
public Object getArg() {
150-
return arg;
151-
}
143+
public Class<?> getResponseType() {
144+
return responseType;
145+
}
152146

153-
public int getCallType() {
154-
return callType;
155-
}
147+
public Object getArg() {
148+
return arg;
149+
}
156150

157-
public int getCallTimeout() {
158-
return callTimeout;
159-
}
151+
public int getCallType() {
152+
return callType;
153+
}
160154

155+
public int getCallTimeout() {
156+
return callTimeout;
161157
}
158+
159+
}
162160
}

saluki-core/src/main/java/com/quancheng/saluki/core/grpc/client/GrpcResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.io.Serializable;
1111

1212
import com.google.protobuf.Message;
13-
import com.quancheng.saluki.core.grpc.util.SerializerUtils;
13+
import com.quancheng.saluki.core.grpc.util.SerializerUtil;
1414
import com.quancheng.saluki.serializer.exception.ProtobufException;
1515

1616
/**
@@ -25,7 +25,7 @@ public static class Default implements GrpcResponse, Serializable {
2525

2626
@Override
2727
public Object getResponseArg() throws ProtobufException {
28-
return SerializerUtils.Protobuf2Pojo(this.getMessage(), this.getReturnType());
28+
return SerializerUtil.protobuf2Pojo(this.getMessage(), this.getReturnType());
2929
}
3030

3131
private static final long serialVersionUID = 1L;

saluki-core/src/main/java/com/quancheng/saluki/core/grpc/client/hystrix/GrpcHystrixCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import com.quancheng.saluki.core.grpc.exception.RpcFrameworkException;
3535
import com.quancheng.saluki.core.grpc.service.ClientServerMonitor;
3636
import com.quancheng.saluki.core.grpc.service.MonitorService;
37-
import com.quancheng.saluki.core.grpc.util.MethodDescriptorUtil;
37+
import com.quancheng.saluki.core.grpc.util.GrpcUtil;
3838
import com.quancheng.saluki.serializer.exception.ProtobufException;
3939

4040
import io.grpc.MethodDescriptor;
@@ -110,7 +110,7 @@ protected Object run() throws Exception {
110110
@Override
111111
protected Object getFallback() {
112112
Class<?> responseType = this.request.getMethodRequest().getResponseType();
113-
Message response = MethodDescriptorUtil.buildDefaultInstance(responseType);
113+
Message response = GrpcUtil.createDefaultInstance(responseType);
114114
Object obj = this.transformMessage(response);
115115
collect(serviceName, methodName, getRequestMessage(), response, true);
116116
return obj;

saluki-core/src/main/java/com/quancheng/saluki/core/grpc/interceptor/HeaderClientInterceptor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import org.slf4j.LoggerFactory;
1313

1414
import com.quancheng.saluki.core.common.RpcContext;
15-
import com.quancheng.saluki.core.grpc.util.MetadataUtil;
16-
import com.quancheng.saluki.core.grpc.util.SerializerUtils;
15+
import com.quancheng.saluki.core.grpc.util.GrpcUtil;
16+
import com.quancheng.saluki.core.grpc.util.SerializerUtil;
1717

1818
import io.grpc.CallOptions;
1919
import io.grpc.Channel;
@@ -65,10 +65,10 @@ private void copyThreadLocalToMetadata(Metadata headers) {
6565
Map<String, Object> values = RpcContext.getContext().get();
6666
try {
6767
if (!attachments.isEmpty()) {
68-
headers.put(MetadataUtil.GRPC_CONTEXT_ATTACHMENTS, SerializerUtils.toJson(attachments));
68+
headers.put(GrpcUtil.GRPC_CONTEXT_ATTACHMENTS, SerializerUtil.toJson(attachments));
6969
}
7070
if (!values.isEmpty()) {
71-
headers.put(MetadataUtil.GRPC_CONTEXT_VALUES, SerializerUtils.toJson(values));
71+
headers.put(GrpcUtil.GRPC_CONTEXT_VALUES, SerializerUtil.toJson(values));
7272
}
7373
} catch (Throwable e) {
7474
log.error(e.getMessage(), e);

saluki-core/src/main/java/com/quancheng/saluki/core/grpc/interceptor/HeaderServerInterceptor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import com.google.gson.reflect.TypeToken;
1616
import com.quancheng.saluki.core.common.Constants;
1717
import com.quancheng.saluki.core.common.RpcContext;
18-
import com.quancheng.saluki.core.grpc.util.MetadataUtil;
19-
import com.quancheng.saluki.core.grpc.util.SerializerUtils;
18+
import com.quancheng.saluki.core.grpc.util.GrpcUtil;
19+
import com.quancheng.saluki.core.grpc.util.SerializerUtil;
2020

2121
import io.grpc.ForwardingServerCall.SimpleForwardingServerCall;
2222
import io.grpc.Grpc;
@@ -62,17 +62,17 @@ public void request(int numMessages) {
6262
}
6363

6464
private void copyMetadataToThreadLocal(Metadata headers) {
65-
String attachments = headers.get(MetadataUtil.GRPC_CONTEXT_ATTACHMENTS);
66-
String values = headers.get(MetadataUtil.GRPC_CONTEXT_VALUES);
65+
String attachments = headers.get(GrpcUtil.GRPC_CONTEXT_ATTACHMENTS);
66+
String values = headers.get(GrpcUtil.GRPC_CONTEXT_VALUES);
6767
try {
6868
if (attachments != null) {
69-
Map<String, String> attachmentsMap = SerializerUtils.fromJson(attachments,
69+
Map<String, String> attachmentsMap = SerializerUtil.fromJson(attachments,
7070
new TypeToken<Map<String, String>>() {}.getType());
7171
RpcContext.getContext().setAttachments(attachmentsMap);
7272
}
7373
if (values != null) {
7474
Map<String, Object> valuesMap =
75-
SerializerUtils.fromJson(values, new TypeToken<Map<String, Object>>() {}.getType());
75+
SerializerUtil.fromJson(values, new TypeToken<Map<String, Object>>() {}.getType());
7676
for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
7777
RpcContext.getContext().set(entry.getKey(), entry.getValue());
7878
}

saluki-core/src/main/java/com/quancheng/saluki/core/grpc/server/internal/DefaultProxyExporter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.quancheng.saluki.core.grpc.server.GrpcProtocolExporter;
2121
import com.quancheng.saluki.core.grpc.service.ClientServerMonitor;
2222
import com.quancheng.saluki.core.grpc.service.MonitorService;
23-
import com.quancheng.saluki.core.grpc.util.MethodDescriptorUtil;
23+
import com.quancheng.saluki.core.grpc.util.GrpcUtil;
2424
import com.quancheng.saluki.core.utils.ReflectUtils;
2525

2626
import io.grpc.MethodDescriptor;
@@ -60,7 +60,7 @@ public ServerServiceDefinition export(Class<?> protocol, Object protocolImpl) {
6060
final ConcurrentMap<String, AtomicInteger> concurrents = Maps.newConcurrentMap();
6161
for (Method method : methods) {
6262
MethodDescriptor<Message, Message> methodDescriptor =
63-
MethodDescriptorUtil.createMethodDescriptor(serivce, method);
63+
GrpcUtil.createMethodDescriptor(serivce, method);
6464
serviceDefBuilder.addMethod(methodDescriptor, ServerCalls.asyncUnaryCall(
6565
new ServerInvocation(serviceRef, method, providerUrl, concurrents, clientServerMonitor)));
6666
}

saluki-core/src/main/java/com/quancheng/saluki/core/grpc/server/internal/ServerInvocation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.quancheng.saluki.core.common.GrpcURL;
2020
import com.quancheng.saluki.core.common.RpcContext;
2121
import com.quancheng.saluki.core.grpc.service.MonitorService;
22-
import com.quancheng.saluki.core.grpc.util.SerializerUtils;
22+
import com.quancheng.saluki.core.grpc.util.SerializerUtil;
2323
import com.quancheng.saluki.core.utils.ReflectUtils;
2424

2525
import io.grpc.Status;
@@ -63,10 +63,10 @@ public void invoke(Message request, StreamObserver<Message> responseObserver) {
6363
try {
6464
getConcurrent().getAndIncrement();
6565
Class<?> requestType = ReflectUtils.getTypedOfReq(method);
66-
Object reqPojo = SerializerUtils.Protobuf2Pojo(reqProtoBufer, requestType);
66+
Object reqPojo = SerializerUtil.protobuf2Pojo(reqProtoBufer, requestType);
6767
Object[] requestParams = new Object[] { reqPojo };
6868
Object respPojo = method.invoke(serviceToInvoke, requestParams);
69-
respProtoBufer = SerializerUtils.Pojo2Protobuf(respPojo);
69+
respProtoBufer = SerializerUtil.pojo2Protobuf(respPojo);
7070
collect(reqProtoBufer, respProtoBufer, start, false);
7171
responseObserver.onNext(respProtoBufer);
7272
responseObserver.onCompleted();

0 commit comments

Comments
 (0)