• 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
  • 相关阅读:
    超详细的Chrome插件Vimium使用教程!
    Chrome浏览器快捷键详细介绍,让你摆脱鼠标的束缚!
    为什么你的电脑越来越卡?很可能是因为这个!
    像黑客一样用命令打开应用!
    电脑桌面乱?看完这篇文章是不存在这种情况的!
    任务栏简化,让你的任务栏更美观!
    USB无法识别?简单详细的驱动程序安装教程!
    文件扩展名是什么?有什么用?通俗易懂的文件扩展名讲解!
    Win2008 IIS7.5安装配置PHP7.3.2步骤,及500错误解决
    用命令行方式关闭CentOS防火墙
  • 原文地址:https://www.cnblogs.com/kangoroo/p/7127003.html
Copyright © 2020-2023  润新知