前置工作
- 當前服務器爲CentOS6.5+ 64bit
新建 elasticsearch用戶,ES無法用root啓動
- useradd -d /usr/elasticsearch -g elastic -m elasticsearch
獲取elasticsearch,本文用的v2.3.4 找到要獲取的版本
- ES已經有5.x版本,然而Spring data目前還只支持ES2.x版本,Springdata支持ES版本說明
瞭解ES與mysql的對應關係
- index –> DB
- type –> Table
- Document –> row
安裝Elasticsearch
用wget命令 獲取elasticsearch後,直接解壓到對應的目錄中
修改 config下的elasticsearch.yml
配置 cluster.name、network.host
cluster.name:nini
network.host 0.0.0.0 #爲任意都可連接
啓動ES,bin目錄下
sh elasticsearch
Springboot集成ES
新建ES module,引入 spring-boot-starter-data-elasticsearch、spring-data-elasticsearch
build.gradle
apply plugin: 'org.springframework.boot'
dependencyManagement{
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:" + springCloudVersion
mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion
}
}
repositories {
mavenCentral()
}
dependencies {
compile ('org.springframework.data:spring-data-redis')
compile ('org.springframework.boot:spring-boot-starter-data-mongodb:'+springBootVersion)
compile ('org.springframework.boot:spring-boot-starter-web:'+springBootVersion)
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile ('mysql:mysql-connector-java:'+mysqlVersion)
compile ('com.alibaba:druid:'+druidVersion)
compile ('org.mybatis:mybatis-spring:'+mybatisSpringBootVersion)
compile ('org.mybatis:mybatis:'+mybatisVersion)
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile ('org.springframework.boot:spring-boot-starter-thymeleaf')
compile ('net.sourceforge.nekohtml:nekohtml:'+nekoHtmlVersion)
compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion)
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-aop')
compile ('com.alibaba:fastjson:'+fastjsonVersion)
compile ('redis.clients:jedis')
compile ('org.springframework.boot:spring-boot-starter-data-elasticsearch:'+springBootVersion)
compile ('org.springframework.data:spring-data-elasticsearch')
testCompile ('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'junit', name: 'junit', version: '4.11'
}
configurations {
all*.exclude module: 'spring-boot-starter-logging'
all*.exclude module: 'logback-classic'
all*.exclude module: 'log4j-over-slf4j'
all*.exclude module: 'snappy-java'
}
jar {
baseName = 'es-server-bootcwenao'
}
在application.yml中配置ES信息
application.yml
spring:
data:
elasticsearch:
cluster-name: nini
cluster-nodes: 192.168.21.1:9300
local: false
repositories:
enable: true
創建 ES bean
用來查詢獲取對象,與mybatis差不多,唯獨多了@Document與@Id 註解
AccountInfo.java
/**
* @author cwenao
* @version $Id AccountInfo.java, v 0.1 2017-02-06 10:28 cwenao Exp $$
*/
@Document(indexName = "cwenao",type = "accountinfo", shards = 1,replicas = 0, refreshInterval = "-1")
public class AccountInfo {
@Id
private String id;
@Field
private String accountName;
@Field
private String nickName;
//getter setter ...
}
創建 Repository
用於數據查詢,需要extends ElasticsearchRepository
ElasticAccountInfoRepository.java
/**
* @author cwenao
* @version $Id ElasticAccountInfoRepository.java, v 0.1 2017-02-06 10:26 cwenao Exp $$
*/
@Component("elasticAccountInfoRepository")
public interface ElasticAccountInfoRepository extends ElasticsearchRepository<AccountInfo,String> {
//TODO define the search
AccountInfo findByAccountName(String accountName);
}
創建 service
ESAccountInfoService.java
/**
* @author cwenao
* @version $Id ESAccountInfoService.java, v 0.1 2017-02-06 10:36 cwenao Exp $$
*/
public interface ESAccountInfoService {
AccountInfo queryAccountInfoById(String id);
AccountInfo queryAccountInfoByName(String accountName);
}
ESAccountInfoServiceImpl.java
/**
* @author cwenao
* @version $Id ESAccountInfoServiceImpl.java, v 0.1 2017-02-06 10:38 cwenao Exp $$
*/
@Service("esAccountInfoServiceImpl")
public class ESAccountInfoServiceImpl implements ESAccountInfoService {
@Autowired
private ElasticAccountInfoRepository elasticAccountInfoRepository;
public AccountInfo queryAccountInfoById(String id) {
return elasticAccountInfoRepository.findOne(id);
}
@Override
public AccountInfo queryAccountInfoByName(String accountName) {
return elasticAccountInfoRepository.findByAccountName(accountName);
}
}
創建controller
ESController.java
/**
* @author cwenao
* @version $Id ESController.java, v 0.1 2017-02-06 10:44 cwenao Exp $$
*/
@Controller
public class ESController {
@Autowired
private ESAccountInfoService esAccountInfoServiceImpl;
@RequestMapping("/esAccountInfo")
public String queryAccountInfo(String id, ModelMap modelMap){
AccountInfo accountInfo = esAccountInfoServiceImpl.queryAccountInfoById(id);
modelMap.addAttribute("esAccountInfo",accountInfo);
modelMap.addAttribute("test_elastic","Test the elasticsearch");
return "accountInfo";
}
@RequestMapping("/esAccountInfoName")
public String queryAccountInfoByAccountName(String accountName, ModelMap modelMap){
AccountInfo accountInfo = esAccountInfoServiceImpl.queryAccountInfoByName(accountName);
modelMap.addAttribute("esAccountInfo",accountInfo);
modelMap.addAttribute("test_elastic","Test the elasticsearch");
return "accountInfo";
}
}
在apigateway工程配置path
bootstrap.yml
zuul:
routes:
esserver:
path: /esserver/**
serviceId: ESSERVER
apigateway/mybatis 等集成參考前面文章
測試
- 依次啓動 discovery、configserver、apigateway、esserver
- 訪問 http://localhost:10002/esserver/esAccountInfo?id=a63126d074f04db587cd76a48c817510
更多的查詢方式
- ElasticsearchRepository提供了很有用的查詢構造器
- 比如查詢 去重distinct : findDistinctAccountInfoById(String id)
- 比如條件組合 **AND/OR : findAccountInfoByIdAndAccountname(String id,accountname)
- 查詢構造器會忽略 find..By 等前綴
代碼
代碼請移步 Github參考地址
如有疑問請加公衆號(K171),如果覺得對您有幫助請 github start