springboot zipkin调用链

 

一.服务端(最新版本官方不建议自己写服务端,建议直接使用编译好的jar,通过启动参数来配置)

下载地址:https://repo1.maven.org/maven2/io/zipkin/java/zipkin-server

1.pom.xml中依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tanjun</groupId>
    <artifactId>zipkin-server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.15.RELEASE</version>
    </parent>

    <dependencies>

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

        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin</artifactId>
            <version>2.8.4</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
            <version>2.8.4</version>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

2.yaml文件配置

spring:
  application:
    name: zipkin-server
server:
  port: 9411

3.启动类

SpringBootApplication
@EnableZipkinServer
public class Application {
    public static void main(String[] args) {
        SpringApplication springApplication=new SpringApplication(Application.class);
        springApplication.run(args);
    }
}

 4.验证

输入:localhost:9411

二.客户端

1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tanjun</groupId>
    <artifactId>eureka-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.15.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!---Eureka远程服务所需要的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!--调用链客户端所需要的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <!--健康检查所需要的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>




    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

2. yml文件

server:
  port: 8080
spring:
  application:
    name: eureka-client
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    trace-id128: true
    sampler:
      percentage: 1
#健康检查
management:
  security:
    enabled: false
endpoints:
  actuator:
    enabled: true
  health:
    sensitive: true

3.需要注意点:需要配置http请求相关的bean

@Component
public class HttpConfig {
    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }
}

 4.测试

 @RequestMapping("hello")
    public String hello() throws InterruptedException {
        Thread.sleep(3000L);
        String s=restTemplate.getForEntity("http://www.baidu.com",String.class).getBody();
        System.out.println(s);
        Thread.sleep(1000L);
        return restTemplate.getForEntity("http://www.baidu.com",String.class).getBody();
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Spring Boot 中整合 Zipkin 路追踪需要进行以下步骤: 1. 在 pom.xml 中添加依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> ``` 2. 在 application.properties 中添加如下配置: ``` spring.zipkin.base-url=http://localhost:9411 ``` 3. 在启动类上添加 `@EnableZipkinServer` 注解开启 Zipkin 服务端功能。 示例代码如下: ``` @SpringBootApplication @EnableZipkinServer public class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); } } ``` 4. 在需要进行路追踪的服务中添加依赖和配置: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> ``` 在 application.properties 中添加如下配置: ``` spring.zipkin.base-url=http://localhost:9411 ``` 5. 在启动类上添加 `@EnableZipkinStreamServer` 注解开启 Zipkin 客户端功能。 示例代码如下: ``` @SpringBootApplication @EnableZipkinStreamServer public class ZipkinClientApplication { public static void main(String[] args) { SpringApplication.run(ZipkinClientApplication.class, args); } } ``` 以上就是在 Spring Boot 中整合 Zipkin 路追踪的简单示例。希望这能帮到您。 ### 回答2: Zipkin是一个用于分布式系统的路追踪工具,方便开发人员在微服务架构中进行系统性能监控和故障排查。下面是一个基于Spring BootZipkin路追踪的配置整合示例。 首先,我们需要在pom.xml文件中添加相关依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <!-- 其他相关依赖 --> </dependencies> ``` 接下来,在Spring Boot应用的启动类上添加@EnableZipkinServer注解,启用Zipkin Server功能: ```java @SpringBootApplication @EnableZipkinServer public class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); } } ``` 然后,我们需要在应用的配置文件application.properties中添加相关配置: ```properties spring.application.name=zipkin-server server.port=9411 spring.zipkin.enabled=true spring.sleuth.sampler.probability=1.0 spring.zipkin.base-url=http://localhost:9411 ``` 其中,spring.zipkin.enabled为true表示启用Zipkin功能,spring.sleuth.sampler.probability=1.0表示采样率为100%,spring.zipkin.base-url为Zipkin Server的地址。 最后,我们需要在每个需要进行路追踪的Spring Boot应用上添加相关配置: ```properties spring.zipkin.baseUrl=http://localhost:9411 spring.zipkin.discovery-client-enabled=true spring.zipkin.communications.enabled=true spring.zipkin.sender.type=web ``` 其中,spring.zipkin.baseUrl为Zipkin Server的地址,spring.zipkin.discovery-client-enabled为true表示启动服务发现功能,spring.zipkin.communications.enabled为true表示启动基于HTTP的通信功能,spring.zipkin.sender.type为web表示使用web方式发送路数据。 通过以上配置和整合,我们就可以在Spring Boot应用中实现Zipkin路追踪的监控和故障排查功能了。 ### 回答3: Zipkin是一个开源的分布式追踪系统,可以用于追踪微服务架构中的请求路。在Spring Boot中整合Zipkin,可以通过配置和依赖注入来实现路追踪功能。 首先,需要在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> ``` 接下来,在application.properties文件中添加以下配置: ```properties spring.zipkin.base-url=http://localhost:9411 # Zipkin服务器的地址 ``` 然后,创建一个Spring Boot应用程序,并在启动类上添加`@EnableZipkinServer`注解,以启用Zipkin服务器: ```java @SpringBootApplication @EnableZipkinServer public class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); } } ``` 在需要追踪的微服务应用程序中,需要添加以下配置: ```properties spring.zipkin.baseUrl=http://localhost:9411 # Zipkin服务器的地址 spring.sleuth.sampler.probability=1.0 # 采样比例,此处为100%采样 ``` 然后,在需要追踪的请求执行之前,添加以下注解: ```java @Autowired private Tracer tracer; ... Span span = tracer.getCurrentSpan(); span.tag("key", "value"); // 可以添加自定义的标签信息 ... span.finish(); ``` 这样就完成了Zipkin路追踪功能的配置和整合。通过访问Zipkin服务器提供的界面,可以查看各个微服务之间的请求路和调用耗时。 总结:通过添加依赖和配置,以及使用Tracer来记录和追踪请求的执行情况,可以实现在Spring Boot应用程序中使用Zipkin进行路追踪。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值