引言
对于注册进 eureka里面的微服务,可以通过服务发现来获得该服务的信息
修改支付服务8091的controller
@Slf4j
@RestController
public class PaymentController {
@Resource
PaymentService paymentService;
@Value("${server.port}")
private String SERVER_PORT;
@Resource
DiscoveryClient discoveryClient;
@GetMapping("/payment/discovery")
public Object discovery() {
// 获取所有注册的服务名
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("注册的服务 : " +service);
}
// 获取某个注册服务的实例信息
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVER");
log.info("服务id\t\t\t\t\t端口号\t\t主机\t\turl");
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId() + "\t" + instance.getPort() + "\t" +
instance.getHost() + "\t" + instance.getUri());
}
return this.discoveryClient;
}
}
修改启动类
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentMain8091 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8091.class,args);
}
}