2: [SpringData集成Elasticsearch] --- 配置打印命令日志

文章目录

  • 一:引入SpringDataElasticsearch依赖
  • 二:创建RestHighLevelClient配置类
  • 三:配置YML: logging.level --- 日志等级
  • 三:请求接口,查看日志
    • 3.1:请求代码
    • 3.2:Postman请求地址
    • 3.3:查看输出日志

一:引入SpringDataElasticsearch依赖

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

二:创建RestHighLevelClient配置类

@Configuration
public class EsConfig {

    @Bean(destroyMethod = "close")
    public org.elasticsearch.client.RestHighLevelClient restClient() {

        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("192.yourIp.103:9200")
                .withBasicAuth("elastic", "123456")
                .build();

        org.elasticsearch.client.RestHighLevelClient client = RestClients.create(clientConfiguration).rest();
        return client;
    }

}

2: [SpringData集成Elasticsearch] --- 配置打印命令日志_第1张图片

三:配置YML: logging.level — 日志等级

核心

logging:
  level:
    org.springframework.data.elasticsearch.core: debug
    org.springframework.data.elasticsearch.client.WIRE: TRACE
spring:
  elasticsearch:
    rest:
      uris: 192.yourIp.103:9200
      username: elastic
      password: 123456
logging:
  level:
    org.springframework.data.elasticsearch.core: debug
    org.springframework.data.elasticsearch.client.WIRE: TRACE

三:请求接口,查看日志

3.1:请求代码

@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IBizProductService iBizProductService;
    @Autowired
    private IEsBizProductRepository iEsBizProductRepository;

    @PostMapping("/syncEsProduct")
    public String syncEsProduct() throws Exception {
        List<BizProduct> productList = iBizProductService.list();
        Iterable<BizProduct> productIterable = iEsBizProductRepository.saveAll(productList);
        return "同步Es成功";
    }


}

2: [SpringData集成Elasticsearch] --- 配置打印命令日志_第2张图片

3.2:Postman请求地址

http://localhost:8093/product/syncEsProduct
2: [SpringData集成Elasticsearch] --- 配置打印命令日志_第3张图片

3.3:查看输出日志

2023-01-10 22:56:57.114 TRACE 26180 — [nio-8093-exec-1] o.s.data.elasticsearch.client.WIRE : [481141cc]

Sending request POST /_bulk?timeout=1m with parameters: 
Request body: {"index":{"_index":"stock","_id":"1"}}
{"_class":"com.europa.tx.stock.entity.BizProduct","id":1,"name":"可口可乐","categoryId":1,"categoryName":"纤维","stockSn":"00001","auditStatus":0,"onSelfStatus":1,"cityId":200000,"cityName":"上海","provinceId":200000,"provinceName":"上海","price":5.0,"promotionPrice":5.0,"salesPrice":5.0,"stock":100,"warningStock":95,"unit":"ml","beginTime":1673236800000,"endTime":1675051200000,"tenantId":0}

2023-01-10 22:56:57.124 TRACE 26180 --- [/O dispatcher 1] o.s.data.elasticsearch.client.WIRE       : [481141cc] Received raw response: 200 OK
2023-01-10 22:56:57.139 TRACE 26180 --- [nio-8093-exec-1] o.s.data.elasticsearch.client.WIRE       : [3a274db7] Sending request POST /stock/_refresh?ignore_throttled=false&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true with parameters: 
2023-01-10 22:56:57.170 TRACE 26180 --- [/O dispatcher 1] o.s.data.elasticsearch.client.WIRE       : [3a274db7] Received raw response: 200 OK

2: [SpringData集成Elasticsearch] --- 配置打印命令日志_第4张图片

你可能感兴趣的:(Java-后端,#,elasticsearch,springboot,java)