最近刚好一个项目,由于其他原因,需要将elaticsearch集群加上账号密码访问(之前一直都是直接访问elaticsearch,未加账号密码,所用到的elaticsearch版本号2.4.0)。
maven依赖
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>shield</artifactId>
<version>2.4.0</version>
</dependency>
elasticsearch.properties配置文件
#es集群名称
es.cluster.name=flowaudit
es.client.transport.sniff=false
#es服务器ip以及tcp端口
#es.transport.address.list=127.0.0.1:9301
#es 账号密码
es.shield.user=root:surfilter@1218
#es tcp端口
es.transport.port=9301
#es 索引名称
es.index.name=app
es.index.type=appinfo
applicationContext-es.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<description>elasticsearch索引配置</description>
<!--配置esManager的名称以及路径-->
<bean id="esManager" class="com.surfilter.pscms.app.market.manager.ESManager">
<constructor-arg ref="esClient"/>
</bean>
</beans>
配置好以后,在其他地方之间采用spring的自动注入方法(如下),即可正常操作elaticsearch
@Autowired
private ESManager esManager;
esManager代码文件以及其他相关代码文件参考附件下载。