Admin监控服务端搭建后,一个个微服务单元注册到服务是阔以的,还有也可以借助Nacos,Nacos自动帮我们整合了与Admin的关联工作,只需要将所有客户端服务注册进Nacos,并且与服务端保持在同一命名空间和分组下,每个服务简单配置健康监测即可。
一、Admin&Nacos服务端搭建
搭建方式参考:springboot集成Admin「服务端」
根据上述文章后,基础的具有安全认证的Admin服务端已搭建妥了,下面集成健康监测与Nacos,
1. application.yml 配置
server:
port: 7003
servlet:
context-path: /springboot-admin-nacos-server
spring:
application:
name: springboot-admin-nacos-server
security:
user:
name: admin
password: admin
#健康监测
management:
server:
port: 17003
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
logging:
file: admin.log
2. bootstrap.yml Nacos配置
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: 7e98b650-0c03-4663-b747-b3d4848630aa
group: DEFAULT_GROUP
3. 启动类
@EnableAdminServer
@EnableDiscoveryClient
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class,args);
}
}
启动后,访问: http://localhost:7003/springboot-admin-nacos-server/#/applications
访问:http://localhost:8848/nacos/index.html
看到上面两个页面效果,springboot 集成 Admin & Nacos 的服务端已妥了。
二、Admin&Nacos客户端搭建
搭建方式参考:springboot集成Admin「客户端」
1. 引入增加的POM依赖
<!--健康检查-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. application.yml 配置
server:
port: 7004
servlet:
context-path: /springboot-admin-nacos-client-a
spring:
application:
name: springboot-admin-nacos-client-a
# 相对原先的Nacos项目,只是多了以下健康监测的配置代码
management:
server:
port: 17004
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
logging:
file: admin.log
3. bootstrap.yml配置
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: 7e98b650-0c03-4663-b747-b3d4848630aa
group: DEFAULT_GROUP
4. 启动类
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class,args);
}
}
启动springboot-admin-nacos-client-a后,刷新以上两个页面:
按照springboot-admin-nacos-client-a同样方式,搭建springboot-admin-nacos-client-b,查看效果
三、搭建中可能会遇到的问题
1、服务已启动,成功注册到Nacos,但在Admin中是DOWN状态。
- 原因1
可能工程中未引入健康检测 spring-boot-starter-actuator 依赖。
- 原因2,缺少配置
management:
server:
port: 17004
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
- 原因3,缺少原因2中配置的server.port
这种原因是,工程application.yml配置了server.servlet.context-path,导致Admin检测时,404。
解决这个问题,有两种解决方案:
(1)配置端口
management:
server:
port: 17004
(2)注册Nacos时,增加元数据属性:
spring:
cloud:
nacos:
discovery:
metadata:
management:
context-path: ${server.servlet.context-path}/actuator
springboot集成admin DEMO: gitee.com/renxiaoshi/…