亚马逊网站建设资料,网站如何做中英文效果,seo优化入门教程,WordPress对象存储插件文章目录 attach JVMagent**ArthasBootstrap** arthas-core的启动可以从上一篇做参考 参考 pom#xff0c;即启动是调用的 Arthas 的 main 方法 attach JVM
JVM提供了 Java Attach 功能#xff0c;能够让客户端与目标JVM进行通讯从而获取JVM运行时的数据#xff0c;甚至可以… 文章目录 attach JVMagent**ArthasBootstrap** arthas-core的启动可以从上一篇做参考 参考 pom即启动是调用的 Arthas 的 main 方法 attach JVM
JVM提供了 Java Attach 功能能够让客户端与目标JVM进行通讯从而获取JVM运行时的数据甚至可以通过Java Attach 加载自定义的代理工具实现AOP、运行时class热更新等功能。
private void attachAgent(Configure configure) throws Exception {VirtualMachineDescriptor virtualMachineDescriptor null;for (VirtualMachineDescriptor descriptor : VirtualMachine.list()) {String pid descriptor.id();if (pid.equals(Long.toString(configure.getJavaPid()))) {virtualMachineDescriptor descriptor;break;}}VirtualMachine virtualMachine null;try {if (null virtualMachineDescriptor) { // 使用 attach(String pid) 这种方式virtualMachine VirtualMachine.attach( configure.getJavaPid());} else {virtualMachine VirtualMachine.attach(virtualMachineDescriptor);}……try {virtualMachine.loadAgent(arthasAgentPath,configure.getArthasCore() ; configure.toString());} catch (IOException e) {}VirtualMachine.list() 相当于 jps 的功能 attach是依靠这条代码 virtualMachine VirtualMachine.attach(virtualMachineDescriptor); 之后就是 loadAgent 并传入 core 和 configure 参数。
agent
agent 的代码很简单只有2个类
核心就是 AgentBootstrap 对于 javaagent来说核心的启动入口 public static void premain(String args, Instrumentation inst) {main(args, inst);}public static void agentmain(String args, Instrumentation inst) {main(args, inst);} 神奇不在 arthas-core 里传入的 -core 参数实际上是透传给agent的。整的很绕。 通过 bind 方法进行线程绑定这一步和 arthas-boot 很像。
private static synchronized void main(String args, final Instrumentation inst) {// 尝试判断arthas是否已在运行如果是的话直接就退出try {Class.forName(java.arthas.SpyAPI); // 加载不到会抛异常if (SpyAPI.isInited()) {ps.println(Arthas server already stared, skip attach.);ps.flush();return;}} catch (Throwable e) {// ignore}try {ps.println(Arthas server agent start...);// 传递的args参数分两个部分:arthasCoreJar路径和agentArgs, 分别是Agent的JAR包路径和期望传递到服务端的参数if (args null) {args ;}args decodeArg(args);String arthasCoreJar;final String agentArgs;int index args.indexOf(;);if (index ! -1) {arthasCoreJar args.substring(0, index);agentArgs args.substring(index);} else {arthasCoreJar ;agentArgs args;}File arthasCoreJarFile new File(arthasCoreJar);if (!arthasCoreJarFile.exists()) {ps.println(Can not find arthas-core jar file from args: arthasCoreJarFile);// try to find from arthas-agent.jar directoryCodeSource codeSource AgentBootstrap.class.getProtectionDomain().getCodeSource();if (codeSource ! null) {try {File arthasAgentJarFile new File(codeSource.getLocation().toURI().getSchemeSpecificPart());arthasCoreJarFile new File(arthasAgentJarFile.getParentFile(), ARTHAS_CORE_JAR);if (!arthasCoreJarFile.exists()) {ps.println(Can not find arthas-core jar file from agent jar directory: arthasAgentJarFile);}} catch (Throwable e) {ps.println(Can not find arthas-core jar file from codeSource.getLocation());e.printStackTrace(ps);}}}if (!arthasCoreJarFile.exists()) {return;}/*** Use a dedicated thread to run the binding logic to prevent possible memory leak. #195*/final ClassLoader agentLoader getClassLoader(inst, arthasCoreJarFile);Thread bindingThread new Thread() {Overridepublic void run() {try {bind(inst, agentLoader, agentArgs);} catch (Throwable throwable) {throwable.printStackTrace(ps);}}};bindingThread.setName(arthas-binding-thread);bindingThread.start();bindingThread.join();} catch (Throwable t) {t.printStackTrace(ps);try {if (ps ! System.err) {ps.close();}} catch (Throwable tt) {// ignore}throw new RuntimeException(t);}}ArthasBootstrap
bind 有调用了 core 里面 ArthasBootstrap.getInstance
private static void bind(Instrumentation inst, ClassLoader agentLoader, String args) throws Throwable {/*** pre* ArthasBootstrap bootstrap ArthasBootstrap.getInstance(inst);* /pre*/Class? bootstrapClass agentLoader.loadClass(ARTHAS_BOOTSTRAP);Object bootstrap bootstrapClass.getMethod(GET_INSTANCE, Instrumentation.class, String.class).invoke(null, inst, args);boolean isBind (Boolean) bootstrapClass.getMethod(IS_BIND).invoke(bootstrap);if (!isBind) {String errorMsg Arthas server port binding failed! Please check $HOME/logs/arthas/arthas.log for more details.;ps.println(errorMsg);throw new RuntimeException(errorMsg);}ps.println(Arthas server already bind.);}而在 ArthasBootstrap中 我们用的 terminal也就是 ShellServer 就在 6. start agent server
private ArthasBootstrap(Instrumentation instrumentation, MapString, String args) throws Throwable {this.instrumentation instrumentation;initFastjson();// 1. initSpy()initSpy();// 2. ArthasEnvironmentinitArthasEnvironment(args);String outputPathStr configure.getOutputPath();if (outputPathStr null) {outputPathStr ArthasConstants.ARTHAS_OUTPUT;}outputPath new File(outputPathStr);outputPath.mkdirs();// 3. init loggerloggerContext LogUtil.initLogger(arthasEnvironment);// 4. 增强ClassLoaderenhanceClassLoader();// 5. init beansinitBeans();// 6. start agent serverbind(configure);executorService Executors.newScheduledThreadPool(1, new ThreadFactory() {Overridepublic Thread newThread(Runnable r) {final Thread t new Thread(r, arthas-command-execute);t.setDaemon(true);return t;}});shutdown new Thread(as-shutdown-hooker) {Overridepublic void run() {ArthasBootstrap.this.destroy();}};transformerManager new TransformerManager(instrumentation);Runtime.getRuntime().addShutdownHook(shutdown);}ShellServerImpl 的 bind 这段实现中BuiltinCommandPack 完成了命名的声明并与 cli 输入内容进行命名绑定。并用 Netty 开启server。 /*** Bootstrap arthas server** param configure 配置信息* throws IOException 服务器启动失败*/private void bind(Configure configure) throws Throwable {long start System.currentTimeMillis();if (!isBindRef.compareAndSet(false, true)) {throw new IllegalStateException(already bind);}// init random portif (configure.getTelnetPort() ! null configure.getTelnetPort() 0) {int newTelnetPort SocketUtils.findAvailableTcpPort();configure.setTelnetPort(newTelnetPort);logger().info(generate random telnet port: newTelnetPort);}if (configure.getHttpPort() ! null configure.getHttpPort() 0) {int newHttpPort SocketUtils.findAvailableTcpPort();configure.setHttpPort(newHttpPort);logger().info(generate random http port: newHttpPort);}// try to find appNameif (configure.getAppName() null) {configure.setAppName(System.getProperty(ArthasConstants.PROJECT_NAME,System.getProperty(ArthasConstants.SPRING_APPLICATION_NAME, null)));}try {if (configure.getTunnelServer() ! null) {tunnelClient new TunnelClient();tunnelClient.setAppName(configure.getAppName());tunnelClient.setId(configure.getAgentId());tunnelClient.setTunnelServerUrl(configure.getTunnelServer());tunnelClient.setVersion(ArthasBanner.version());ChannelFuture channelFuture tunnelClient.start();channelFuture.await(10, TimeUnit.SECONDS);}} catch (Throwable t) {logger().error(start tunnel client error, t);}try {ShellServerOptions options new ShellServerOptions().setInstrumentation(instrumentation).setPid(PidUtils.currentLongPid()).setWelcomeMessage(ArthasBanner.welcome());if (configure.getSessionTimeout() ! null) {options.setSessionTimeout(configure.getSessionTimeout() * 1000);}this.httpSessionManager new HttpSessionManager();if (IPUtils.isAllZeroIP(configure.getIp()) StringUtils.isBlank(configure.getPassword())) {// 当 listen 0.0.0.0 时强制生成密码防止被远程连接String errorMsg Listening on 0.0.0.0 is very dangerous! External users can connect to your machine! No password is currently configured. Therefore, a default password is generated, and clients need to use the password to connect!;AnsiLog.error(errorMsg);configure.setPassword(StringUtils.randomString(64));AnsiLog.error(Generated arthas password: configure.getPassword());logger().error(errorMsg);logger().info(Generated arthas password: configure.getPassword());}this.securityAuthenticator new SecurityAuthenticatorImpl(configure.getUsername(), configure.getPassword());shellServer new ShellServerImpl(options);ListString disabledCommands new ArrayListString();if (configure.getDisabledCommands() ! null) {String[] strings StringUtils.tokenizeToStringArray(configure.getDisabledCommands(), ,);if (strings ! null) {disabledCommands.addAll(Arrays.asList(strings));}}BuiltinCommandPack builtinCommands new BuiltinCommandPack(disabledCommands);ListCommandResolver resolvers new ArrayListCommandResolver();resolvers.add(builtinCommands);//worker groupworkerGroup new NioEventLoopGroup(new DefaultThreadFactory(arthas-TermServer, true));// TODO: discover user provided command resolverif (configure.getTelnetPort() ! null configure.getTelnetPort() 0) {logger().info(try to bind telnet server, host: {}, port: {}., configure.getIp(), configure.getTelnetPort());shellServer.registerTermServer(new HttpTelnetTermServer(configure.getIp(), configure.getTelnetPort(),options.getConnectionTimeout(), workerGroup, httpSessionManager));} else {logger().info(telnet port is {}, skip bind telnet server., configure.getTelnetPort());}if (configure.getHttpPort() ! null configure.getHttpPort() 0) {logger().info(try to bind http server, host: {}, port: {}., configure.getIp(), configure.getHttpPort());shellServer.registerTermServer(new HttpTermServer(configure.getIp(), configure.getHttpPort(),options.getConnectionTimeout(), workerGroup, httpSessionManager));} else {// listen local address in VM communicationif (configure.getTunnelServer() ! null) {shellServer.registerTermServer(new HttpTermServer(configure.getIp(), configure.getHttpPort(),options.getConnectionTimeout(), workerGroup, httpSessionManager));}logger().info(http port is {}, skip bind http server., configure.getHttpPort());}for (CommandResolver resolver : resolvers) {shellServer.registerCommandResolver(resolver);}shellServer.listen(new BindHandler(isBindRef));if (!isBind()) {throw new IllegalStateException(Arthas failed to bind telnet or http port! Telnet port: String.valueOf(configure.getTelnetPort()) , http port: String.valueOf(configure.getHttpPort()));}//http api session managersessionManager new SessionManagerImpl(options, shellServer.getCommandManager(), shellServer.getJobController());//http api handlerhttpApiHandler new HttpApiHandler(historyManager, sessionManager);logger().info(as-server listening on network{};telnet{};http{};timeout{};, configure.getIp(),configure.getTelnetPort(), configure.getHttpPort(), options.getConnectionTimeout());// 异步回报启动次数if (configure.getStatUrl() ! null) {logger().info(arthas stat url: {}, configure.getStatUrl());}UserStatUtil.setStatUrl(configure.getStatUrl());UserStatUtil.setAgentId(configure.getAgentId());UserStatUtil.arthasStart();try {SpyAPI.init();} catch (Throwable e) {// ignore}logger().info(as-server started in {} ms, System.currentTimeMillis() - start);} catch (Throwable e) {logger().error(Error during start as-server, e);destroy();throw e;}}参考
https://blog.csdn.net/tianjindong0804/article/details/128423819 文章转载自: http://www.morning.wktbz.cn.gov.cn.wktbz.cn http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.ysgnb.cn.gov.cn.ysgnb.cn http://www.morning.gxklx.cn.gov.cn.gxklx.cn http://www.morning.yrmgh.cn.gov.cn.yrmgh.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.cknsx.cn.gov.cn.cknsx.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.sskns.cn.gov.cn.sskns.cn http://www.morning.gkmwx.cn.gov.cn.gkmwx.cn http://www.morning.rkhhl.cn.gov.cn.rkhhl.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.jqllx.cn.gov.cn.jqllx.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.jppdk.cn.gov.cn.jppdk.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.ykbgs.cn.gov.cn.ykbgs.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.nkrmh.cn.gov.cn.nkrmh.cn http://www.morning.bpmnl.cn.gov.cn.bpmnl.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.ngcth.cn.gov.cn.ngcth.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.skql.cn.gov.cn.skql.cn http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.lxhny.cn.gov.cn.lxhny.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn