对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
    修改cloud-provider-payment8001的Controller

    1. @RestController
    2. @Slf4j
    3. public class PaymentController {
    4. @Resource
    5. private PaymentService paymentService;
    6. @Value("${server.port}")
    7. private String serverPort;
    8. @Resource
    9. private DiscoveryClient discoveryClient;
    10. 。。。。。。
    11. @GetMapping(value = "/payment/discovery")
    12. public Object discovery()
    13. {
    14. List<String> services = discoveryClient.getServices();
    15. for (String element : services) {
    16. log.info("*****element: "+element);
    17. }
    18. List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    19. for (ServiceInstance instance : instances) {
    20. log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
    21. }
    22. return this.discoveryClient;
    23. }
    24. }

    主启动类添加 @EnableDiscoveryClient

    1. @SpringBootApplication
    2. @EnableEurekaClient
    3. @EnableDiscoveryClient
    4. public class PaymentMain8001 {
    5. public static void main(String[] args) {
    6. SpringApplication.run(PaymentMain8001.class,args);
    7. }
    8. }

    测试
    访问http://localhost:8001/payment/discovery
    image.png
    image.png