Skip to content

Commit

Permalink
增加基于tomcat的应用的流量优雅上下线功能
Browse files Browse the repository at this point in the history
  • Loading branch information
4fool committed Jul 17, 2023
1 parent 1a1a727 commit f8d71af
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void initCommands(List<String> disabledCommands) {
commandClassList.add(BlockedThreadCommand.class);
commandClassList.add(BigKeyCommand.class);

commandClassList.add(OffLineCommand.class);
//commandClassList.add(OffLineCommand.class);
commandClassList.add(KeepAliveCommand.class);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ public KeepAliveAdviceListener(KeepAliveCommand command, CommandProcess process,
@Override
public void before(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target, Object[] args)
throws Throwable {
Class<?> cls = target.getClass();
try {
Class cls = target.getClass();
Method toString = method(cls,"toString");
String str = (String)toString.invoke(target);
if(str.contains(command.host)){
Field keepAliveLeft = field(cls,"keepAliveLeft");
keepAliveLeft.setAccessible(true);
int left = (int)keepAliveLeft.get(target);
Method toString = cls.getDeclaredMethod("toString");
String string = (String) toString.invoke(target);

Method setKeepAliveLeft = method(cls,"setKeepAliveLeft",int.class);
setKeepAliveLeft.invoke(target,command.getLeft());
Field keepAliveLeft = cls.getDeclaredField("keepAliveLeft");
keepAliveLeft.setAccessible(true);
int left = (int) keepAliveLeft.get(target);

int afterLeft = (int)keepAliveLeft.get(target);
logger.info("keepAliveLeft value before {} after {} , socket {}", left, afterLeft, str);
}
Method setKeepAliveLeft = cls.getDeclaredMethod("setKeepAliveLeft",int.class);
setKeepAliveLeft.invoke(target, 1);

int afterLeft = (int) keepAliveLeft.get(target);
logger.info("keepAliveLeft value before {} after {} , socket {}", left, afterLeft, string);
}catch (Throwable t){
logger.error("{} {}",args[0],args[1],t);
}
Expand All @@ -46,46 +45,12 @@ public void before(ClassLoader loader, Class<?> clazz, ArthasMethod method, Obje
@Override
public void afterReturning(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target, Object[] args,
Object returnObject) throws Throwable {
try {
Class cls = target.getClass();
Method toString = method(cls,"toString");
String str = (String)toString.invoke(target);
if(str.contains(command.host)) {
Field keepAliveLeft = field(cls, "keepAliveLeft");
keepAliveLeft.setAccessible(true);
int left = (int) keepAliveLeft.get(target);
logger.info("keepAliveLeft value {} , socket {}", left, str);
}
}catch (Throwable t){
logger.error("{} {}",args[0],args[1],t);
}

}

@Override
public void afterThrowing(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target, Object[] args,
Throwable throwable) {
logger.info("{} {} {} {}",clazz.getName(),method.getName(),target,args.length);
}

private static Method method(Class<?> clazz, String methodName,Class<?>... parameterTypes){
if(clazz == null){
return null;
}
try{
return clazz.getDeclaredMethod(methodName,parameterTypes);
} catch (NoSuchMethodException e) {
return method(clazz.getSuperclass(),methodName);
}
}
private static Field field(Class<?> clazz, String fieldName){
if(clazz == null){
return null;
}
try{
return clazz.getDeclaredField(fieldName);
}catch (NoSuchFieldException exception){
return field(clazz.getSuperclass(),fieldName);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.taobao.arthas.core.command.monitor200.tomcat;

import com.alibaba.arthas.deps.org.slf4j.Logger;
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
import com.taobao.arthas.core.GlobalOptions;
import com.taobao.arthas.core.advisor.AdviceListener;
import com.taobao.arthas.core.command.Constants;
Expand All @@ -13,44 +11,23 @@
import com.taobao.arthas.core.util.matcher.Matcher;
import com.taobao.middleware.cli.annotations.Description;
import com.taobao.middleware.cli.annotations.Name;
import com.taobao.middleware.cli.annotations.Option;
import com.taobao.middleware.cli.annotations.Summary;

import java.util.Arrays;

@Name("keepalive")
@Summary("keepalive http connection for cxf")
@Description(Constants.EXPRESS_DESCRIPTION + "\nExamples:\n" +
" keepalive -host 127.0.0.1 -left 6\n" +
" keepalive\n" +
Constants.WIKI + Constants.WIKI_HOME + "keepalive")
public class KeepAliveCommand extends EnhancerCommand {
private static final Logger logger = LoggerFactory.getLogger(KeepAliveCommand.class);
private static String className;
private static String methodName;
static {
className = "org.apache.tomcat.util.net.SocketWrapper";
methodName = "decrementKeepAlive";
}

protected String host = "hello world";
@Option(shortName = "host", longName = "host")
@Description("host ip address")
public void setHost(String host) {
this.host = host;
logger.info("{}", host);
}

private int left;
@Option(shortName = "left", longName = "left")
@Description("keepalive left")
public void setCount(int left) {
this.left = left;
}

public int getLeft() {
return left;
}

@Override
protected Matcher getClassNameMatcher() {
if (classNameMatcher == null) {
Expand Down

0 comments on commit f8d71af

Please sign in to comment.