栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

spring boot整合elasticSearch,及相关问题解决

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot整合elasticSearch,及相关问题解决

引入依赖

     org.springframework.boot
     spring-boot-starter-data-elasticsearch

定义实体对象
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;


@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;


public interface UserdocumentRespository extends ElasticsearchRepository {

}
测试类
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;


@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,引入的依赖是


     org.springframework.boot
     spring-boot-starter-data-redis

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;


@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);
 }
		
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/234209.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号