elastic stack(六) springboot整合es

elasticsearch版本类型统一7.8.0

1、项目架构如下

2、配置文件(进行验证登陆的时候需要输入账号、密码)

/**
 * @author yfc
 * @date 2020/8/17
 * @description elasticsearch配置文件
 * @parmas
 */
@Configuration
public class ElasticsearchClientConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        //集群构建多个,非集群构建一个 RestClient.builder(
        //                        new HttpHost("localhost", 9200, "http"),
        //                        new HttpHost("localhost", 9201, "http")))
        //1、用户认证对象
        final CredentialsProvider credentialsProvider=new BasicCredentialsProvider();
        //2、设置账号密码
        credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("elastic","elastic"));
        //3、创建rest对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("121.0.0.170", 9200, "http")).setHttpClientConfigCallback(
                        new RestClientBuilder.HttpClientConfigCallback() {
                            @Override
                            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                                return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                            }
                        }
                ));
        return client;
    }


}

没有设置账号密码的配置文件如下:

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        //集群构建多个,非集群构建一个 RestClient.builder(
        //                        new HttpHost("localhost", 9200, "http"),
        //                        new HttpHost("localhost", 9201, "http")))
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("121.0.0.170", 9200, "http")));
        return client;
    }

3、测试类

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * @Author yfc
     * @Date 2020/8/17
     * @Despration 测试索引的创建
     * @Param
     */
    @Test
    void testCreateIndex() throws IOException {
        //1、创建索引请求
        CreateIndexRequest request=new CreateIndexRequest("yfc");
        //2、执行请求 IndicesClient
        CreateIndexResponse createIndexResponse=restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }

}

4、创建索引成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值