search
HomeJavajavaTutorialspring-cloud-sleuth+zipkin tracking service implementation (2)

1. Brief description

In the previous section "spring-cloud-sleuth+zipkin tracking service implementation (1)", we used microservice-zipkin-server, microservice-zipkin-client, microservice- The three zipkin-client-backend programs implement service call link tracking using http for communication and data persistence into memory.
Here we make two changes. First, the data is changed from being stored in memory to being persisted to the database. Second, the http communication is changed to mq asynchronous communication.

We still use the three programs in the previous section to make modifications so that everyone can see the differences. Here, a stream is added to each project name to indicate the difference.

2. microservice-zipkin-stream-server

To change the http method to communication through MQ, we need to replace the original dependent io.zipkin.java:zipkin-server with spring-cloud-sleuth-zipkin-stream and spring-cloud-starter-stream-rabbit
To use mysql persistence at the same time, we need to add mysql related dependencies.
All maven dependencies are as follows:

"
  <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter</artifactid>
    </dependency>
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-test</artifactid>
        <scope>test</scope>
    </dependency>
    <!--zipkin依赖-->
    <!--此依赖会自动引入spring-cloud-sleuth-stream并且引入zipkin的依赖包-->
    <dependency>
        <groupid>org.springframework.cloud</groupid>
        <artifactid>spring-cloud-sleuth-zipkin-stream</artifactid>
    </dependency>
    <dependency>
        <groupid>org.springframework.cloud</groupid>
        <artifactid>spring-cloud-starter-stream-rabbit</artifactid>
    </dependency>
    <dependency>
        <groupid>io.zipkin.java</groupid>
        <artifactid>zipkin-autoconfigure-ui</artifactid>
        <scope>runtime</scope>
    </dependency>

    <!--保存到数据库需要如下依赖-->
    <dependency>
        <groupid>mysql</groupid>
        <artifactid>mysql-connector-java</artifactid>
    </dependency>
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-jdbc</artifactid>
    </dependency>
 
"

After adding the above maven dependencies, we will replace the @EnableZipkinServer annotation in the startup class ZipkinServer with @EnableZipkinStreamServer,
The details are as follows:

package com.yangyang.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer;

/**
 * Created by chenshunyang on 2017/5/24.
 */
@EnableZipkinStreamServer// //使用Stream方式启动ZipkinServer
@SpringBootApplication
public class ZipkinStreamServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZipkinStreamServerApplication.class,args);
    }
}

Click@ From the source code of the EnableZipkinStreamServer annotation, we can see that it also introduces the @EnableZipkinServer annotation and also creates a rabbit-mq message queue listener.
spring-cloud-sleuth+zipkin tracking service implementation (2)

To facilitate receiving mq messages sent from the message client.

Since the message middleware rabbit mq and mysql are used, we also need to add relevant configurations to the configuration file application.properties:

 server.port=11020
spring.application.name=microservice-zipkin-stream-server
#zipkin数据保存到数据库中需要进行如下配置
#表示当前程序不使用sleuth
spring.sleuth.enabled=false
#表示zipkin数据存储方式是mysql
zipkin.storage.type=mysql
#数据库脚本创建地址,当有多个是可使用[x]表示集合第几个元素
spring.datasource.schema[0]=classpath:/zipkin.sql
#spring boot数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/zipkin?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initialize=true
spring.datasource.continue-on-error=true


#rabbitmq配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

Among them, zipkin.sql can be copied directly to the official website, or You can copy it from this demo
In order to avoid interference from http communication, we changed the original listening port from 11008 to 11020, started the program, and no error was reported and the rabbit connection log could be seen, indicating that the program started successfully.

3.microservice-zipkin-stream-client, microservice-zipkin-client-stream-backend

The same as the configuration in the previous section, the client configuration is also very simple, and the maven dependency only You need to replace the original spring-cloud-starter-zipkin with the following two dependencies

     <dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-sleuth-zipkin-stream</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-starter-stream-rabbit</artifactid>
        </dependency>

        <dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-starter-sleuth</artifactid>
        </dependency>

In addition, add the configuration to connect to MQ in the configuration file

server:
  port: 11021

spring:
  application:
    name: microservice-zipkin-stream-client

 #rabbitmq配置
  rabbitmq:
    host: 127.0.0.1
    port : 5672
    username: guest
    password: guest

Of course for the sake of demonstration Difference, the port has also been adjusted accordingly

4. Test

Access according to the previous section: http://localhost:11021/call/1, we can go to the previous section, This shows that the sleuth function of rabbit-mq communication has taken effect.

We have visited the consumer's address many times and can see in the log that the request time will not suddenly take a long time.
In order to experience the feature of no data loss brought to us by MQ communication, we clear the data in the database, and then refresh the zipkin server interface. You can see that there is no more data.

Then we will zipkin -The server program wants to close, and then access the consumer address multiple times. After that, we restart the zipkin server program, and access the UI interface after successful startup
Soon we see that the Span Name option has data to select, and at the same time, the records in the database The number of entries is no longer the previous 0.

This shows that after restarting our zipkin, we successfully obtained the information data generated by the provider and consumer during the shutdown period from MQ. In this way, we can use the rest service call tracking function of spring-cloud-sleuth-stream+zipkin.


The above is the detailed content of spring-cloud-sleuth+zipkin tracking service implementation (2). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does the JVM manage garbage collection across different platforms?How does the JVM manage garbage collection across different platforms?Apr 28, 2025 am 12:23 AM

JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa

Why can Java code run on different operating systems without modification?Why can Java code run on different operating systems without modification?Apr 28, 2025 am 12:14 AM

Java code can run on different operating systems without modification, because Java's "write once, run everywhere" philosophy is implemented by Java virtual machine (JVM). As the intermediary between the compiled Java bytecode and the operating system, the JVM translates the bytecode into specific machine instructions to ensure that the program can run independently on any platform with JVM installed.

Describe the process of compiling and executing a Java program, highlighting platform independence.Describe the process of compiling and executing a Java program, highlighting platform independence.Apr 28, 2025 am 12:08 AM

The compilation and execution of Java programs achieve platform independence through bytecode and JVM. 1) Write Java source code and compile it into bytecode. 2) Use JVM to execute bytecode on any platform to ensure the code runs across platforms.

How does the underlying hardware architecture affect Java's performance?How does the underlying hardware architecture affect Java's performance?Apr 28, 2025 am 12:05 AM

Java performance is closely related to hardware architecture, and understanding this relationship can significantly improve programming capabilities. 1) The JVM converts Java bytecode into machine instructions through JIT compilation, which is affected by the CPU architecture. 2) Memory management and garbage collection are affected by RAM and memory bus speed. 3) Cache and branch prediction optimize Java code execution. 4) Multi-threading and parallel processing improve performance on multi-core systems.

Explain why native libraries can break Java's platform independence.Explain why native libraries can break Java's platform independence.Apr 28, 2025 am 12:02 AM

Using native libraries will destroy Java's platform independence, because these libraries need to be compiled separately for each operating system. 1) The native library interacts with Java through JNI, providing functions that cannot be directly implemented by Java. 2) Using native libraries increases project complexity and requires managing library files for different platforms. 3) Although native libraries can improve performance, they should be used with caution and conducted cross-platform testing.

How does the JVM handle differences in operating system APIs?How does the JVM handle differences in operating system APIs?Apr 27, 2025 am 12:18 AM

JVM handles operating system API differences through JavaNativeInterface (JNI) and Java standard library: 1. JNI allows Java code to call local code and directly interact with the operating system API. 2. The Java standard library provides a unified API, which is internally mapped to different operating system APIs to ensure that the code runs across platforms.

How does the modularity introduced in Java 9 impact platform independence?How does the modularity introduced in Java 9 impact platform independence?Apr 27, 2025 am 12:15 AM

modularitydoesnotdirectlyaffectJava'splatformindependence.Java'splatformindependenceismaintainedbytheJVM,butmodularityinfluencesapplicationstructureandmanagement,indirectlyimpactingplatformindependence.1)Deploymentanddistributionbecomemoreefficientwi

What is bytecode, and how does it relate to Java's platform independence?What is bytecode, and how does it relate to Java's platform independence?Apr 27, 2025 am 12:06 AM

BytecodeinJavaistheintermediaterepresentationthatenablesplatformindependence.1)Javacodeiscompiledintobytecodestoredin.classfiles.2)TheJVMinterpretsorcompilesthisbytecodeintomachinecodeatruntime,allowingthesamebytecodetorunonanydevicewithaJVM,thusfulf

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools