SpringCloud系列----->SpringBoot项目中整合ElasticSearch

  1. 云栖社区>
  2. 博客>
  3. 正文

SpringCloud系列----->SpringBoot项目中整合ElasticSearch

程序狗狐狸 2019-08-04 19:49:19 浏览117
展开阅读全文
SpringBoot项目中整合ElasticSearch:
    1、首先在maven的pom.xml中加入以下配置:
              <properties>
          <!-- 设定和你的es版本相同的版本号,springboot默认的是2.x的版本-->
          <elasticsearch.version>6.5.4</elasticsearch.version>
       </properties>
    2、在pom.xml中增加:
          
           <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j.core</artifactId>
        <version>2.7</version>
    </dependency>

    3、在application.properties文件中添加
    
          appender.console.type = Console
          appender.console.name = console
          appender.console.layout.type = PatternLayout
          appender.console.layout.pattern = [%t] %-5p %c - %m%n
          rootLogger.level = info 
          rootLogger.appenderRef.console.ref = console 
          
    4、在项目中新建config目录,添加EsConfig.class文件:
    
        
        @Configuration
        public class EsConfig {
              @Bean
              public TransportClient client()  throws UnknownHostException {
                      InetSocketTransportAddress  node = new  InetSocketTransportAddress(
                              InetAddress.getByName("localhost"),
                              9300
                        );
                      Settings settings = Settings.builder().put("cluster.name","biges").build();
                      TransportClient client = new PreBuiltTransportClient(settings);
                      client.addTransportAddress(node);
                      return client;

            }
        }

网友评论

登录后评论
0/500
评论
程序狗狐狸
+ 关注