版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/IT_hejinrong/article/details/89483012
文章目录
一. 项目整合
1.1 pom文件依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
1.2 application.yml配置
# es配置
spring:
data:
elasticsearch:
#集群名称
cluster-name: myes #这里的名字一定要和elasticsearch.yml中的一样
#集群地址
cluster-nodes: 101.37.146.240:9300
1.3 实体类层
/**
* @author: hejr
* @desc:
* @date: 2019/4/10 14:35
*/
@Data
@Document(indexName = "hszsd", type = "user")
public class UserEntity {
@Id
private String id;
private String name;
private Integer sex;
private Integer age;
}
1.4 Dao层
/**
* @author: hejr
* @desc:
* @date: 2019/4/10 14:37
*/
@Component
public interface UserRepository extends CrudRepository<UserEntity, String> {
}
1.5 Controller层
/**
* @author: hejr
* @desc:
* @date: 2019/4/10 14:37
*/
@RestController
@Slf4j
public class EsController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/addUser")
public UserEntity addUser(UserEntity user) {
return userRepository.save(user);
}
@RequestMapping("/findUser")
public Optional<UserEntity> findUser(String id) {
return userRepository.findById(id);
}
}
1.6 启动类配置
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.hejr.dao")
public class Es001Application {
public static void main(String[] args) {
SpringApplication.run(Es001Application.class, args);
}
}
1.7 启动项目
报错以下错误:
None of the configured nodes are available
解决方案:
vim /usr/local/elk/elasticsearch-6.7.0/config/elasticsearch.yml
cluster.name: myes
1.8 9200和9300区别
9300端口: ES节点之间通讯使用
9200端口: ES节点 和 外部 通讯使用
9300是TCP协议端口号,ES集群之间通讯端口号
9200端口号,暴露ES RESTful接口端口号
二. 源码下载
https://gitee.com/hejr.cn.com/elasticsearch_2019/tree/master