需要执行下面步骤
https://www.elastic.co/guide/en/shield/current/_using_elasticsearch_java_clients_with_shield.html
1:在java项目中的pom文件中添加maven依赖
<repositories> <!-- add the elasticsearch repo --> <repository> <id>elasticsearch-releases</id> <url>https://maven.elasticsearch.org/releases</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <!-- add the shield jar as a dependency --> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>shield</artifactId> <version>2.4.3</version> </dependency>
注意:shield的maven依赖版本必须是2.4.3的,官方例子写的是2.2.0的无法正常运行。因为我们的shield安装的版本也是2.4.3、要保持版本一致。
2:代码如下:
Settings settings = Settings.settingsBuilder() .put("cluster.name", "elasticsearch") //设置shield创建的用户名和密码 .put("shield.user", "es_admin:123456") .build(); TransportClient client = TransportClient.builder() //添加shield plugin .addPlugin(ShieldPlugin.class) .settings(settings).build(); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.80.100"), 9300));