在@Autowired EurekaDiscoveryClient之后无法创建Bean

时间:2016-07-02 07:46:15

标签: spring-cloud netflix-eureka spring-cloud-netflix

我正在尝试使用从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);
    }
}

任何建议都表示赞赏。

0 个答案:

没有答案