Spring Cloud Sleuth和ELK实现日志跟踪
 南窗  分类:IT技术  人气:91  回帖:0  发布于1年前 收藏

Spring Cloud Sleuth和ELK(Elasticsearch、Logstash和Kibana)是一种流行的组合,可用于实现分布式跟踪和日志分析。

一、添加依赖

首先,我们需要在Maven或Gradle项目中添加Spring Cloud Sleuth和ELK的依赖。以下是在Maven项目中添加依赖的示例:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>6.2</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <version>2.6.3</version>
</dependency>

在上面的依赖中,我们添加了Spring Cloud Sleuth的核心依赖和ELK的依赖。我们还使用Logstash Logback Encoder来格式化日志输出,并使用Log4j2作为日志记录器。

二、配置应用

在添加了Spring Cloud Sleuth和ELK的依赖之后,我们需要为应用程序配置一些参数,以便Sleuth和ELK能够正常工作。以下是一个简单的配置示例:

spring:
  sleuth:
    sampler:
      probability: 1.0

logging:
  level:
    root: INFO
    org.springframework.web: INFO
    com.example.demo: DEBUG

  file:
    path: logs
    name: app.log
    encoder:
      pattern: "%date %level [%thread] %logger{10} [%file:%line] %msg%n"
      charset: UTF-8

app:
  name: order-service

在上面的配置中,我们将采样率设置为1.0,这意味着我们将对所有请求进行跟踪。我们还配置了日志记录器的级别和格式,并指定了日志文件的路径和名称。我们还添加了一个应用程序名称,用于将日志发送到ELK服务器。

三、配置ELK

现在,我们已经完成了Spring Cloud Sleuth和应用程序的配置,接下来我们需要配置ELK服务器以收集和分析日志数据。以下是一个简单的ELK配置示例:

input {
  tcp {
    port => 5000
    codec => json_lines
  }
}

filter {
  if [app][name] == "order-service" {
    mutate {
      add_field => { "service" => "order-service" }
    }
  }
}

output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "%{[service]}-%{+YYYY.MM.dd}"
  }
}

在上面的配置中,我们使用Logstash作为数据收集器,将日志数据发送到Elasticsearch。我们还添加了一个过滤器来为日志数据添加一个服务字段,并将数据索引到特定的索引中,索引名称由服务名称和日期组成。在这个示例中,我们的服务名称是order-service,因此我们将日志数据索引到order-service-YYYY.MM.dd的索引中。

四、在应用程序中使用ELK

现在,我们已经完成了Spring Cloud Sleuth和ELK的配置,接下来我们需要在应用程序中使用它们。以下是一个简单的示例:

@RestController
public class OrderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/orders/{id}")
    public Order getOrder(@PathVariable Long id) {
        LOGGER.info("Getting order with id {}", id);
        Order order = restTemplate.getForObject("http://localhost:8081/orders/" + id, Order.class);
        LOGGER.info("Got order with id {}", id);
        return order;
    }
}

在上面的示例中,我们使用了Spring Boot的@RestController注解来创建一个REST API端点。在方法中,我们使用Spring Boot的RestTemplate来发送HTTP请求,并记录请求的开始和结束时间。由于我们已经在应用程序中使用了Spring Cloud Sleuth,因此Sleuth会自动记录跟踪ID和跟踪span ID,并将它们添加到日志中。

五、分析日志数据

现在,我们已经在应用程序中记录了日志,并将它们发送到ELK服务器,接下来我们需要使用Kibana来分析日志数据。以下是一个简单的Kibana查询示例:

GET order-service-*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "service": "order-service"
          }
        },
        {
          "match": {
            "message": "Getting order with id"
          }
        }
      ]
    }
  }
}

在上面的查询中,我们使用Elasticsearch的match查询来搜索包含服务名称和"Getting order with id"的日志消息。此查询将返回所有满足条件的日志数据,并将它们显示在Kibana的搜索结果中。

 标签:Springcloud,

讨论这个帖子(0)垃圾回帖将一律封号处理……