引言

对于注册进 eureka里面的微服务,可以通过服务发现来获得该服务的信息

修改支付服务8091的controller

  1. @Slf4j
  2. @RestController
  3. public class PaymentController {
  4. @Resource
  5. PaymentService paymentService;
  6. @Value("${server.port}")
  7. private String SERVER_PORT;
  8. @Resource
  9. DiscoveryClient discoveryClient;
  10. @GetMapping("/payment/discovery")
  11. public Object discovery() {
  12. // 获取所有注册的服务名
  13. List<String> services = discoveryClient.getServices();
  14. for (String service : services) {
  15. log.info("注册的服务 : " +service);
  16. }
  17. // 获取某个注册服务的实例信息
  18. List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVER");
  19. log.info("服务id\t\t\t\t\t端口号\t\t主机\t\turl");
  20. for (ServiceInstance instance : instances) {
  21. log.info(instance.getServiceId() + "\t" + instance.getPort() + "\t" +
  22. instance.getHost() + "\t" + instance.getUri());
  23. }
  24. return this.discoveryClient;
  25. }
  26. }

修改启动类

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