修复限流返回fail信息时的解析失败问题
This commit is contained in:
parent
d00cd159d3
commit
cd4a0fb4ed
@ -7,9 +7,7 @@
|
||||
|
||||
1.先安装并开启zookeeper
|
||||
|
||||
[windows 环境下zookeeper的安装与配置]: https://blog.csdn.net/fisherish/article/details/118974827?spm=1001.2014.3001.5506
|
||||
|
||||
|
||||
安装参考:https://blog.csdn.net/fisherish/article/details/118974827?spm=1001.2014.3001.5506
|
||||
|
||||
2.运行Server包下的TestServer,再运行Client包下的TestClient
|
||||
|
||||
|
||||
@ -16,12 +16,27 @@ public class TestClient {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ClientProxy clientProxy=new ClientProxy();
|
||||
UserService proxy=clientProxy.getProxy(UserService.class);
|
||||
for(int i = 0; i < 15; i++) {
|
||||
Integer i1 = i;
|
||||
new Thread(()->{
|
||||
try{
|
||||
User user = proxy.getUserByUserId(i1);
|
||||
|
||||
User user = proxy.getUserByUserId(1);
|
||||
System.out.println("从服务端得到的user="+user.toString());
|
||||
|
||||
User u=User.builder().id(100).userName("wxx").sex(true).build();
|
||||
Integer id = proxy.insertUserId(u);
|
||||
Integer id = proxy.insertUserId(User.builder().id(i1).userName("User" + i1.toString()).sex(true).build());
|
||||
System.out.println("向服务端插入user的id"+id);
|
||||
} catch (NullPointerException e){
|
||||
System.out.println("user为空");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
//User user = proxy.getUserByUserId(1);
|
||||
//System.out.println("从服务端得到的user="+user.toString());
|
||||
//
|
||||
//User u=User.builder().id(100).userName("wxx").sex(true).build();
|
||||
//Integer id = proxy.insertUserId(u);
|
||||
//System.out.println("向服务端插入user的id"+id);
|
||||
}
|
||||
|
||||
@ -39,6 +39,7 @@ public class NettyRPCServerHandler extends SimpleChannelInboundHandler<RpcReques
|
||||
RateLimit rateLimit=serviceProvider.getRateLimitProvider().getRateLimit(interfaceName);
|
||||
if(!rateLimit.getToken()){
|
||||
//如果获取令牌失败,进行限流降级,快速返回结果
|
||||
System.out.println("服务限流!!");
|
||||
return RpcResponse.fail();
|
||||
}
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import part1.common.Message.MessageType;
|
||||
import part1.common.Message.RpcResponse;
|
||||
import part1.common.serializer.mySerializer.Serializer;
|
||||
|
||||
import java.util.List;
|
||||
@ -35,7 +36,9 @@ public class MyDecoder extends ByteToMessageDecoder {
|
||||
//4.读取序列化数组
|
||||
byte[] bytes=new byte[length];
|
||||
in.readBytes(bytes);
|
||||
System.out.println("bytes==="+new String(bytes));
|
||||
Object deserialize= serializer.deserialize(bytes, messageType);
|
||||
|
||||
out.add(deserialize);
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,6 +43,11 @@ public class JsonSerializer implements Serializer {
|
||||
break;
|
||||
case 1:
|
||||
RpcResponse response = JSON.parseObject(bytes, RpcResponse.class);
|
||||
// 如果类型为空,说明返回错误
|
||||
if(response.getDataType()==null){
|
||||
obj = RpcResponse.fail();
|
||||
break;
|
||||
}
|
||||
Class<?> dataType = response.getDataType();
|
||||
//判断转化后的response对象中的data的类型是否正确
|
||||
if(!dataType.isAssignableFrom(response.getData().getClass())){
|
||||
|
||||
Loading…
Reference in New Issue
Block a user