springboot使用arthas-spring-boot-starter引入arthas方案
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
点击阅读全文
更多推荐
活动日历
查看更多
直播时间 2025-02-26 16:00:00


直播时间 2025-01-08 16:30:00


直播时间 2024-12-11 16:30:00


直播时间 2024-11-27 16:30:00


直播时间 2024-11-21 16:30:00


所有评论(0)