springboot整合ES

springboot整合ES,第1张

springboot整合ES 1.新建gulimall-search项目

其pom文件如下



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.2.RELEASE
         
    

    com.atguigu.gulimall
    gulimall-search
    0.0.1-SNAPSHOT
    gulimall-search
    ES检索服务

    
        1.8
        7.4.2
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.atguigu.gulimall
            gulimall-common
            0.0.1-SNAPSHOT
        

        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.4.2
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2.application.yml

配置nacos注册中心地址

server:
  port: 12000

spring:
  application:
    name: gulimall-search

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
logging:
  level:
    com.atguigu.gulimall.search: debug
3.启动类

排除数据源

package com.atguigu.gulimall.gulimallsearch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GulimallSearchApplication {
    public static void main(String[] args) {
        SpringApplication.run(GulimallSearchApplication.class, args);
    }
}
4.ES配置

新建config包

package com.atguigu.gulimall.gulimallsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;


@SpringBootConfiguration
public class GulimallESConfiguration {
    @Bean
    RestHighLevelClient client() {
        RestClientBuilder builder = RestClient.builder(
                new HttpHost("192.168.56.10", 9200, "http"));
        return new RestHighLevelClient(builder);
    }
}
5.测试类
package com.atguigu.gulimall.gulimallsearch;

import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class GulimallSearchApplicationTests {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Test
    void contextLoads() {
        System.out.println(restHighLevelClient);
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5676761.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存