nacos服务发现
想要开启服务发现,需要在main函数上添加 @EnableDiscoveryClient 注解

然后我们编写一个controller类来查询nacos中注册的所有微服务以及对应的ip+端口号
@Controller
public class DiscoveryController {
@Autowired
DiscoveryClient discoveryClient;
// 这个方法可以获取nacos中注册的服务信息
@GetMapping("/getServicesInfo")
@ResponseBody
public String getServicesInfo() {
StringBuilder sb = new StringBuilder();
// 查询服务名称和服务的ip+端口号
for (String serviceId : discoveryClient.getServices()) {
sb.append(serviceId + "<br>");
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
sb.append(instance.getHost() + ":" + instance.getPort() + "<br>");
}
}
return sb.toString();
}
}
接着启动微服务,然后在浏览器中调用该接口就可以查到对应的信息

最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



