我正在尝试使用从Netflix Eureka服务器获取的IP地址创建bean cassandraServer,但每次它都会为discoveryClient抛出NPE。
如何使用Eureka Server中的属性创建Bean?
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Autowired
private DiscoveryClient discoveryClient;
@Bean
public String cassandraServer() throws InterruptedException {
return this.discoveryClient.getInstances("some-service").get(0).getHost();
}
}
@RestController
class ServiceInstanceRestController {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
}
任何建议都表示赞赏。