1.版本说明
Spring-Boot版本:2.3.7.RELEASE
Spring-Cloud版本:Hoxton.SR9
Spring-Cloud-Alibaba版本:2.2.5.RELEASE
Spring-Boot-Admin-starter-client:2.3.1
Spring-Boot-Admin-starter-server:2.3.1
这里springboot和springboot admin的大小版本最好相同,如boot版本是2.1,admin版本也应该是2.1,否则项目很有可能会报错无法启动。
2.创建监控server端:
2.1.pom依赖
(1)这里web排除tomcat,引入undertow,是因为用tomcat有可能会在启动时报错。
(2)引入了security依赖,这样会在admin必须要登陆才可以进入,保证了一定的安全性。
de.codecentric
spring-boot-admin-starter-server
${spring-boot-admin.version}
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
org.springframework.boot
spring-boot-starter-web
spring-boot-starter-tomcat
org.springframework.boot
org.springframework.boot
spring-boot-starter-undertow
org.springframework.boot
spring-boot-starter-security
3.配置文件
注意:如果在properties中,星号不需要打引号,在yml中,需要打单引号!
server:
port: 9111
spring:
application:
# 应用名称
name: cloud-monitor
security:
user:
name: admin
password: 123456
boot:
admin:
ui:
title: 服务状态监控
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
logfile:
enabled: true #可在线查看日志
4.主启动类和相关配置
@EnableDiscoveryClient
@EnableAdminServer //开启spring boot admin相关功能
@SpringBootApplication
public class MonitorApplication
{
public static void main(String[] args)
{
SpringApplication.run(MonitorApplication.class, args);
}
}
5.spring security配置
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter
{
private final String adminContextPath;
public WebSecurityConfigurer(AdminServerProperties adminServerProperties)
{
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http
.headers().frameOptions().disable()
.and().authorizeRequests()
.antMatchers(adminContextPath + "/assets/**"
, adminContextPath + "/login"
, adminContextPath + "/actuator/**"
, adminContextPath + "/instances/**"
).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login")
.successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout")
.and()
.httpBasic().and()
.csrf()
.disable();
}
}
5.创建被监控的client端(微服务):
5.1pom中添加依赖
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
org.springframework.boot
spring-boot-starter-actuator
de.codecentric
spring-boot-admin-starter-client
5.22. yml中配置
spring:
boot:
admin:
client:
url: http://localhost:8888 #admin server的地址
management:
endpoints:
web:
exposure:
include: '*' #暴露所有可监控的端点
需要注意的是:如果要监控被security或oauth2所保护的微服务,我们需要在security中配置(如果因为安全,我们可以配置ip白名单,只有指定ip,才可以访问)
6.登录,浏览器输入server端ip+端口