SpringBoot -- 集成Elasticsearch

前置工作


  • 当前服务器为CentOS6.5+ 64bit
  • 新建 elasticsearch用户,ES无法用root启动

  • useradd -d /usr/elasticsearch -g elastic -m elasticsearch

安装Elasticsearch

用wget命令 获取elasticsearch后,直接解压到对应的目录中

修改 config下的elasticsearch.yml
配置 cluster.namenetwork.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 等集成参考前面文章

测试

这里写图片描述

更多的查询方式

  • ElasticsearchRepository提供了很有用的查询构造器
  • 比如查询 去重distinct : findDistinctAccountInfoById(String id)
  • 比如条件组合 **AND/OR : findAccountInfoByIdAndAccountname(String id,accountname)
  • 查询构造器会忽略 find..By 等前缀

代码

代码请移步 Github参考地址

如有疑问请加公众号(K171),如果觉得对您有帮助请 github start
公众号_k171

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值