spring-data-elasticsearch和shield插件结合

来源:互联网 发布:mrt数据导入导出怎么做 编辑:程序博客网 时间:2024/06/15 01:51
      在springboot项目中使用elasticsearch,可以直接使用spring-data集成elasticsearch的方式,也就是这个依赖
<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>
      不过目前此种集成方式只支持到elasticsearch2.4版本,接下来的操作都是基于elasticsearch2.x版本来说的。引入依赖后,在springboot项目配置文件application.properties中写入elasticsearch节点信息,一下为主要配置信息:
#cluster-name: #默认为elasticsearchspring.data.elasticsearch.cluster-name=elasticsearch#cluster-nodes: 112.74.72.18:9300 #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNodespring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
       然后在controller中直接注入ElasticsearchTemplate就可以调用api开发了:
@Autowiredpublic ElasticsearchTemplate elasticsearchTemplate;

       因为采用的是集成的方式,spring-data-elasticsearch底层直接将配置文件中的配置注入到模板中了,所以什么都不用管了;除非你elasticsearch服务端安装了shield插件,因为安全管理很重要,所以必须安装shield插件;在上一篇博客中我说我们公司maven仓库没有shield依赖,最后改用elasticsearch5.x版本采用x-pack安全管理。但是我还是想知道假如可以用shield依赖,那该如何配置呢。所以我在网上将shield依赖下载下来直接放入classpath中,该插件在maven中央仓库坐标如下:

<dependency>   <groupId>org.elasticsearch.plugin</groupId>   <artifactId>shield</artifactId>   <version>2.4.6</version>   <scope>provided</scope></dependency>
      当你elasticsearch中安装了shield插件后,如果你没有在客户端连接中配置shield安全信息,就会报下面的错:

Caused by: org.elasticsearch.ElasticsearchSecurityException: missing authentication token for action [cluster:monitor/nodes/liveness]

      而要配置shield安全信息,就要重写模板中的client信息,即ElasticsearchTemplate中的客户端不能再用程序默认注入的client了,而是我们要手动注入并添加shield信息。所以要在项目里加一个配置类:

@Configurationpublic class EsConfig {    @Bean    public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {        Client client = create();        return new ElasticsearchTemplate(client);//重写模板中的client    }    public Client create() throws UnknownHostException {        Settings settings = Settings.settingsBuilder()                .put("cluster.name", "elasticsearch")                .put("shield.user", "es_admin:123456") //使用#esusers useradd命令添加的用户和密码                .build();        Client client = TransportClient.builder()                .addPlugin(ShieldPlugin.class) //shield依赖里面的类                .settings(settings).build()                .addTransportAddress(new                        InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));        return client;    }}
       这样做以后,因为目前没有发现在配置文件中可以配置shield的属性,所以我们要把之前在application.properties中配置的elasticsearch信息注释掉,要不然程序又会使用默认的client导致重写的不生效。再次启动后报了一个新错误:java.lang.ClassNotFoundException: com.sun.jna.Native

       明显是缺少jna的依赖,至于为什么需要这个依赖,网上有人说是如果运行的是嵌入式节点就必须要jna;关于嵌入式节点大家可以搜索elasticsearch的两种客户端连接方式(嵌入式和tansportclient方式)来进行更详细的学习。但是我这里虽然重写了模板中的client,但是我的client明明更像是tansportclient方式,并不是嵌入式的连接方式:

//启动Node node= nodeBuilder().node();Client client=node.client();//关闭node.close();
       所以这里还不太明白,希望有人能帮我解答。不管怎么样,加入了jna依赖后,启动成功,搜索功能调用都正常,当然生产环境不会这样用的。期待官方尽快把spring-data-elasticsearch更新到支持elasticsearch5.x,那时或许配置安全信息会更方便。以下是jna的依赖坐标:
<dependency>   <groupId>com.sun.jna</groupId>   <artifactId>jna</artifactId>   <version>3.0.9</version></dependency>

     参考博客:http://blog.csdn.net/napoay/article/details/52201558

                       http://www.cnblogs.com/bmaker/p/5731320.html

                       http://blog.csdn.net/tianyaleixiaowu/article/details/72846814

             https://discuss.elastic.co/t/org-elasticsearch-bootstrap-jna-not-found-native-methods-will-be-disabled/30030

原创粉丝点击