在controller添加代码

@Autowired
private EurekaClient eurekaClient;

@GetMapping("getUrl0")
public String serviceUrl2() {
    InstanceInfo instance = eurekaClient.getNextServerFromEureka("TEST001", false);
    return instance.getHomePageUrl();
}

或者

@Autowired
private DiscoveryClient discoveryClient;

@GetMapping("getUrl")
public String serviceUrl() {
    List list = discoveryClient.getInstances("TEST001");
    if (list != null && list.size() > 0 ) {
        return list.get(0).getUri().toString();
    }
    return null;
}

通过url查看为:

wKioL1lricLBUmgaAAA9POwd3Fc441.png-wh_50

wKiom1lricOhaCs4AABCuu4ze9U789.png-wh_50

展示的为:http://LAPTOP-DPI1U1CG:8081/


如果需要展示ip需要再yml添加配置:

spring-Cloud 查看地址_第1张图片

重启注册中心eureka再次访问可出现ip地址:

wKioL1lriQORh_WrAAA4Z39a8BQ677.png-wh_50

wKiom1lriQPiGLZfAAA5cjkotNQ373.png-wh_50



还有一种方式

@GetMapping("getUrl2")
public ServiceInstance serviceUrl2() {
    ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
    return serviceInstance;
}

展示效果

wKiom1lri6fB7acEAAA-325shP8882.png-wh_50

{"host":"192.168.2.101","port":8081,"metadata":{},"serviceId":"test001","uri":"http://192.168.2.101:8081","secure":false}