org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘elasticsearchClient’ defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: Factory method ‘elasticsearchClient’ threw exception; nested exception is java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
报错信息如上,创建elasticsearchClient bean失败,是因为TransportClient instantiate Failed
异常信息nested exception is java.lang.IllegalStateException让人摸不着头脑,感觉仿佛是自己什么地方配置错误,而且刚刚接触,不熟悉配置信息(其实是个坑)
原因:
Es的netty与Netty服务本身不兼容(你说bug也行)
SpringBoot 2.X 的 spring-boot-starter-data-redis 默认是以 lettuce 作为连接池的, 而在 lettuce , elasticsearch transport 中都会依赖netty, 二者的netty 版本不一致,不能够兼容
**
**
关于@PostConstruct注解
参考:https://blog.csdn.net/qq360694660/article/details/82877222
/**
* @ClassName ElasticSearchConfig
* @Description //ElasticSearchConfig
* @Author TY
* @Date 2021/1/26 10:05
* @Version 1.0
**/
@Configuration
public class ElasticSearchConfig {
/**
* 防止netty的bug
* java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
*/
@PostConstruct
void init() {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
}