之所以说是最简单,是因为这样能搭建出一个能跑出来结果的框架,而更高级的功能往往就是在最简单的框架的基础上建设的,对吧。
客户端环境:spring boot 1.4.1
ElasticSearch环境:Elasticsearch2.4.0 on Windows 2008R2 100.2.92.100:9300
POM:
4.0.0
miku.ltd
esl
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.4.1.RELEASE
org.springframework.boot
spring-boot-maven-plugin
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.data
spring-data-elasticsearch
2.0.4.RELEASE
为了更简单的在spring boot中使用es,直接依赖spring-boot-starter-data-elasticsearch就可以了。
application.properties
server.port=12001
logging.level.org.springframework=INFO
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=100.2.92.100:9300
spring.data.elasticsearch.local=false
spring.data.elasticsearch.repositories.enabled=true
AppMain.java
package ltd.miku.esl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@SpringBootApplication
//@EnableElasticsearchRepositories(basePackages = {"ltd.miku.esl.dao"})
public class AppMain implements CommandLineRunner
{
public static ConfigurableApplicationContext ctx;
private static final Logger LOG = LoggerFactory.getLogger(AppMain.class);
@Override
public void run(String... strings) throws Exception
{
}
public static void main(String[] args)
{
ctx = SpringApplication.run(AppMain.class, args);
LOG.info(" application running...");
}
}
EslController.java
package ltd.miku.esl.controller;
import ltd.miku.esl.service.EslService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/esl/")
public class EslController
{
@Autowired
private EslService esl;
private static final Logger LOG = LoggerFactory.getLogger(EslController.class);
@RequestMapping(value = "test", method = RequestMethod.POST)
public Object test(@RequestBody String id)
{
return esl.findCliente(id);
}
}
ClienteRepository.java
package ltd.miku.esl.dao;
import ltd.miku.esl.model.Cliente;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ClienteRepository extends ElasticsearchRepository
{
}
跟JPA或者mongo的DAO差不多是一样的。
Cliente.java
package ltd.miku.esl.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* the bean of customer
*/
@Document(indexName = "customer", type = "external", shards = 1, replicas = 0, refreshInterval = "-1")
public class Cliente
{
@Id
private String id;
private long account_number;
private long balance;
private String firstname;
private String lastname;
private int age;
private String gender;
private String address;
private String employer;
private String email;
private String city;
private String state;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public long getAccount_number()
{
return account_number;
}
public void setAccount_number(long account_number)
{
this.account_number = account_number;
}
public long getBalance()
{
return balance;
}
public void setBalance(long balance)
{
this.balance = balance;
}
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getLastname()
{
return lastname;
}
public void setLastname(String lastname)
{
this.lastname = lastname;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getEmployer()
{
return employer;
}
public void setEmployer(String employer)
{
this.employer = employer;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
public String getState()
{
return state;
}
public void setState(String state)
{
this.state = state;
}
@Override
public String toString()
{
return "Cliente{" + "id='" + id + '\'' + ", account_number=" + account_number + ", balance=" + balance + ", firstname='" + firstname + '\'' + ", lastname='" + lastname + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", address='" + address + '\'' + ", employer='" + employer + '\'' + ", email='" + email + '\'' + ", city='" + city + '\'' + ", state='" + state + '\'' + '}';
}
}
EslService.java
package ltd.miku.esl.service;
import ltd.miku.esl.model.Cliente;
public interface EslService
{
Cliente findCliente(String id);
}
EslServiceImpl.java
package ltd.miku.esl.service.impl;
import ltd.miku.esl.dao.ClienteRepository;
import ltd.miku.esl.model.Cliente;
import ltd.miku.esl.service.EslService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EslServiceImpl implements EslService
{
@Autowired
private ClienteRepository clienteDao;
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(EslServiceImpl.class);
@Override
public Cliente findCliente(String id)
{
Cliente cliente = clienteDao.findOne(id);
LOG.info(" get cliente by id {} is {}", id, cliente);
return cliente;
}
}
记录问题:
1,在开发环境搭建的过程中,总是报错:
Field clienteDao in ltd.miku.esl.service.impl.EslServiceImpl required a bean named 'elasticsearchTemplate' that could not be found
然后看看代码和POM,貌似配置也没问题,后来发现了maven依赖有问题:
spring-data-elasticsearch-2.0.4.RELEASE.jar里点名的依赖是2.2.0,但是idea的依赖中说缺少2.4.0
虽然不知道为啥但是在私服中加上2.4.0版本的就好用了。
2,早起开发的时候一直遇到连接不到ES节点的异常,但是用ES服务器的9200端口进行http访问就能正常连接,异常信息如下:
16:33:54.196 [elasticsearch[Shuma-Gorath][generic][T#2]] INFO org.elasticsearch.client.transport.internalInfo:124 - [Shuma-Gorath] failed to get node info for [#transport#-1][clauspc][inet[/100.2.92.100:9300]], disconnecting...
org.elasticsearch.transport.NodeDisconnectedException: [][inet[/100.2.92.100:9300]][cluster:monitor/nodes/info] disconnected
16:33:59.221 [elasticsearch[Shuma-Gorath][generic][T#2]] INFO org.elasticsearch.client.transport.internalInfo:124 - [Shuma-Gorath] failed to get node info for [#transport#-1][clauspc][inet[/100.2.92.100:9300]], disconnecting...
org.elasticsearch.transport.NodeDisconnectedException: [][inet[/100.2.92.100:9300]][cluster:monitor/nodes/info] disconnected
16:34:04.245 [elasticsearch[Shuma-Gorath][generic][T#2]] INFO org.elasticsearch.client.transport.internalInfo:124 - [Shuma-Gorath] failed to get node info for [#transport#-1][clauspc][inet[/100.2.92.100:9300]], disconnecting...
org.elasticsearch.transport.NodeDisconnectedException: [][inet[/100.2.92.100:9300]][cluster:monitor/nodes/info] disconnected