Es spring data jpa 修改连接配置

使用Spring Data Elasticsearch连接elasticsearch时,正常情况下只需要在application.properites文件中添加如下配置即可连接:

spring.data.elasticsearch.repositories.enabled = true
spring.data.elasticsearch.cluster-name = es-cluster
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300

以看到Spring Data Elasticsearch连接elasticsearch很简单。

上面说的是正常情况,但是有些情况下连接需要验证。比如安装x-pack(旧版本的Shield、Marvel等)之后,访问elasticsearch就需要验证(未开启匿名访问),默认用户名/密码是:elastic/changeme,参考官方文档了解更多细节。

如果不做相应的修改就会报错

org.elasticsearch.transport.RemoteTransportException: [i9Vl_pG][10.111.27.202:9300][cluster:monitor/nodes/liveness]
Caused by: org.elasticsearch.ElasticsearchSecurityException: missing authentication token for action [cluster:monitor/nodes/liveness]
	at org.elasticsearch.xpack.security.support.Exceptions.authenticationError(Exceptions.java:36) ~[na:na]
	at org.elasticsearch.xpack.security.authc.DefaultAuthenticationFailureHandler.missingToken(DefaultAuthenticationFailureHandler.java:74) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$AuditableTransportRequest.anonymousAccessDenied(AuthenticationService.java:541) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$handleNullToken$17(AuthenticationService.java:357) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.handleNullToken(AuthenticationService.java:362) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.consumeToken(AuthenticationService.java:277) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$extractToken$7(AuthenticationService.java:249) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.extractToken(AuthenticationService.java:266) ~[na:na]
	at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$null$0(AuthenticationService.java:201) ~[na:na]
	at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:60) ~[elasticsearch-5.6.8.jar:5.6.8]
	at org.elasticsearch.xpack.security.authc.TokenService.getAndValidateToken(TokenService.java:239) ~[na:na]


在这样的情况下,Spring Data Elasticsearch 该如何设置才能连接到 elasticsearch?目前最新版的 Spring Data Elasticsearch 2.1.6.Release 还不能通过配置的形式来完成。那要如何做,更换TransportClient即可!

修改依赖

<repository>
    <id>elasticsearch-releases</id>
    <url>https://artifacts.elastic.co/maven</url>
    <releases>
        <enabled>true</enabled>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>
...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>x-pack-transport</artifactId>
    <version>5.5.0</version>
</dependency>

x-pack-transport 的版本号保持与elasticsearch一致。

重新实例化 TransportClient

@Bean
public TransportClient transportClient() throws UnknownHostException {
    TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
            .put("cluster.name", "es-cluster")
            .put("xpack.security.user", "elastic:changeme")
            .build())
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
    return client;
    }

就这么多,之前属性文件中的配置可以删除了,这样就能保证连接验证通过。

转载至https://my.oschina.net/kipeng/blog/1800523

上一篇:【题解】排列


下一篇:ICPC North Central NA Contest 2017