spring-boot学习笔记(三)ElasticSearch介绍与项目集成

一、ElasticSearch介绍



分布式高可用,例如将一个系统部署在一台服务器上面,某一天崩溃了,该系统就无法使用了,但是当分布式部署到多台服务器上面之后,其中几个服务器崩溃掉了,但是不会影响整体的运作,从而实现高可用。


二、ElasticSearch项目集成

1、build.gradle添加es依赖和jna依赖,application.properties配置文件添加es服务地址和连接超时时间


  
  1. dependencies {
  2. compile('org.springframework.boot:spring-boot-starter-web')
  3. compile('org.springframework.boot:spring-boot-starter-thymeleaf')
  4. compile('org.springframework.boot:spring-boot-starter-data-jpa')
  5. compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')
  6. runtime('com.h2database:h2:1.4.196')
  7. testCompile('org.springframework.boot:spring-boot-starter-test')
  8. compile('mysql:mysql-connector-java:6.0.5')
  9. compile('net.java.dev.jna:jna:4.5.1')
  10. }

  
  1. #elasticSearch服务地址
  2. spring.data.elasticsearch.cluster-nodes=localhost:9300
  3. #设置连接超时时间
  4. spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s

2、下载elasticsearch-6.3.0,解压到d:盘下,打开D:\elasticsearch-6.3.0\bin ,点击elasticsearch.bat 启动es服务,

访问本地9200端口,看到es版本信息说明启动成功。

3、编写测试用例

新建EsBlog实体类


  
  1. */
  2. @Document(indexName="blog",type="blog")
  3. public class EsBlog implements Serializable {
  4. /**
  5. *
  6. */
  7. private static final long serialVersionUID = 159383211493905568L;
  8. @Id
  9. private String id;
  10. private String title;
  11. private String summary;
  12. private String content;

编写接口继承ElasticsearchRepository


  
  1. public interface EsBlogRepository extends ElasticsearchRepository<EsBlog, String> {
  2. /**
  3. * 分页查询博客去重
  4. * @param title
  5. * @param summary
  6. * @param content
  7. * @param pageable
  8. * @return Page<EsBlog>
  9. */
  10. Page<EsBlog> findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(String title,String summary,String content,Pageable pageable);
  11. }

编写测试test代码


  
  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class EsBlogRepositoryTest {
  4. @Autowired
  5. private EsBlogRepository esBlogRepository;
  6. public void initRepositoryData() {
  7. /**
  8. * 删除所有数据
  9. */
  10. esBlogRepository.deleteAll();
  11. /**
  12. * 初始化三个blog
  13. */
  14. esBlogRepository.save(new EsBlog("1", "段", "段朝旭"));
  15. esBlogRepository.save(new EsBlog("2", "朝", "段朝旭"));
  16. esBlogRepository.save(new EsBlog("3", "旭", "段朝旭"));
  17. }
  18. /**
  19. * 分页查询博客去重
  20. *
  21. * @param title
  22. * @param summary
  23. * @param content
  24. * @param pageable
  25. * @return Page<EsBlog>
  26. */
  27. @Test
  28. public void findDistinctEsBlogByTitleContaingOrSummaryContaingOrContentContaing() {
  29. initRepositoryData();
  30. Pageable pageable = PageRequest.of(0, 20);
  31. Page<EsBlog> page = esBlogRepository
  32. .findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining("1", "旭", "王", pageable);
  33. System.out.println(page.getTotalElements());
  34. for (EsBlog blog : page.getContent()) {
  35. System.out.println(blog);
  36. }
  37. }

运行测试代码之后看到控制台打印出


说明执行成功。

4、编写controller层


  
  1. @RestController
  2. @RequestMapping("/blog")
  3. public class EsBlogController {
  4. @Autowired
  5. private EsBlogRepository esBlogRepository;
  6. @GetMapping
  7. public List<EsBlog> list(@RequestParam(value="title")String title,
  8. @RequestParam(value="summary")String summary,
  9. @RequestParam(value="content")String content,
  10. @RequestParam(value="pageIndex",defaultValue="0")int pageIndex,
  11. @RequestParam(value="pageSize",defaultValue="10")int pageSize){
  12. Pageable pageable = PageRequest.of(pageIndex,pageSize);
  13. Page<EsBlog> page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title,summary,content,pageable);
  14. return page.getContent();
  15. }
  16. }

访问如图所示的地址,可看到返回的json格式数据


文章来源: blog.csdn.net,作者:血煞风雨城2018,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_31905135/article/details/80856073

(完)