elasticsearch6.1在java中的连接基于xpack实现权限访问配置

最近项目中用到elastic,做个记录。     

1、maven项目在pom文件中加入需要的包配置



    org.elasticsearch.client
    x-pack-transport
    6.1.1


    org.elasticsearch.client
    transport
    6.1.1

  
    org.elasticsearch  
    elasticsearch  
    6.1.1  

2、java中连接ElasticConfig代码
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:/elasticsearch.properties")
public class EsConfig implements FactoryBean, InitializingBean, DisposableBean {

	private static final Logger logger = LoggerFactory.getLogger(EsConfig.class);
	
	@Value("${spring.data.elasticsearch.cluster-nodes}")
	private String clusterNodes;

	@Value("${spring.data.elasticsearch.cluster-name}")
	private String clusterName;
	
	private TransportClient client;
	private PreBuiltXPackTransportClient preBuiltTransportClient;

	@Override
	public void destroy() throws Exception {
		try {
            logger.info("Closing elasticSearch client");
            if (client != null) {
                client.close();
            }
        } catch (final Exception e) {
            logger.error("Error closing ElasticSearch client: ", e);
        }
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		 buildClient();
	}

	@Override
	public TransportClient getObject() throws Exception {
		return client;
	}

	@Override
	public Class getObjectType() {
		 return TransportClient.class;
	}

	@Override
	public boolean isSingleton() {
		return false;
	}

	protected void buildClient()  {
        try {
        	preBuiltTransportClient = new PreBuiltXPackTransportClient(settings());

			String InetSocket[] = clusterNodes.split(":");
			String address = InetSocket[0];
			Integer port = Integer.valueOf(InetSocket[1]);
			client = preBuiltTransportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(address), port));
        } catch (UnknownHostException e) {
            logger.error(e.getMessage());
        }
    }
	
	private Settings settings(){
		Settings settings = Settings.builder()
	               .put("cluster.name", clusterName) //设置ES实例的名称
	               .put("xpack.security.transport.ssl.enabled",false)//设置xpack权限用户
	               .put("xpack.security.user","elastic:asd123")
	               .put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
	               .build();
        return settings;
    }

}
3、业务类中通过注入获取连接实例
@Autowired
private TransportClient client;

你可能感兴趣的:(全文检索引擎)