一、構建模塊
1.構建搜索微服務子模塊
(1)創建模塊tensquare_search ,pom.xml引入依賴
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.tensquare</groupId>
<artifactId>tensquare_common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.tensquare</groupId>
<artifactId>tensquare_common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
(2)新建配置文件application.yml
server:
port: 9007
spring:
application:
name: tensquare-search #指定服務名
data:
elasticsearch:
cluster‐nodes: 127.0.0.1:9300
(3)創建包com.tensquare.search ,包下創建啓動類
package com.tensquare.search;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
@SpringBootApplication
public class RecruitApplication {
public static void main(String[] args) {
SpringApplication.run(RecruitApplication.class, args);
}
@Bean
public IdWorker idWorker(){
return new IdWorker(1, 1);
}
}
2. 添加文章
(1)創建實體類
創建com.tensquare.search.pojo包,包下建立類
/**
* 文章實體類
*/
@Document(indexName="tensquare",type="article")
public class Article implements Serializable {
@Id
private String id;//ID
@Field(index= true
,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String title;//標題
@Field(index= true
,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String content;//文章正文
private String state;//審覈狀態
//getter and setter ......
}
(2)創建數據訪問接口
創建com.tensquare.search.dao包,包下建立接口
/**
* 文章數據訪問層接口
*/
public interface ArticleSearchDao extends
ElasticsearchRepository<Article,String> {
}
(3)創建業務邏輯類
創建com.tensquare.search.service包,包下建立類
@Service
public class ArticleSearchService {
@Autowired
private ArticleSearchDao articleSearchDao;
/**
* 增加文章
* @param article
*/
public void add(Article article){
articleSearchDao.save(article);
}
}
(4)創建控制器類
創建com.tensquare.search.controller包,包下建立類
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleSearchController {
@Autowired
private ArticleSearchService articleSearchService;
@RequestMapping(method= RequestMethod.POST)
public Result save(@RequestBody Article article){
articleSearchService.add(article);
return new Result(true, StatusCode.OK, "操作成功",null);
}
}
3.文章搜索
(1)ArticleSearchDao新增方法定義
/**
* 檢索
* @param
* @return
*/
public Page<Article> findByTitleOrContentLike(String title, String
content, Pageable pageable);
(2)ArticleSearchService新增方法
public Page<Article> findByTitleLike(String keywords, int page, int size)
{
PageRequest pageRequest = PageRequest.of(page-1, size);
return articleSearchDao.findByTitleOrContentLike(keywords,keywords, pageRequest);
}
(3)ArticleSearchController方法
/**
* 搜索文章
* @param keywords
* @param page
* @param size
* @return
*/
@RequestMapping(value="/search/{keywords}/{page}/{size}",method= RequestMethod.GET)
public Result findByTitleLike(@PathVariable String keywords,
@PathVariable int page, @PathVariable int size){
Page<Article> articlePage =
articleSearchService.findByTitleLike(keywords,page,size);
return new Result(true, StatusCode.OK, "查詢成功",
new PageResult<Article>(articlePage.getTotalElements(),
articlePage.getContent()));
}
二、elasticsearch與MySQL數據同步
1.什麼是Logstash
Logstash是一款輕量級的日誌蒐集處理框架,可以方便的把分散的、多樣化的日誌蒐集
起來,並進行自定義的處理,然後傳輸到指定的位置,比如某個服務器或者文件。
2. Logstash安裝與測試
解壓,進入bin目錄,在控制檯輸入
logstash -e 'input { stdin { } } output { stdout {} }'
控制檯輸入字符,隨後就有日誌輸出
stdin,表示輸入流,指從鍵盤輸入
stdout,表示輸出流,指從顯示器輸出
命令行參數:
-e 執行
–config 或 -f 配置文件,後跟參數類型可以是一個字符串的配置或全路徑文件名或全路徑
路徑(如:/etc/logstash.d/,logstash會自動讀取/etc/logstash.d/目錄下所有*.conf 的文
本文件,然後在自己內存裏拼接成一個完整的大配置文件再去執行)
3.MySQL數據導入Elasticsearch
(1)在logstash-5.6.8安裝目錄下創建文件夾mysqletc (名稱隨意)
(2)文件夾下創建mysql.conf (名稱隨意) ,內容如下:
input {
jdbc {
# mysql jdbc connection string to our backup databse
jdbc_connection_string => "jdbc:mysql://192.168.192.130:3306/tensquare_article?characterEncoding=UTF8&&useSSL=false"
# the user we wish to excute our statement as
jdbc_user => "root"
jdbc_password => "123456"
# the path to our downloaded jdbc driver
jdbc_driver_library => "C:\Users\admin\Desktop\code\any\mysqletc"
# the name of the driver class for mysql
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
#以下對應着要執行的sql的絕對路徑。
#statement_filepath => ""
statement => "select id,title,content from tb_article"
#定時字段 各字段含義(由左至右)分、時、天、月、年,全部爲*默認含義爲每分鐘都更新(測試結果,不同的話請留言指出)
schedule => "* * * * *"
}
}
output {
elasticsearch {
#ESIP地址與端口
hosts => "localhost:9200"
#ES索引名稱(自己定義的)
index => "tensquare"
#自增ID編號
document_id => "%{id}"
document_type => "article"
}
stdout {
#以JSON格式輸出
codec => json_lines
}
}
(3)將mysql驅動包mysql-connector-java-5.1.46.jar拷貝至C:\Users\admin\Desktop\code\any\logstash-5.6.8\mysqletc下 。C:\Users\admin\Desktop\code\any\logstash-5.6.8是你的安裝目錄
(4)命令行下執行
logstash ‐f ../mysqletc/mysql.conf
觀察控制檯輸出,每間隔1分鐘就執行一次sql查詢。