java通过shield链接Elasticsearch

December 09, 2023
测试
测试
测试
测试
12 分钟阅读

本文mark了springboot中集成elasticsearch,并且实现连接带有shield权限管理的elasticsearch的方法。

tips:首先建议java client版本和elasticsearch版本一致。不然可能会出现各种问题。

1、首先在pom中加入如下依赖

<repositories>
        <repository>
            <id>elasticsearch-releases</id>
            <url>https://maven.elasticsearch.org/releases</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
   </repositories>
<dependencies>
        <dependency>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>shield</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.3.3</version>
        </dependency>  
    </dependencies>
  

2、其次编写ESClient类,实现ES连接

1)这里需要注意的是语法TransportClient.builder().addPlugin(ShieldPlugin.class),激活shield插件,不然还是会爆noNodesAvailable,小心。

2)java连接elasticsearch和python不一样,java使用的tcp端口,一般是8300,而python使用的是web端口,一般是9300。

3)一般会关闭嗅探,sniff=false。不然可能会报noNodesAvailable。

package com.xiaoju.dqa.sentinel.client.es;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.shield.ShieldPlugin;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;


public class ESClient {

    private TransportClient client;
    private String cluster;
    private String addressListString;
    private boolean sniff;
    private String shield;

    public void setCluster(String cluster) {
        this.cluster = cluster;
    }
    public void setAddressListStr(String addressListString) {
        this.addressListString = addressListString;
    }
    public void setSniff(boolean sniff) {
        this.sniff = sniff;
    }
    public void setShield(String shield) {
        this.shield = shield;
    }

    public void init() throws Exception{
        try {
            Settings settings = getSettings();
            TransportClient transportClient = null;
            transportClient = TransportClient.builder().addPlugin(ShieldPlugin.class).settings(settings).build();
            List<TransportAddress> addressList = getAddressList(addressListString);
            for (TransportAddress address : addressList) {
                transportClient.addTransportAddress(address);
            }
            this.client = transportClient;
        } catch (IOException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        }
    }

    public void close() throws Exception {
        if (client != null) {
            client.close();
        }
        client = null;
    }

    public Client getClient() {
        if (this.client == null) {
            throw new UnsupportedOperationException("client is not init");
        }
        return this.client;
    }

    private Settings getSettings() throws Exception {
        Settings.Builder settingBuilder = Settings.settingsBuilder();
        settingBuilder.put("cluster.name", cluster);
        settingBuilder.put("shield.user", shield);
        settingBuilder.put("client.transport.sniff", sniff);
        settingBuilder.put("transport.address.list", addressListString);
        return settingBuilder.build();
    }

    private synchronized List<TransportAddress> getAddressList(String addressListString)
            throws UnknownHostException {
        List<TransportAddress> addressList = new ArrayList<TransportAddress>();

        if (addressListString != null && !addressListString.trim().isEmpty()) {
            try {
                String[] addressStringArray = addressListString.trim().split(
                        ",");
                for (String addressString : addressStringArray) {
                    String[] pair = addressString.trim().split(":");
                    String host = pair[0].trim();
                    int port = Integer.parseInt(pair[1].trim());
                    addressList.add(new InetSocketTransportAddress(InetAddress
                            .getByName(host), port));
                }
            } catch (Exception e) {
                throw new IllegalArgumentException(
                        "transport.address.list has invalid format");
            }
        }
        return addressList;
    }

}

3、configuration

实现配置和实例化,注意esClient.setShield(shield);

shield格式如下:es.shield.user=98:qHa3pehd用户名:密码

package com.xiaoju.dqa.sentinel.configuration;


import com.xiaoju.dqa.sentinel.client.es.ESClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ESConfiguration {
    @Value("${es.cluster.name}")
    private String cluster;
    @Value("${es.client.transport.sniff}")
    private boolean sniff;
    @Value("${es.transport.address.list}")
    private String addressListString;
    @Value("${es.shield.user}")
    private String shield;

    @Bean(initMethod = "init", destroyMethod = "close")
    public ESClient esClient() {
        ESClient esClient = new ESClient();
        esClient.setCluster(cluster);
        esClient.setSniff(sniff);
        esClient.setAddressListStr(addressListString);
        esClient.setShield(shield);
        return esClient;
    }
}

4、配置文件

#  **********  ES 配置  **********
es.cluster.name=bigdata-arius-vip
es.client.transport.sniff=false
es.transport.address.list=bigdata-arius-ser200.gz01:8300,bigdata-arius-ser201.gz01:8300,bigdata-arius-ser202.gz01:8300,bigdata-arius-ser203.gz01:8300,bigdata-arius-ser204.gz01:8300,bigdata-arius-ser205.gz01:8300,bigdata-arius-ser206.gz01:8300
es.shield.user=98:qHa3pehd

继续阅读

更多来自我们博客的帖子

如何安装 BuddyPress
由 测试 December 17, 2023
经过差不多一年的开发,BuddyPress 这个基于 WordPress Mu 的 SNS 插件正式版终于发布了。BuddyPress...
阅读更多
Filter如何工作
由 测试 December 17, 2023
在 web.xml...
阅读更多
如何理解CGAffineTransform
由 测试 December 17, 2023
CGAffineTransform A structure for holding an affine transformation matrix. ...
阅读更多