SpringBoot + Elasticsearch5.x

@Component
@Configuration
public class ElasticsearchConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(ElasticsearchConfiguration.class);
    @Value("${es.cluster.nodes}")
    private String clusterNodes ;
    @Value("${es.cluster.name}")
    private String clusterName;
    private TransportClient client;

    @Bean
    public TransportClient init(){
        logger.info("Building ElasticSearch client");
        Settings settings = Settings.builder().put("cluster.name", clusterName).build();
        client = new PreBuiltTransportClient(settings);
        try {
            if (!"".equals(clusterNodes)){
                for (String nodes:clusterNodes.split(",")) {
                    String InetSocket[] = nodes.split(":");
                    String address = InetSocket[0];
                    Integer port = Integer.valueOf(InetSocket[1]);
                    client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(address), port));
                }
            }
        } catch (Exception e) {
            logger.error("Error build ElasticSearch client: {}", e.getMessage());
        }
        return client;
    }

    @PreDestroy
    public void destory() {
        if (client != null){
            client.close();
        }
    }
}


项目源码:点击打开链接

你可能感兴趣的:(NoSQL)