• SpringBoot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]


    转载自:https://segmentfault.com/a/1190000022237333
    引入依赖
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>

    定义实体对象

    package com.tangyuewei.user.document;
    import lombok.Data;
    import org.springframework.data.elasticsearch.annotations.Document;
    import org.springframework.data.elasticsearch.annotations.Field;
    import org.springframework.data.elasticsearch.annotations.FieldType;
    
    import javax.persistence.Id;
    
    /**
     * @author tangyuewei
     * <p>
     * Description: es文档对象
     * </p>
     * @date 2020/4/1
     */
    @Document(indexName = "user",type = "docs", shards = 1, replicas = 0)
    @Data
    public class UserDocument {
    
        @Id
        private String id;
    
        @Field(type = FieldType.Keyword)
        private String userName;
    
    }
    

    继承接口

    package com.tangyuewei.user.common.es;
    import com.tangyuewei.user.document.UserDocument;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
    
    /**
     * @author tangyuewei
     * <p>
     * Description:定义 UserDocumentRespository 接口
     * </p>
     * @date 2020/4/1
     */
    public interface UserDocumentRespository extends ElasticsearchRepository<UserDocument,String> {
    
    }

    测试类

    package com.tangyuewei.user.tests;
    
    import com.tangyuewei.user.common.es.UserDocumentRespository;
    import com.tangyuewei.document.UserDocument;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.annotation.Resource;
    
    /**
     * @author tangyuewei
     * <p>
     * Description:
     * </p>
     * @date 2020/4/1
     * @see com.tangyuewei.user.tests
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class UserTests {
    
        @Resource
        private ElasticsearchTemplate elasticsearchTemplate;
    
        @Resource
        private UserDocumentRespository userDocumentRespository;
    
        @Test
        public void testCreateIndex() {
            elasticsearchTemplate.createIndex(UserDocument.class);
            UserDocument user = new UserDocument();
            user.setId("123456");
            user.setUserName("Test");
            UserDocument saveUser = userDocumentRespository.save(user);
            Assert.assertNotNull(saveUser);
            System.out.println(userDocumentRespository.findById(user.getId()));
            elasticsearchTemplate.deleteIndex(UserDocument.class);
        }
    
    }

    运行测试

    运行时抛的的异常如下,此处只截取了部分

    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDocumentRespository': Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'elasticsearchTemplate' parameter 0; nested exception is 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]
    
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDocumentRespository': Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'elasticsearchTemplate' parameter 0; nested exception is 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]
    
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'elasticsearchTemplate' parameter 0; nested exception is 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]
    
    Caused by: 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]
    
    Caused by: 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]
    
    Caused by: java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
    

    因为spring boot项目中也使用了Redis,引入的依赖是

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    github上给出的回答是:启动时设置es.set.netty.runtime.available.processors=false

    原文地址: https://github.com/netty/nett...

    解决方案一:

    写个config类:

    package com.tangyuewei.user.common.es;
    
    import org.springframework.context.annotation.Configuration;
    
    import javax.annotation.PostConstruct;
    
    /**
     * @author tangyuewei
     * <p>
     * Description: 解决同时引用redis与es启动时报错
     * </p>
     * @date 2020/4/1
     * @see  com.tangyuewei.user.common.es
     */
    @Configuration
    public class ElasticSearchConfig {
        @PostConstruct
        void init() {
            System.setProperty("es.set.netty.runtime.available.processors", "false");
        }
    }

    解决方案二:

    在程序的入口类main方法上加入

    public static void main(String[] args) {
            System.setProperty("es.set.netty.runtime.available.processors", "false");
            SpringApplication.run(SpringbootApplication.class, args);
     }
  • 相关阅读:
    Entity Framework Code-First(10.3):Property Mappings
    Entity Framework Code-First(10.2):Entity Mappings
    Entity Framework Code-First(10.1):EntityTypeConfiguration
    Entity Framework Code-First(10):Fluent API
    Entity Framework Code-First(9.11):DataAnnotations
    Entity Framework Code-First(9.10):DataAnnotations
    Entity Framework Code-First(9.9):DataAnnotations
    Entity Framework Code-First(9.8):DataAnnotations
    Entity Framework Code-First(9.7):DataAnnotations
    JAVA-初步认识-第六章-类与对象体现
  • 原文地址:https://www.cnblogs.com/lijing1/p/13605065.html
Copyright © 2020-2023  润新知