spring boot整合es,及相關問題解決


引入依賴

<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/netty/issues/6956

解決方案一:

寫個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);
 }
		


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM