spring boot 集成es

目錄

maven依賴

啓動類

application.yml

pojo

dao


maven依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

啓動類

/** @author bsl springBoot啓動類 @EnableScheduling 開啓定時器 */
@EnableScheduling
@SpringBootApplication
@MapperScan("com.wonders.mapper")
public class NetWorkApplication /*extends SpringBootServletInitializer*/ {
/**
 * springBoot啓動類,運行此方法啓動項目
 * @param args
 */
  public static void main(String[] args) {
      //        System.out.println("===========================================");
      /**
       * Springboot整合Elasticsearch 在項目啓動前設置一下的屬性,防止報錯
       * 解決netty衝突後初始化client時還會拋出異常
       * java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
       */
      System.setProperty("es.set.netty.runtime.available.processors", "false");
    SpringApplication.run(NetWorkApplication.class, args);
  }

application.yml

spring:
  application:
    name: es
  thymeleaf: # 關閉thymeleaf緩存
    cache: false
  #配置ES
  data:
    elasticsearch:
      cluster-name: docker-cluster
      cluster-nodes: ip:9300 #配置es節點信息,逗號分隔,如果沒有指定,則啓動ClientNode(9200端口是http查詢使用的。9300集羣使用。這裏使用9300.)
server:
  port: 33333
#指定mybatis映射文件的地址
mybatis:
  mapper-locations: classpath:mappers/*.xml
  type-aliases-package: com.wonders.bo

pojo

@Component
@Scope("prototype")
@Data
@Document(indexName = "govbaselog", type = "loginfos")
public class GovBaseLog implements Serializable {
	@Id
	private String st_id;
	@Field(type = FieldType.text,analyzer="ik_max_word")
	private String st_opt_id;


	@Field(type = FieldType.Date ,analyzer="ik_max_word",format = DateFormat.custom,pattern ="yyyy-MM-dd HH:mm:ss")
	@JsonFormat(shape =JsonFormat.Shape.STRING,pattern ="yyyy-MM-dd HH:mm:ss",timezone ="GMT+8")
	private Date dt_time;

	

	public GovBaseLog() {}
}

dao

import com.wonders.bo.GovBaseLog;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface GovBaseLogRepository extends ElasticsearchRepository<GovBaseLog,String>, PagingAndSortingRepository<GovBaseLog,String> {
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章