Spring4 整合ElasticSearch6.x:availableProcessors is already set to [4], rejecting [4]分析

虾米哥 阅读:290 2021-03-31 13:47:41 评论:0

Spring4.x 整合ElasticSearch6.x 提示如下错误信息:

java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]

解决方式就是在项目初始化钱设置一下一个属性。在初始化之前加上

System.setProperty(“es.set.netty.runtime.available.processors”, “false”);

Spring4.x 集成ElasticSearch6.x 配置对象:

import java.net.InetAddress; 
import java.net.UnknownHostException; 
 
import javax.annotation.PostConstruct; 
 
import org.elasticsearch.client.transport.TransportClient; 
import org.elasticsearch.common.settings.Settings; 
import org.elasticsearch.common.transport.TransportAddress; 
import org.elasticsearch.transport.client.PreBuiltTransportClient; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
 
import com.zzg.elasticsearch.util.ElasticSearchUtil; 
/** 
 * elasticsearch 检索配置对象 
 * @author zzg 
 * 
 */ 
@Configuration 
public class ElasticsearchConfig { 
	// 日志记录 
	public static final Logger logger = LoggerFactory.getLogger(ElasticsearchConfig.class); 
	 
	@PostConstruct 
	public void init() { 
        System.setProperty("es.set.netty.runtime.available.processors", "false"); 
    } 
	 
	@Bean 
	public TransportClient getTransportClient(){ 
		TransportClient transportClient = null; 
		 try { 
	            Settings settings = Settings.builder() 
	                    .put("cluster.name", "my-application").build(); 
 
	            transportClient = new PreBuiltTransportClient(settings) 
	                    .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.73"), 9300)); 
	        } catch (UnknownHostException e) { 
	           logger.error(e.getMessage()); 
	        } 
	    return transportClient; 
	} 
	 
	@Bean 
	public ElasticSearchUtil getElasticSearchUtil(){ 
		return new ElasticSearchUtil(getTransportClient()); 
	} 
 
} 

或者在SpringBoot 项目程序启动入口添加:

@SpringBootApplication 
public class Application { 
 
    public static void main(String[] args) { 
//        System.out.println("==========================================="); 
        /** 
         * Springboot整合Elasticsearch 在项目启动前设置一下的属性,防止报错 
         * 解决netty冲突后初始化client时还会抛出异常 
         * java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4] 
         */ 
        System.setProperty("es.set.netty.runtime.available.processors", "false"); 
        SpringApplication.run(EurekaBussnissServiceUserApplication.class, args); 
    } 
}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号