SBA集成Arthas

SpringBoot Admin

服务端

创建SBA服务

image-20220909110903187

image-20220909111111148

服务端配置

1
2
3
4
5
6
7
8
9
server:
port: 8888
spring:
application:
name: sba-server
management:
endpoint:
health:
show-details: always

客户端

创建SBA客户端

image-20220909112146430

image-20220909112223386

新增依赖

不加此依赖服务起不来

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

客户端配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
server:
port: 8080

spring:
application:
# 应用名称
name: sba-client
jmx:
enabled: true
boot:
admin:
client:
# 服务端 url
url: http://127.0.0.1:8888
instance:
# 客户端实例 url
service-url: http://127.0.0.1:8080
# 客户端实例名称
name: sba-client
# 必须配置服务端认证账号密码,否则不会注册
password: admin
username: admin
management:
endpoints:
web:
exposure:
# 暴漏的接口 - 所有接口
include: "*"
logging:
file:
name: client.log
pattern:
# 日志高亮
file: '%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx'

拓展

服务端SpringSecurity配置

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private final String adminContextPath;

@Autowired
private ArthasProperties arthasProperties;

public SecurityConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}

@Override
protected void configure(HttpSecurity http) throws Exception {

// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");

// allow iframe
if (arthasProperties.isEnableIframeSupport()) {
http.headers().frameOptions().disable();
}

http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()//Grants public access to all static assets and the login page.
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()// Every other request must be authenticated.
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()//Configures login and logout.
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()//Enables HTTP-Basic support. This is needed for the Spring Boot Admin Client to register.
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())// Enables CSRF-Protection using Cookies
.ignoringAntMatchers(
adminContextPath + "/instances",// Disables CRSF-Protection the endpoint the Spring Boot Admin Client uses to register.
adminContextPath + "/actuator/**"//Disables CRSF-Protection for the actuator endpoints.
);
}
}

集成Arthas Web Console

服务端配置

  1. 将Arthas源码中的tunnel-server中的类与文件拷贝到服务端,copy后如下

    image-20220909194431907

  2. 注释掉Arthas启动类ArthasTunnelApplication,使用SBA启动类来启动项目,SBA启动类如下

    1
    2
    3
    4
    5
    6
    7
    8
    @EnableAdminServer
    @SpringBootApplication
    @EnableCaching
    public class OkSbaServerApplication {
    public static void main(String[] args) {
    SpringApplication.run(OkSbaServerApplication.class, args);
    }
    }
  3. pom配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    <dependency>
    <groupId>com.taobao.arthas</groupId>
    <artifactId>arthas-common</artifactId>
    <version>${arthas.version}</version>
    </dependency>
    <dependency>
    <groupId>com.taobao.arthas</groupId>
    <artifactId>arthas-tunnel-common</artifactId>
    <version>${arthas.version}</version>
    </dependency>
    <build>
    <resources>
    <!-- 指定 src/main/resources下所有文件及文件夹为资源文件 -->
    <resource>
    <directory>src/main/resources</directory>
    <targetPath>${project.build.directory}/classes</targetPath>
    <includes>
    <include>**/*</include>
    </includes>
    <filtering>true</filtering>
    </resource>
    <!-- 通过 Maven Resource 的指定配置打入指定目录,实现 SBA 启动时的自定义加载 ,通过application配置 外链-->
    <resource>
    <directory>src/main/resources/static</directory>
    <targetPath>${project.build.directory}/classes/META-INF/spring-boot-admin-server-ui/extensions/arthas
    </targetPath>
    <filtering>false</filtering>
    </resource>
    </resources>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

客户端配置

1
2
3
4
5
6
7
<arthas.version>3.6.4</arthas.version>        
<!--arthas starter-->
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spring-boot-starter</artifactId>
<version>${arthas.version}</version>
</dependency>
1
2
3
4
5
6
7
8
9
#arthas配置
arthas:
tunnel-server: ws://localhost:7777/ws
#客户端id,应用名@随机值,js会截取前面的应用名
agent-id: ${spring.application.name}_${random.value}
#如果是防止一个机器上启动多个 arthas端口冲突。可以配置为随机端口(配置为0),或者配置为 -1,并且通过tunnel server来使用arthas。
telnet-port: -1
http-port: -1

服务端最终配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
server:
port: 8888

spring:
application:
name: ok-sba-server
## 集成了spring security安全组件,定义登录SBA的账号密码,
## 后期注册到SBA的客户端也要设置此权限才能注册进来
security:
user:
name: admin
password: admin
boot:
admin:
# SBA添加外链扩展页面,此处外链跳转Arthas控制台
ui:
external-views:
- label: "Arthas Console"
url: "./extensions/arthas/index.html"
order: 1900
- label: "AgentInfo"
url: "./extensions/arthas/apps.html"
order: 1901
# Arthas的缓存策略
cache:
type: caffeine
cache-names: inMemoryClusterCache
caffeine:
spec: maximumSize=3000,expireAfterAccess=3600s

# 监控所有页面
management:
endpoints:
web:
exposure:
include: '*'
metrics:
tags:
application: ${spring.application.name}
## 关闭rabbitmq,redis,es 健康检查
health:
redis:
enabled: false
rabbit:
enabled: false
elasticsearch:
enabled: false
# 总是显示服务健康细节
endpoint:
health:
show-details: always
# arthas tunnel-server监听地址端口
arthas:
server:
host: 0.0.0.0
port: ${PORT:7777}
enableDetailPages: true

SBA集成Arthas
http://example.com/2023/03/09/服务治理/SpringBoot Admin/
作者
UncleBryan
发布于
2023年3月9日
许可协议