架构师成长记_第八周_21_Springboot 整合 elasticsearch
Posted 流浪少年的梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了架构师成长记_第八周_21_Springboot 整合 elasticsearch相关的知识,希望对你有一定的参考价值。
文章目录
Springboot 整合 elasticsearch
1. 创建es模块
2. 在es的模块下引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
PS: 改版本对应的es是 6.4.3, 故我们集群ES的配置需要下调为 6.4.3. (或者提升这里的版本)
3. 创建 yml 配置文件
spring:
datasource: # 数据源的相关配置
type: com.zaxxer.hikari.HikariDataSource # 数据源类型:HikariCP
driver-class-name: com.mysql.jdbc.Driver # mysql驱动
url: jdbc:mysql://localhost:3306/foodie-shop-dev?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
username: root
password: 210374520
data:
elasticsearch:
cluster-nodes: 192.168.92.148:9300 # PS:后端对接的接口号为9300, es为9200
cluster-name: es6
4. 创建启动类
package com.beyond;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
5. 运行报错 (是由于无netty配置引起的错误)
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]
6. 解决该问题
创建ESConfig类
package com.beyond;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class ESConfig {
/**
* 解决netty引起的issue
*/
@PostConstruct
void init() {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
}
以上是关于架构师成长记_第八周_21_Springboot 整合 elasticsearch的主要内容,如果未能解决你的问题,请参考以下文章