服务端sdk 优化日志打印,线程名加入UID信息

This commit is contained in:
远方 2022-04-04 14:01:10 +08:00
parent 6c61a09682
commit be301d95c9
7 changed files with 43 additions and 119 deletions

View File

@ -4,7 +4,7 @@ spring.profiles.active=dev
################################################################## ##################################################################
# JDBC Config # # JDBC Config #
################################################################## ##################################################################
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/cim?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC spring.datasource.url = jdbc:mysql://yinxindatabase.mysql.rds.aliyuncs.com:3306/cim?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username = cim spring.datasource.username = cim
spring.datasource.password = cimv587! spring.datasource.password = cimv587!
spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.type=com.zaxxer.hikari.HikariDataSource
@ -20,6 +20,7 @@ spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1 spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.validation-timeout=600000 spring.datasource.hikari.validation-timeout=600000
################################################################## ##################################################################
# JPA Config # # JPA Config #
################################################################## ##################################################################
@ -33,9 +34,10 @@ spring.jpa.hibernate.naming.physical-strategy= org.hibernate.boot.model.naming.P
################################################################## ##################################################################
# Redis Config # # Redis Config #
################################################################## ##################################################################
spring.redis.host=127.0.0.1 spring.redis.host=114.215.199.17
spring.redis.port=6379 spring.redis.port=6379
spring.redis.database=12 spring.redis.database=12
spring.redis.password=redisv587
spring.redis.lettuce.pool.max-active=10 spring.redis.lettuce.pool.max-active=10
spring.redis.lettuce.pool.max-wait= 10s spring.redis.lettuce.pool.max-wait= 10s
spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.max-idle=5

View File

@ -59,7 +59,6 @@ public class AppSocketAcceptor extends NioSocketAcceptor {
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch){ public void initChannel(SocketChannel ch){
ch.pipeline().addLast(threadNamingHandler);
ch.pipeline().addLast(new AppMessageDecoder()); ch.pipeline().addLast(new AppMessageDecoder());
ch.pipeline().addLast(new AppMessageEncoder()); ch.pipeline().addLast(new AppMessageEncoder());
ch.pipeline().addLast(loggingHandler); ch.pipeline().addLast(loggingHandler);

View File

@ -25,7 +25,6 @@ import com.farsunset.cim.constant.CIMConstant;
import com.farsunset.cim.constant.ChannelAttr; import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.handler.CIMRequestHandler; import com.farsunset.cim.handler.CIMRequestHandler;
import com.farsunset.cim.handler.LoggingHandler; import com.farsunset.cim.handler.LoggingHandler;
import com.farsunset.cim.handler.ThreadNamingHandler;
import com.farsunset.cim.model.Ping; import com.farsunset.cim.model.Ping;
import com.farsunset.cim.model.SentBody; import com.farsunset.cim.model.SentBody;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
@ -49,7 +48,6 @@ abstract class NioSocketAcceptor extends SimpleChannelInboundHandler<SentBody>{
protected final Logger logger = LoggerFactory.getLogger(getClass()); protected final Logger logger = LoggerFactory.getLogger(getClass());
protected final ChannelHandler loggingHandler = new LoggingHandler(); protected final ChannelHandler loggingHandler = new LoggingHandler();
protected final ChannelHandler threadNamingHandler = new ThreadNamingHandler();
private final EventLoopGroup bossGroup; private final EventLoopGroup bossGroup;
private final EventLoopGroup workerGroup; private final EventLoopGroup workerGroup;

View File

@ -90,7 +90,6 @@ public class WebsocketAcceptor extends NioSocketAcceptor {
@Override @Override
public void initChannel(SocketChannel ch){ public void initChannel(SocketChannel ch){
ch.pipeline().addLast(threadNamingHandler);
ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast(new HttpServerCodec());
ch.pipeline().addLast(new ChunkedWriteHandler()); ch.pipeline().addLast(new ChunkedWriteHandler());
ch.pipeline().addLast(new HttpObjectAggregator(4 * 1024)); ch.pipeline().addLast(new HttpObjectAggregator(4 * 1024));
@ -107,7 +106,6 @@ public class WebsocketAcceptor extends NioSocketAcceptor {
ch.pipeline().addLast(loggingHandler); ch.pipeline().addLast(loggingHandler);
ch.pipeline().addLast(WebsocketAcceptor.this); ch.pipeline().addLast(WebsocketAcceptor.this);
ch.pipeline().addLast(illegalRequestHandler); ch.pipeline().addLast(illegalRequestHandler);
} }
}); });

View File

@ -22,6 +22,7 @@
package com.farsunset.cim.handler; package com.farsunset.cim.handler;
import com.farsunset.cim.constant.ChannelAttr;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelPromise;
@ -36,14 +37,43 @@ public class LoggingHandler extends io.netty.handler.logging.LoggingHandler {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.channelRead(ctx,msg); super.channelRead(ctx,msg);
Thread.currentThread().setName(name);
} }
@Override @Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
super.channelRead(ctx,msg); String name = Thread.currentThread().getName();
setThreadName(ctx);
super.write(ctx,msg,promise);
Thread.currentThread().setName(name);
} }
@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.close(ctx,promise);
Thread.currentThread().setName(name);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.channelInactive(ctx);
Thread.currentThread().setName(name);
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.userEventTriggered(ctx,evt);
Thread.currentThread().setName(name);
}
@Override @Override
public void channelRegistered(ChannelHandlerContext ctx) { public void channelRegistered(ChannelHandlerContext ctx) {
@ -74,4 +104,12 @@ public class LoggingHandler extends io.netty.handler.logging.LoggingHandler {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.warn("EXCEPTION",cause); logger.warn("EXCEPTION",cause);
} }
private void setThreadName(ChannelHandlerContext context){
String uid = context.channel().attr(ChannelAttr.UID).get();
if (uid != null){
Thread.currentThread().setName("nio-uid-" + uid);
}
}
} }

View File

@ -1,111 +0,0 @@
/*
* Copyright 2013-2019 Xia Jun(3979434@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************************
* *
* Website : http://www.farsunset.com *
* *
***************************************************************************************
*/
package com.farsunset.cim.handler;
import com.farsunset.cim.constant.ChannelAttr;
import io.netty.channel.*;
@ChannelHandler.Sharable
public class ThreadNamingHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.channelRegistered(ctx);
Thread.currentThread().setName(name);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.channelUnregistered(ctx);
Thread.currentThread().setName(name);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.channelActive(ctx);
Thread.currentThread().setName(name);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.channelInactive(ctx);
Thread.currentThread().setName(name);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.channelRead(ctx,msg);
Thread.currentThread().setName(name);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.channelReadComplete(ctx);
Thread.currentThread().setName(name);
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.userEventTriggered(ctx,evt);
Thread.currentThread().setName(name);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.channelWritabilityChanged(ctx);
Thread.currentThread().setName(name);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
String name = Thread.currentThread().getName();
setThreadName(ctx);
super.exceptionCaught(ctx,cause);
Thread.currentThread().setName(name);
}
private void setThreadName(ChannelHandlerContext context){
String uid = context.channel().attr(ChannelAttr.UID).get();
if (uid != null){
Thread.currentThread().setName("nio-uid-" + uid);
}
}
}