您的位置:首页 > 文旅 > 美景 > 一篇学会Arthas的基本使用及常用指令

一篇学会Arthas的基本使用及常用指令

2024/12/23 11:17:52 来源:https://blog.csdn.net/qq_51081923/article/details/135703449  浏览:    关键词:一篇学会Arthas的基本使用及常用指令
  • 使用ognl表达式创建一个对象,并对这个对象进行操作
    • 测试代码
  • public class User {private Long uid;private String nickName;public User(Long uid, String nickName) {this.uid = uid;this.nickName = nickName;}public Long getUid() {return uid;}public void setUid(Long uid) {this.uid = uid;}public String getNickName() {return nickName;}public void setNickName(String nickName) {this.nickName = nickName;}}
    • ognl表达式
      • 创建一个对象
  • # 创建一个User对象什么也不做[arthas@27880]$ ognl 'new com.kerwin.arthas.demo.User(100,"kerwin")'                                             @User[                                                                                                                       uid=@Long[100],                                                                                                          nickName=@String[kerwin],                                                                                            ]# 创建一个User对象赋值给user,后续可以在别的地方对创建出来的对象进行操作[arthas@27880]$ ognl '#user = new com.kerwin.arthas.demo.User(100,"kerwin")'                                             @User[                                                                                                                       uid=@Long[100],                                                                                                          nickName=@String[kerwin],                                                                                            ]
      • 调用创建出来的对象中的成员方法
  • [arthas@27880]$ ognl 'new com.kerwin.arthas.demo.User(10001,"kerwin").getNickName()'                                     @String[kerwin]                                                                                          ][arthas@27880]$ ognl '#user = new com.kerwin.arthas.demo.User(10001,"kerwin").getNickName()'                                     @String[kerwin]                                                                                          ][arthas@27880]$ ognl '#user = new com.kerwin.arthas.demo.User(10001,"kerwin"),#user.getNickName()'                                     @String[kerwin]                                                                                          ]
    • 方法复杂入参ognl表达式
      • 测试代码
  • public class OgnlDemo03 {public static OgnlDemo03 ognlDemo03 = new OgnlDemo03();private User user;private static User staticUser;private static List<String> lists;private static Map<String,String> maps;public User setUser(User user){this.user = user;return user;}public static User setStaticUser(User user){OgnlDemo03.staticUser = user;return user;}public static User getMyUser(){return new User(10002L,"kerwin2");}public static User changeUser(User user){return new User(user.getUid(),user.getNickName()+"---changeUser");}public static List<String> setLists(List<String> lists){OgnlDemo03.lists = lists;return lists;}public static Map<String,String> setMaps(Map<String,String> maps){OgnlDemo03.maps = maps;return maps;}}
      • 自定义对象入参
        • 创建一个User对象将这个对象作为参数传入静态方法中
  • [arthas@24600]$ ognl '#user = new com.kerwin.arthas.demo.User(10001,"kerwin"),@com.kerwin.arthas.demo.OgnlDemo03@setStaticUser(#user)'                                                                                                             @User[                                                                                                                       uid=@Long[10001],                                                                                                        nickName=@String[kerwin],                                                                                            ]
        • 创建一个User对象,将这个对象作为参数传入静态属性ognlDemo03对象setUser(user)方法中
  • [arthas@24600]$ ognl '#user = new com.kerwin.arthas.demo.User(10001,"kerwin"),@com.kerwin.arthas.demo.OgnlDemo03@ognlDemo03.setUser(#user)'                                                                                                        @User[                                                                                                                       uid=@Long[10001],                                                                                                        nickName=@String[kerwin],                                                                                            ]
        • 创建一个User对象,在创建一个OgnlDemo03对象,将创建的User对象传入OgnlDemo03对象的setUser(user)方法
  • [arthas@24600]$ ognl '#user = new com.kerwin.arthas.demo.User(10001,"kerwin"),new com.kerwin.arthas.demo.OgnlDemo03().setUser(#user)'                                                                                                              @User[                                                                                                                       uid=@Long[10001],                                                                                                        nickName=@String[kerwin],                                                                                            ]
        • 调用getMyUser()方法获取User对象作为changeUser(user)的入参
  • # {#user1,#user2} 代表将user1、user2这两个对象作为数组输出在控制台,因为这里没有加-x 2默认展开层级为1所以输出的是对象内存地址[arthas@18904]$ ognl '#user1 = @com.kerwin.arthas.demo.OgnlDemo03@getMyUser(),#user2 = @com.kerwin.arthas.demo.OgnlDemo03@changeUser(#user1),{#user1,#user2}'                                                                                      @ArrayList[                                                                                                                  @User[com.kerwin.arthas.demo.User@30833e75],                                                                             @User[com.kerwin.arthas.demo.User@70ca5419],                                                                         ]# 加上-x 2 可以展开数组内部对象                                                                                         [arthas@18904]$ ognl '#user1 = @com.kerwin.arthas.demo.OgnlDemo03@getMyUser(),#user2 = @com.kerwin.arthas.demo.OgnlDemo03@changeUser(#user1),{#user1,#user2}' -x 2                                                                                 @ArrayList[                                                                                                                  @User[                                                                                                                       uid=@Long[10002],                                                                                                        nickName=@String[kerwin2],                                                                                           ],                                                                                                                       @User[                                                                                                                       uid=@Long[10002],                                                                                                        nickName=@String[kerwin2---changeUser],                                                                              ],                                                                                                                   ]
      • 数组入参
  • [arthas@18904]$ ognl '@com.kerwin.arthas.demo.OgnlDemo03@setLists({"k1","k2","k3"})'                                     @ArrayList[                                                                                                                  @String[k1],                                                                                                             @String[k2],                                                                                                             @String[k3],                                                                                                         ]
      • Map入参
  • [arthas@18904]$ ognl '#map = #{"id":10003L,"nickName":"k3"},@com.kerwin.arthas.demo.OgnlDemo03@setMaps(#map)'            @LinkedHashMap[                                                                                                              @String[id]:@Long[10003],                                                                                                @String[nickName]:@String[k3],                                                                                       ]
  • 实践操作:使用ognl表达式获取Spring上下文中对象,并进行操作
    • 测试代码
  • @Servicepublic class OgnlDemoService {private String description;public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}}
    • 自定义一个获取Spring上下文中对象的工具类
  • @Componentpublic class SpringApplicationContext implements ApplicationContextAware {private static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringApplicationContext.applicationContext = applicationContext;}/*** 通过class获取Bean*/public static <T> T getBean(Class<T> clazz) {return applicationContext.getBean(clazz);}/*** 通过name获取 Bean.*/public static Object getBean(String name) {return applicationContext.getBean(name);}}
    • 获取Spring上下文中的OgnlDemoService对象
  • # 通过beanName获取[arthas@30000]$ ognl '@com.example.test_mybatis.controller.SpringApplicationContext@getBean("ognlDemoService")'                      @OgnlDemoService[                                                                                                            description=null,                                                                                                    ]# 通过class获取                                                                                                                  [arthas@30000]$ ognl '#OgnlDemoServiceClass =@com.example.test_mybatis.service.OgnlDemoService@class,@com.kerwin.arthas.utils.SpringApplicationContext@getBean(#OgnlDemoServiceClass)'                                                                   @OgnlDemoService[                                                                                                            description=null,                                                                                                    ]
    • 操作OgnlDemoService对象中变量和方法
  • [arthas@30000]$ ognl '@com.example.test_mybatis.controller.SpringApplicationContext@getBean("ognlDemoService").description'          null                                                                                                                     [arthas@30000]$ ognl '@com.example.test_mybatis.controller.SpringApplicationContext@getBean("ognlDemoService").setDescription("HelloW orld")'                                                                                                                  null                                                                                                                     [arthas@30000]$ ognl '@com.example.test_mybatis.controller.SpringApplicationContext@getBean("ognlDemoService").getDescription()'     @String[Hello World]
  • trace
  • 使用 trace 命令可以跟踪统计方法耗时,经常用于排查运行较慢、耗时较长的场景
  • 测试代码
  • package com.example.test_mybatis.controller;import com.example.test_mybatis.service.UserServiceImpl;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;@RestController@Slf4jpublic class UserController {@Autowiredprivate UserServiceImpl userService;@GetMapping(value = "/user")public HashMap<String, Object> getUser(Integer uid) throws Exception {// 模拟用户查询userService.get(uid);HashMap<String, Object> hashMap = new HashMap<>();hashMap.put("uid", uid);hashMap.put("name", "name" + uid);return hashMap;}}package com.example.test_mybatis.service;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service;@Service@Slf4jpublic class UserServiceImpl {public void get(Integer uid) throws Exception {check(uid);service(uid);redis(uid);mysql(uid);}public void service(Integer uid) throws Exception {int count = 0;for (int i = 0; i < 10; i++) {count += i;}log.info("service  end {}", count);}public void redis(Integer uid) throws Exception {int count = 0;for (int i = 0; i < 10000; i++) {count += i;}log.info("redis  end {}", count);}public void mysql(Integer uid) throws Exception {long count = 0;for (int i = 0; i < 10000000; i++) {count += i;}log.info("mysql end {}", count);}public boolean check(Integer uid) throws Exception {if (uid == null || uid < 0) {log.error("uid不正确,uid:{}", uid);throw new Exception("uid不正确");}return true;}}
  • 在arthas中使用命令 trace com.example.test_mybatis.controller.UserController getUser

  • 继续跟踪耗时高的方法,然后再次访问。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com