栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Springboot中集成ElasticSearch,mysql语句优化面试

Springboot中集成ElasticSearch,mysql语句优化面试

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.ne

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

开源完整内容戳这里

t.UnknownHostException;

import java.util.ArrayList;

import java.util.List;

import java.util.ResourceBundle;

@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 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 的连接了,使用方式如下所示:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584931.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号