对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
修改cloud-provider-payment8001的Controller
@RestController@Slf4jpublic class PaymentController {@Resourceprivate PaymentService paymentService;@Value("${server.port}")private String serverPort;@Resourceprivate DiscoveryClient discoveryClient;。。。。。。@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.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());}return this.discoveryClient;}}
主启动类添加 @EnableDiscoveryClient
@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class PaymentMain8001 {public static void main(String[] args) {SpringApplication.run(PaymentMain8001.class,args);}}
测试
访问http://localhost:8001/payment/discovery

