转载

Springboot2.x整合ElasticSearch报错


错误信息:

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]

Springboot启动类遇到此问题的解决办法:

@SpringBootApplication
public class Application{
    public static void main(String[] args) {
    	System.setProperty("es.set.netty.runtime.available.processors","false");
        SpringApplication.run(Application.class, args);
    }
}

单元测试遇到此问题的解决办法:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ESTest {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    public ESTest(){
    	// 在测试类构造函数中加入这行代码
        System.setProperty("es.set.netty.runtime.available.processors","false");
    }

    @Test
    public void testCreateIndex() {
        elasticsearchTemplate.createIndex(Users.class);
    }
}

Java
Springboot
ES

评论