视频链接:2020最新版SpringCloud框架开发教程-周阳
文章源码:https://github.com/geyiwei-suzhou/cloud2020/
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
1. 修改payment8001的controller
注入如下字段(org.springframework.cloud.client.discovery.DiscoveryClient):
@Resource
private DiscoveryClient discoveryClient;
添加discovery接口:
@GetMapping(value = "/payment/discovery")
public Object discovery() {
List<String> services = discoveryClient.getServices();
for (String element : services) {
log.info("*****element: " + element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getInstanceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
}
return this.discoveryClient;
}
2. 修改payment8001的启动类
在PaymentMain8001类上添加如下注解:
@EnableDiscoveryClient
3. 启动测试
浏览器输入:
http://localhost:8001/payment/discovery
同样的方式我们把payment8002也加上