spring-Cloud 查看地址

在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<ServiceInstance> 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添加配置:

wKioL1lrihjAZMCIAABpqnjiXeE333.png-wh_50

重啓註冊中心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}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章