0

我正在尝试创建 bean cassandraServer,它使用从 Netflix Eureka 服务器获取的 IP 地址,但每次它都会为 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);
    }
}

任何建议表示赞赏。

4

0 回答 0