0

I am trying to create bean cassandraServer which uses IP address fetched from Netflix Eureka server, but everytime it throws NPE for discoveryClient.

How can I create Bean using properties from Eureka Server?

@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);
    }
}

Any suggestion is appreciated.

Vinod Patel
  • 430
  • 1
  • 7
  • 19

0 Answers0