前置工作
- 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:nininetwork.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'}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
在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; }
创建 Repository
用于数据查询,需要extends ElasticsearchRepository
ElasticAccountInfoRepository.java
@Component("elasticAccountInfoRepository")public interface ElasticAccountInfoRepository extends ElasticsearchRepository<AccountInfo,String> { 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); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
创建controller
ESController.java
/** * @author cwenao * @version $Id ESController.java, v 0.1 2017-02-06 10:44 cwenao Exp $$ */@Controllerpublic 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"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
在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