昨天给大家介绍了一下ElasticSearch代码结构设计,今天给大家介绍一下ElasticSearch怎么集成到Spring boot中,不知道大家记不记得以前我也写过它们的整合,但今天要说的模式和以前的不一样(具体区别大家可以对照的看看,这边就不详细介绍了)。
1.我们首先引入ElasticSearch的相关依赖,pom文件的依赖如下所示:
<!-- elasticsearch client依赖 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.2.2</version> </dependency> <!-- elasticsearch依赖 --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.2.2</version> </dependency>
2.然后再配置文件中配置ElasticSearch的相关配置,yml配置文件如下所示:
elasticsearch: cluster: name: my-application address: 192.168.11.24:9300
3.创建一个配置类(@Configuration),然后添加ElasticSearch相关的连接信息,配置类如下所示:
package com.infun.platform.es.config; import org.apache.commons.lang.StringUtils; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; /** * Elasticsearch配置类 * @author linzhiqiang * @date 2018/5/26 * @since 1.0 */ @Configuration public class ElasticsearchConfig { private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class); @Value("${elasticsearch.cluster.name}") private String clusterName; @Value("${elasticsearch.address}") private String address; @Bean public TransportClient getTransportClient() { try { // 设置集群名称,并自动嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中 // settings Settings settings = Settings.builder().put("cluster.name", clusterName) // .put("index.number_of_shards", 1) // .put("index.number_of_replicas", 0) .build(); //地址列表 List<TransportAddress> transportAddressList = new ArrayList<>(); if (StringUtils.isNotBlank(address)) { String[] addresses = address.split(","); String[] hostAndPort = null; //组装地址 for (String str : addresses) { hostAndPort = str.split(":"); if (hostAndPort != null && hostAndPort.length > 1) { transportAddressList.add(new TransportAddress(InetAddress.getByName(hostAndPort[0].trim()), Integer.valueOf(hostAndPort[1].trim()))); } else { transportAddressList.add(new TransportAddress(InetAddress.getByName(hostAndPort[0].trim()), 9300)); } } } //转换 TransportAddress[] transportAddresses = transportAddressList.toArray(new TransportAddress[transportAddressList.size()]); //返回连接 return new PreBuiltTransportClient(settings).addTransportAddresses(transportAddresses); } catch (UnknownHostException e) { e.printStackTrace(); LOGGER.error(e.getMessage()); } return null; } }
4.配置类搞定,我们就可以在service中注入TransportClient 的连接了,使用方式如下所示:
/** * TransportClient连接 */ @Autowired private TransportClient getTransportClient;
总结:
过程就是这样,这样的整合方式和之前的整合方式最大的区别就是,现在这种的只是将elasticsearch的配置信息放在yml配置文件中,elasticsearch代码风格还是一样的,但是spring-data-elasticsearch这种的就不一样了,它的代码风格就像JPA这种模式了,虽然封装的很好,但是很不灵活。两者之间各自有各自的优缺点吧,看具体应用场景适合什么。最后我们就可以结合上一节课讲到ElasticSearch代码结构设计,然后写具体的增删改查命令了。
想要更多干货、技术猛料的孩子,快点拿起手机扫码关注我,我在这里等你哦~