Skip to content

Commit

Permalink
1.2.3版本, 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
naivetoby committed Jun 25, 2020
1 parent 0431996 commit f6591ce
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/main/java/vip/toby/rpc/client/RpcClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class RpcClientProxy<T> implements InvocationHandler {
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
public Object invoke(Object proxy, Method method, Object[] args) {
// 获取方法注解
RpcClientMethod rpcClientMethod = method.getAnnotation(RpcClientMethod.class);
if (rpcClientMethod == null) {
Expand Down Expand Up @@ -73,7 +73,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
LOGGER.warn(this.rpcType.getName() + "-RpcClient-" + this.rpcName + ", Method: " + methodName + ", @ParamData的值不是JSONObject类型, 已忽略");
}
} else {
// Spring-Boot框架默认已加上-parameters编译参数
// Spring-Boot框架默认已加上-parameters编译参数
data.put(parameters[i].getName(), args[i]);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/vip/toby/rpc/client/RpcClientProxyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ private void replyBinding(String rpcName, Queue queue) {
/**
* 实例化 ReplyMessageListenerContainer
*/
private SimpleMessageListenerContainer replyMessageListenerContainer(String rpcName, Queue queue, RabbitTemplate syncSender, ConnectionFactory connectionFactory) {
private void replyMessageListenerContainer(String rpcName, Queue queue, RabbitTemplate syncSender, ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer replyMessageListenerContainer = registerBean(RpcType.SYNC.getName() + "-ReplyMessageListenerContainer-" + rpcName, SimpleMessageListenerContainer.class, connectionFactory);
replyMessageListenerContainer.setQueueNames(queue.getName());
replyMessageListenerContainer.setMessageListener(syncSender);
return replyMessageListenerContainer;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/vip/toby/rpc/entity/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public enum ErrorCode {
// 找不到
NOT_FOUND(404, "Not Found"),
// Method not allowed
METHOD_NOT_ALLOWED(405, "Method not allowed"),
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
// 服务器内部错误
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
// 服务不可用
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
// 请求超时(服务器负载过高,未能及时处理请求)
GATEWAY_TIMEOUT(504, "Gateway Timeout");

private int code;
private String message;
private final int code;
private final String message;

ErrorCode(int code, String message) {
this.code = code;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/vip/toby/rpc/entity/OperateStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public enum OperateStatus {
// 调用失败
FAILURE(0, "Failure");

private int status;
private String message;
private final int status;
private final String message;

OperateStatus(int status, String message) {
this.status = status;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/vip/toby/rpc/entity/RpcMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public enum RpcMode {
// 服务端
RPC_SERVER(1, "服务端");

private int mode;
private String name;
private final int mode;
private final String name;

RpcMode(int mode, String name) {
this.mode = mode;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/vip/toby/rpc/entity/RpcResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
public class RpcResult {

private ServerStatus serverStatus;
private ServerResult serverResult;
private final ServerStatus serverStatus;
private final ServerResult serverResult;

public RpcResult(ServerStatus serverStatus) {
this.serverStatus = serverStatus;
Expand All @@ -34,7 +34,7 @@ public ServerResult getServerResult() {
public String toString() {
JSONObject result = new JSONObject();
result.put("serverStatus", JSONObject.parse(this.serverStatus.toString()));
if (this.serverStatus != null) {
if (this.serverResult != null) {
result.put("serverResult", JSONObject.parse(this.serverResult.toString()));
}
return result.toJSONString();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/vip/toby/rpc/entity/RpcType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public enum RpcType {
// 异步
ASYNC(1, "ASYNC");

private int type;
private String name;
private final int type;
private final String name;

RpcType(int type, String name) {
this.type = type;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/vip/toby/rpc/entity/ServerResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class ServerResult {

private OperateStatus operateStatus;
private final OperateStatus operateStatus;
private String message;
private Object result;
private int errorCode;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/vip/toby/rpc/entity/ServerStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public enum ServerStatus {
// 调用超时, 服务不可用
UNAVAILABLE(-2, "Service Unavailable");

private int status;
private String message;
private final int status;
private final String message;

ServerStatus(int status, String message) {
this.status = status;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/vip/toby/rpc/server/RpcServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void onMessage(Message message, Channel channel) throws IOException {
// 异步执行任务
if (RpcType.ASYNC == this.rpcType) {
long start = System.currentTimeMillis();
asyncExecute(paramData, command, data);
asyncExecute(command, data);
double offset = System.currentTimeMillis() - start;
LOGGER.info("Duration: " + offset + "ms, " + this.rpcType.getName() + "-RpcServer-" + this.rpcName + ", Method: " + command + ", Received: " + messageStr);
if (offset > this.slowCallTime) {
Expand All @@ -124,7 +124,7 @@ public void onMessage(Message message, Channel channel) throws IOException {
}
// 同步执行任务并返回结果
long start = System.currentTimeMillis();
JSONObject resultData = syncExecute(paramData, command, data);
JSONObject resultData = syncExecute(command, data);
if (resultData != null) {
double offset = System.currentTimeMillis() - start;
LOGGER.info("Duration: " + offset + "ms, " + this.rpcType.getName() + "-RpcServer-" + this.rpcName + ", Method: " + command + ", Received: " + messageStr);
Expand Down Expand Up @@ -167,7 +167,7 @@ public void onMessage(Message message, Channel channel) throws IOException {
/**
* 同步调用
*/
private void asyncExecute(JSONObject paramData, String command, JSONObject data) throws InvocationTargetException {
private void asyncExecute(String command, JSONObject data) throws InvocationTargetException {
// 获取当前服务的反射方法调用
String key = this.rpcType.getName() + "_" + this.rpcName + "_" + command;
// 通过缓存来优化性能
Expand All @@ -183,7 +183,7 @@ private void asyncExecute(JSONObject paramData, String command, JSONObject data)
/**
* 异步调用
*/
private JSONObject syncExecute(JSONObject paramData, String command, JSONObject data) throws InvocationTargetException {
private JSONObject syncExecute(String command, JSONObject data) throws InvocationTargetException {
// 获取当前服务的反射方法调用
String key = this.rpcType.getName() + "_" + this.rpcName + "_" + command;
// 通过缓存来优化性能
Expand Down

0 comments on commit f6591ce

Please sign in to comment.