微服务 endpoint 监控配置参考——Nacos作为注册中心

application.properties 配置

management.endpoints.web.base-path=/manage
management.endpoints.web.exposure.include=*

直接访问

1、访问路径如下:http://192.168.1.3:8848/nacos/manage

在这里插入图片描述

2、这里关注一个 mappings 模块,然后访问 mappings 模块,这个模块保存着 web 的访问路径和对应处理事哪个类哪个方法,用来查看 Nacos 或者其他组件非常方便。

3、然后访问路径:http://192.168.1.3:8848/nacos/manage/mappings,结果如下:

在这里插入图片描述

展开其中的 dispatcherServlets ,然后搜索 /v1/ns/instance 可以发现服务注册这个访问路径对应的处理类是:

在这里插入图片描述

这个我们在测试环境中开启这个功能非常方便,就相当于是一个 Swagger2.0 的功能展示。

使用 Nacos 作为 Spring Cloud 的注册中心需要以下几个步骤: 1. 添加依赖:在项目的 `pom.xml` 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> ``` 2. 配置 Nacos 地址:在项目的 YAML 配置文件(比如 `application.yml`)中添加以下配置: ```yaml spring: cloud: nacos: discovery: server-addr: your-nacos-server-address:port ``` 请将 `your-nacos-server-address` 替换为实际的 Nacos 服务器地址和端口。 3. 启用服务注册和发现:在 Spring Boot 的启动类上添加 `@EnableDiscoveryClient` 注解,例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 4. 在需要注册的服务上添加 `@RestController` 和 `@RequestMapping` 注解,例如: ```java import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class YourController { @RequestMapping("/your-endpoint") public String yourEndpoint() { return "Hello, world!"; } } ``` 5. 启动应用程序:运行 Spring Boot 应用程序,它将自动向 Nacos 注册服务,并可通过 Nacos 进行服务发现。 这样,你就成功将 Nacos 作为 Spring Cloud 的注册中心了。可以通过 Nacos 控制台验证服务是否注册成功,并使用服务名访问注册的服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔道不误砍柴功

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值