Spring Cloud微服务之 sleuth+zipkin日志聚合

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 1.简介 (1)什么是服务追踪 Sleuth 在微服务架构中,要完成一个功能,通过Rest请求服务API调用服务来完成,整个调用过程可能会聚合多个后台服务器协同完成。在整个链路上,任何一处调用超时 或出错都有可能造成前端请求失败。

1.简介

(1)什么是服务追踪 Sleuth

在微服务架构中,要完成一个功能,通过Rest请求服务API调用服务来完成,整个调用过程可能会聚合多个后台服务器协同完成。在整个链路上,任何一处调用超时

或出错都有可能造成前端请求失败。这时跟踪记录这些请求的调用的情况就要复杂的多,这就需要一个专门的工具来处理,spring cloud sleuth组件就是用于跟踪记录的工具

Sleuth就相当于为微服务架构引入了一套记录体系,包含两部分,一个是 trace ID;另一个是 span ID,随时记录一个请求的每一步操作。

(2)什么是日志聚合 Zipkin

如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:787707172,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

zipkin 是 Dpper的开源实现,支持多种语言。Sleuth已经将每个请求从开始调用到完成的每一步都进行了记录,但是这些log信息会很分散,使用起来不太方便,就

需要有一个工具可以将这些信息进行收集和汇总,并且显示可视化的结果,便于分析和定位。这就需要创建一个 Zipkin Server用于收集和展示这些调用链路的信息,他

的使用也很方便。

2.如何使用

本例需要创建三个工程:

product-sevice 普通客户端工程,提供一个rest接口(项目创建参考第三节)

order-service 普通客户端工程,用于调用product-service服务(项目创建参考第三节)

zipkin-service 日志服务工程,用于追踪记录请求链路。

第一步:创建 zipkin-service,作为Zipkin Server。

工程目录如图:

Spring Cloud微服务之 sleuth+zipkin日志聚合
(1)首先在 pom.xml中增加依赖

        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-autoconfigure-ui</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-server</artifactId>
    </dependency>

(2)在启动类增加@EnableZipkinServer注解,用来开启Zipkin Server的功能。

package com.hole;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import zipkin.server.EnableZipkinServer;

@EnableDiscoveryClient
@EnableZipkinServer
@SpringBootApplication
public class ZipkinServiceApplication {

private static Logger logger = LoggerFactory.getLogger(ZipkinServiceApplication.class);

public static void main(String[] args) {
    SpringApplication.run(ZipkinServiceApplication.class, args);
}

}
(3)application.properties配置如下:

spring.application.name=zipkin-service
server.port=9411
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
(4)启动 Zipkin Server,访问 http://localhost:9411/,此时因为还没有服务请求关联的zipkin server,所以服务名里列表里是空的,如图:

Spring Cloud微服务之 sleuth+zipkin日志聚合

第二步:创建日志客户端工程

想要在界面上能看到zipkin server搜集的日志信息及依赖关系,需要在每个工程中增加sleuth与zipkin的依赖,然后增加注册地址,指向到 zipkin server上就可以了。

1.(1)创建product-service工程,并增加依赖。

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

(2)application.properties配置增加配置,指向 zipkin server服务地址。

spring.zipkin.base-url=http://localhost:9411/
(3)启动类代码增加 /hello 接口,并在接口入口处做一个日志打印,代码如下

package com.hole;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class ProductServiceApplication {

private static Logger logger = LoggerFactory.getLogger(ProductServiceApplication.class);

public static void main(String[] args) {
    SpringApplication.run(ProductServiceApplication.class, args);
}

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public ResponseEntity<String> hello(){
    logger.info("called by product-service");
    return new ResponseEntity<String>("hello product service!", HttpStatus.OK);
}

}
2.(1)创建order-service工程。

依赖和配置同 product-sevice。

(2)启动类增加 /product/hello接口,目的是调动 product-service接口。在接口入口位置做一个日志打印。

package com.hole;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class OrderServiceApplication {

private static Logger logger = LoggerFactory.getLogger(OrderServiceApplication.class);

@Bean
@LoadBalanced
RestTemplate restTemplate(){
    return new RestTemplate();
}

@Autowired
RestTemplate restTemplate;

public static void main(String[] args) {
    SpringApplication.run(OrderServiceApplication.class, args);
}

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public ResponseEntity<String> hello(){
    return new ResponseEntity<String>("hello order service!", HttpStatus.OK);
}

@RequestMapping(value = "/product/hello",method = RequestMethod.GET)
public String productHello(){
    logger.info("order-service calling product-service!");
    return restTemplate.getForEntity("http://PRODUCT-SERVICE/hello",String.class).getBody();
}

}
第三步:启动项目并验证。

(1)启动全部项目,启动成功后在监控页面查看结果。

(2)访问 order-service 服务的 /product/hello接口,结果访问成功。可以多刷新几次

Spring Cloud微服务之 sleuth+zipkin日志聚合
(3)访问 zipkin server(9411端口),查看日志服务列表,发现service name下拉框已有下拉选项,验证成功。

Spring Cloud微服务之 sleuth+zipkin日志聚合
点击 Find Traces 查看结果可以看到它的完整链路条,点击链条可以看到调用链条,点击 Dependencies可以查看依赖关系,等等,自行发觉吧,就不上图了。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
7天前
|
安全 Java API
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)
27 0
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
|
7天前
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
|
1月前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(二)Rest微服务工程搭建
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(二)Rest微服务工程搭建
52 0
|
1月前
|
前端开发 微服务 容器
springcloud - 使用knife4j聚合微服务接口文档
springcloud - 使用knife4j聚合微服务接口文档
springcloud - 使用knife4j聚合微服务接口文档
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
155 0
|
1月前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
101 0
|
1天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
9天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【4月更文挑战第17天】Spring Cloud是Java微服务治理的首选框架,整合了Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心)。通过Eureka实现服务注册与发现,Ribbon提供负载均衡,Hystrix实现熔断保护,Zuul作为API网关,Config Server集中管理配置。理解并运用Spring Cloud进行微服务治理是现代Java开发者的关键技能。
|
24天前
|
负载均衡 网络协议 Java
构建高效可扩展的微服务架构:利用Spring Cloud实现服务发现与负载均衡
本文将探讨如何利用Spring Cloud技术实现微服务架构中的服务发现与负载均衡,通过注册中心来管理服务的注册与发现,并通过负载均衡策略实现请求的分发,从而构建高效可扩展的微服务系统。
|
1月前
|
SpringCloudAlibaba 负载均衡 Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
66 1