博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring 通过EsClientFactory注入elasticsearch
阅读量:6813 次
发布时间:2019-06-26

本文共 3161 字,大约阅读时间需要 10 分钟。

hot3.png

spring-es.xml

EsClientFactory.java

package com.es.esClientFactory;import java.net.InetAddress;import java.net.UnknownHostException;import org.elasticsearch.client.Client;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.transport.client.PreBuiltTransportClient;import org.es.conf.EsConf;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class EsClientFactory {		@Autowired	private EsConf esConf;	@Bean(name = "esClient")	public Client getESClient() {		Settings settings = Settings.builder()				.put("cluster.name", esConf.getClusterName()).build();				Client client = new PreBuiltTransportClient(settings);		for (String ip : esConf.getIps().split(",")) {			try {				((TransportClient) client)						.addTransportAddress(new InetSocketTransportAddress(								InetAddress.getByName(ip), 9300));			} catch (UnknownHostException e) {				e.printStackTrace();			}		}				return client;	}}

EsServiceImpl.java

package org.taian.service.impl;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.client.Client;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Service;@Servicepublic class EsServiceImpl {		@Autowired 	@Qualifier("esClient")	private Client client;		public GetResponse testes(){		GetResponse response = client.prepareGet("test", "t5", "285309").execute().actionGet();		System.out.println(response.toString());		System.out.println("configuration");		System.out.println(response.getSourceAsString());		return null;	}}

ESContoller.java

package org.taian.web;import java.util.Date;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.search.SearchResponse;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.taian.service.ElasticsearchService;import org.taian.service.impl.EsServiceImpl;import org.taian.utils.HttpRequestUtil;import org.taian.utils.JSONUtils;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;@Controller@RequestMapping("/myes")public class ESController {		private final Logger logger = LoggerFactory.getLogger(this.getClass());		@Autowired	EsServiceImpl estest;		@ResponseBody	@RequestMapping(value="testes", method = { RequestMethod.GET, RequestMethod.POST }, produces="application/json")	public JSONObject testes(){		estest.testes();		return JSON.parseObject("");	}}

 

转载于:https://my.oschina.net/duanvincent/blog/1162690

你可能感兴趣的文章
常见的内存错误
查看>>
关于Ext checkboxfiled 获取值为 on的解决办法
查看>>
C#对IE使用Proxy(代理)
查看>>
sign签名算法一致算法-.net、java、golang
查看>>
Node.js简介与架构
查看>>
Entity Framework 异常档案
查看>>
如何成功发布一个MSMQ的Windows服务
查看>>
EntLib 3.1学习笔记(5) : Exception Handling Application Block
查看>>
工厂模式 接口 封装 实例
查看>>
bzoj1061 志愿者招募
查看>>
p2093 [国家集训队]JZPFAR
查看>>
枚举当前打开的所有窗口
查看>>
【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)...
查看>>
跨域问题
查看>>
GDI+中发生一般性错误的解决办法(转)
查看>>
MVC过滤器详解
查看>>
并发相关随笔(持续更新)
查看>>
iOS开发之 WebView
查看>>
【Selenium】1.介绍 Selenium
查看>>
NodeJS stream 一:Buffer
查看>>