继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

慕妹6017191
关注TA
已关注
手记 5
粉丝 1
获赞 4

引入依赖

<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

解决方案一:

写个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);
 }
		
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP