java通过shield链接Elasticsearch

本文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

 

转载于:https://www.cnblogs.com/kangoroo/p/7127003.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值