Client - XPack Transport Client

优质
小牛编辑
129浏览
2023-12-01

XPackTransportClient

如果 ElasticSearch 服务安装了 x-pack 插件,需要PreBuiltXPackTransportClient实例才能访问

使用Maven管理项目,把下面代码增加到pom.xml;

一定要修改默认仓库地址为https://artifacts.elastic.co/maven ,因为这个库没有上传到Maven中央仓库

  1. <project ...>
  2. <repositories>
  3. <!-- add the elasticsearch repo -->
  4. <repository>
  5. <id>elasticsearch-releases</id>
  6. <url>https://artifacts.elastic.co/maven</url>
  7. <releases>
  8. <enabled>true</enabled>
  9. </releases>
  10. <snapshots>
  11. <enabled>false</enabled>
  12. </snapshots>
  13. </repository>
  14. ...
  15. </repositories>
  16. ...
  17. <dependencies>
  18. <!-- add the x-pack jar as a dependency -->
  19. <dependency>
  20. <groupId>org.elasticsearch.client</groupId>
  21. <artifactId>x-pack-transport</artifactId>
  22. <version>5.6.3</version>
  23. </dependency>
  24. ...
  25. </dependencies>
  26. ...
  27. </project>

实例

  1. Settings settings = Settings.builder().put("cluster.name", "xxx")
  2. .put("xpack.security.transport.ssl.enabled", false)
  3. .put("xpack.security.user", "xxx:xxx")
  4. .put("client.transport.sniff", true).build();
  5. try {
  6. client = new PreBuiltXPackTransportClient(settings)
  7. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("xxx.xxx.xxx.xxx"), 9300))
  8. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("xxx.xxx.xxx.xxx"), 9300));
  9. } catch (UnknownHostException e) {
  10. e.printStackTrace();
  11. }

更多请浏览 dayu-spring-boot-starter 开源项目