1. 客户端搭建
前提:接上文SBA+Arthas服务端已经搭建完成
引入关键依赖,(actuator是监听组件,但是没有ui,接入SBA展示)
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spring-boot-starter</artifactId>
<version>3.6.4</version>
</dependency>
2. yml注册模块配置
server:
port: 28001
servlet:
context-path: /iotw
shutdown: graceful
spring:
boot:
admin:
client:
url: SBA服务的地址:端口
username: admin
password: admin
instance:
service-url: 客户端服务的地址:端口
## 如果存在server.servlet.context-path,需要添加下面的参数
management-base-url: 客户端地址服务地址:端口/${server.servlet.context-path}
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: "*"
base-path: '/actuator'
enabled-by-default: true
arthas:
## 自定义本服务注册到arthas的唯一agentId
agent-id: work
tunnel-server: ws://127.0.0.1:7777/ws
arthas.tunnel-server:客户端使用websocket的方式注册到SBA的Arthas服务中,tunnel-server的服务地址为部署SBA的服务IP,端口为SBA中定义的arthas.server.port的值,默认为7777。
3. 分布式环境下多节点注册agent-id冲突问题
客户端初始化时拦截注册到arthas配置信息重新编写,添加当前服务IP前缀
package com.tsit.iot.work.config;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author :yzg
* @date :Created in 2022/8/8 16:40
* @description:
* @modified By:
* @version: $
*/
@Slf4j
@Component
public class ArthasPropertiesBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
try {
if (StrUtil.equals(beanName, "arthasConfigMap")) {
String ip = NetUtil.getLocalhostStr();
log.info("arthas current ip={}", ip);
((Map) bean).put("agent-id", ip + "_" + ((Map) bean).get("agent-id"));
}
} catch (Exception e) {
log.warn("arthas get local ip unexpected occur", e);
}
return bean;
}
}
4. 效果展示






搞定收工,可以愉快的压测调优了![]()
本文详细介绍了如何在Spring Boot应用中集成Spring Boot Admin客户端,配置Arthas以实现服务注册,并解决分布式环境中的agent-id冲突问题。通过YAML配置和自定义BeanPostProcessor,确保客户端与SBA服务器的正确连接与监控。

2483

被折叠的 条评论
为什么被折叠?



