Spring Cloud入门-Admin服务监控中心(Hoxton版本),java高级编程技术
2021/11/15 14:41:31
本文主要是介绍Spring Cloud入门-Admin服务监控中心(Hoxton版本),java高级编程技术,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
}
创建admin-client模块
这里我们创建一个admin-client模块作为客户端注册到admin-server。
在pom.xml中添加相关依赖:
org.springframework.boot
spring-boot-starter-web
de.codecentric
spring-boot-admin-starter-client
在application.yml中进行配置:
server:
port: 9305
spring:
application:
name: admin-client
boot:
admin:
client:
配置admin-server地址
url: http://localhost:9301
management:
endpoints:
web:
exposure:
include: ‘*’
endpoint:
health:
show-details: always
添加开启admin的日志监控
logging:
file: admin-client.log
启动admin-server和admin-client服务。
监控信息演示
访问如下地址打开Spring Boot Admin的主页:http://localhost:9301
点击wallboard按钮,选择admin-client查看监控信息;
监控信息概览;
度量指标信息,比如JVM、Tomcat及进程信息;
环境变量信息,比如系统属性、系统环境变量以及应用配置信息;
查看所有创建的Bean信息;
查看应用中的所有配置信息;
查看日志信息,admin-client模块的applicaiton.yml需要添加以下配置才能开启;
logging:
#添加开启admin的日志监控
file: admin-client.log
查看JVM信息;
查看可以访问的Web端点;
结合注册中心使用
Spring Boot Admin结合Spring Cloud 注册中心使用,只需将admin-server和注册中心整合即可,admin-server 会自动从注册中心获取服务列表,然后挨个获取监控信息。这里以Eureka注册中心为例来介绍下该功能。
修改admin-server
在pom.xml中添加相关依赖:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
在application-eureka.yml中进行配置,只需添加注册中心配置即可:
server:
port: 9301
spring:
application:
name: admin-server
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
在启动类上添加@En
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】 浏览器打开:qq.cn.hn/FTf 开源分享
ableDiscoveryClient来启用服务注册功能:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
修改admin-client
在pom.xml中添加相关依赖:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
在application-eureka.yml中进行配置,删除原来的admin-server地址配置,添加注册中心配置即可:
server:
port: 9305
spring:
application:
name: admin-client
boot:
admin:
client:
配置admin-server地址
url: http://localhost:9301
management:
endpoints:
web:
exposure:
include: ‘*’
endpoint:
health:
show-details: always
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
添加开启admin的日志监控
logging:
file: admin-client.log
在启动类上添加@EnableDiscoveryClient来启用服务注册功能:
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
功能演示
启动eureka-server,使用application-eureka.yml配置启动admin-server,admin-client;
查看注册中心发现服务均已注册:http://localhost:8001/
查看Spring Boot Admin 主页发现可以看到服务信息:http://localhost:9301
添加登录认证
我们可以通过给admin-server添加Spring Security支持来获得登录认证功能。
创建admin-security-server模块
在pom.xml中添加相关依赖:
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-web
de.codecentric
spring-boot-admin-starter-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
在application.yml中进行配置,配置登录用户名和密码,忽略admin-security-server的监控信息:
server:
port: 9301
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
spring:
application:
name: admin-security-server
security:
user:
配置登录用户名和密码
name: root
password: 123456
boot:
admin:
discovery:
不显示admin-security-server的监控信息
ignored-services: ${spring.application.name}
对SpringSecurity进行配置,以便admin-client可以注册:
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(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.authorizeRequests()
//1.配置所有静态资源和登录页可以公开访问
.antMatchers(adminContextPath + “/assets/**”).permitAll()
.antMatchers(adminContextPath + “/login”).permitAll()
.anyRequest().authenticated()
.and()
//2.配置登录和登出路径
.formLogin().loginPage(adminContextPath + “/login”).successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + “/logout”).and()
//3.开启http basic支持,admin-client注册时需要使用
.httpBasic().and()
.csrf()
//4.开启基于cookie的csrf保护
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
//5.忽略这些路径的csrf保护以便admin-client注册
.ignoringAntMatchers(
adminContextPath + “/instances”,
adminContextPath + “/actuator/**”
);
}
}
修改启动类,开启AdminServer及注册发现功能:
这篇关于Spring Cloud入门-Admin服务监控中心(Hoxton版本),java高级编程技术的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南