springboot使用arthas-spring-boot-starter引入arthas方案

1、maven引入arthas-spring-boot-starter

        <dependency>
			<groupId>com.taobao.arthas</groupId>
			<artifactId>arthas-spring-boot-starter</artifactId>
			<version>3.6.3</version>
		</dependency>

2、自定义配置文件ArthasConfigMapConfig,也可以通过yaml文件配置

import com.alibaba.arthas.spring.ArthasProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.SocketUtils;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Author: niuzhenyu
 * @Description:
 * @Date:Create:in 2022/6/28 15:45
 * @Modified By:
 */
@Slf4j
@Configuration
@ConditionalOnClass(value = ArthasProperties.class)
public class ArthasConfigMapConfig {

    @Value("${zhxy.service.microServiceName}")
    private String serviceName;

    @Value("${server.port:8080}")
    private String serverPort;

    @Autowired
    private Environment environment;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Bean("arthasConfigMap")
    public Map<String, String> arthasConfigMap(@Autowired ArthasProperties arthasProperties) {
        Map<String, String> map = new HashMap<>();

        //接收环境变量参数,否则随机生成
        String agentId = environment.getProperty("arthas.agentId");
        //优先使用环境变量arthas.name,其次使用服务名+随机数
        if (agentId == null) {
            Random secureRandom = new SecureRandom();
            agentId = serviceName + ":" + secureRandom.nextInt(1000);
        }
        log.info("agentId:{}", agentId);
        map.put("agent-id", agentId);


        //优先从配置中获取tunnel-server,如果没有,则从redis中获取,如果redis没有则使用默认
        String tunnelServer = arthasProperties.getTunnelServer();
        if (tunnelServer == null) {
            tunnelServer = redisTemplate.opsForValue().get("arthas.tunnel-server");
            if (tunnelServer == null) {
                tunnelServer = "ws://ip:7777/ws";
                redisTemplate.opsForValue().set("arthas.tunnel-server", tunnelServer);
            }
        }
        log.info("tunnelServer:{}", tunnelServer);
        map.put("tunnel-server", tunnelServer);

        Integer serverPortInt = null;
        if(StringUtils.isNotBlank(serverPort)) {
            serverPortInt = Integer.parseInt(serverPort);
        }

        //优先从环境变量中获取,其次从配置文件中获取,否则为serverPort+1,否则随机(值为0),若值为-1则不监听端口
        String httpPort = environment.getProperty("arthas.httpPort");
        if (httpPort == null) {
            if (arthasProperties.getHttpPort() == 0) {
                if (serverPortInt!=null) {
                    map.put("http-port", "" + (serverPortInt + 1));
                } else {
                    map.put("http-port", "" + SocketUtils.findAvailableTcpPort());
                }
            } else {
                map.put("http-port", "" + arthasProperties.getHttpPort());
            }
        } else {
            map.put("http-port", httpPort);
        }
        log.info("httpPort:{}", map.get("http-port"));


        //优先从环境变量中获取,其次从配置文件中获取,否则为serverPort+2,否则随机(值为0),若值为-1则不监听端口
        String telnetPort = environment.getProperty("arthas.telnetPort");

        if (telnetPort == null) {
            if (arthasProperties.getTelnetPort() == 0) {
                if (serverPortInt!=null) {
                    map.put("telnet-port", "" + (serverPortInt + 2));
                } else {
                    map.put("telnet-port", "" + SocketUtils.findAvailableTcpPort());
                }
            } else {
                map.put("telnet-port", "" + arthasProperties.getHttpPort());
            }
        } else {
            map.put("telnet-port", telnetPort);
        }
        log.info("telnetPort:{}", map.get("telnet-port"));


        //如果有配置target-ip,那么使用,否则默认127.0.0.1
        if (StringUtils.isNotBlank(arthasProperties.getIp())) {
            map.put("ip", arthasProperties.getIp());
        }
        log.debug("targetIp:{}", map.get("ip"));

        //鉴权用的用户名,默认arthas,targetIp非127.0.0.1才生效
        if (StringUtils.isNotBlank(arthasProperties.getUsername())) {
            map.put("username", arthasProperties.getUsername());
        } else {
            map.put("username", "username");
        }
        log.debug("username:{}", map.get("username"));

        //鉴权用的密码,targetIp非127.0.0.1才生效
        if (StringUtils.isNotBlank(arthasProperties.getPassword())) {
            map.put("password", arthasProperties.getPassword());
        } else {
            map.put("password", "pwd");
        }
        log.debug("password:{}", map.get("password"));


        //不禁用stop命令
        map.put("disabledCommands", "");
        return map;
    }
}

3、下载arthas-tunnel-server-3.6.3-fatjar.jar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值