'Spring Cloud Sleuth with MySQL

I am stucked to find any proper example which can demonstrate me to find out traces and store it in MySQL database locally. I have used zipkin server to visualize my distrubuted tracing of microservices. If any one worked with Spring Cloud Sleuth with gradle in latest version please handover the perfect Example which can help me.

Here' my code:

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}
ext {
    set('springCloudVersion', "Hoxton.SR4")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
    implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'

    // https://mvnrepository.com/artifact/io.zipkin.zipkin2/zipkin-storage-mysql-v1
    implementation group: 'io.zipkin.zipkin2', name: 'zipkin-storage-mysql-v1', version: '2.23.2'
    
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.5.0'

}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

application.properties

server.port=8083
spring.application.name=service1
#logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG
spring.sleuth.sampler.percentage=1.0
spring.zipkin.enabled=true
spring.zipkin.base-url=http://localhost:9411/
spring.sleuth.enabled=true

spring.zipkin.storage.type=mysql
spring.datasource.url=jdbc:mysql://localhost:3306/zipkin
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.schema=classpath:mysql.sql
spring.datasource.initialization-mode=always
spring.jpa.show-sql=true
spring.sleuth.web.enabled=true

spring.zipkin.sender.type=web

Controller.class

@RestController
@Slf4j
@RequiredArgsConstructor
public class SpaceShipRestController {

    private final RestTemplate restTemplate;

    @GetMapping("/service1")
    public String service1() {
        log.info("service1 start");
        String service2 = restTemplate.getForObject("http://localhost:8090/service2", String.class);
        String service3 = restTemplate.getForObject("http://localhost:9090/service3", String.class);
        log.info("call" + service2 + "and" + service3);
        return "calling service2";
    }
}

Apllication.class

@SpringBootApplication
@EnableAutoConfiguration
public class DemoApplication {

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

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

    @Bean
    @Primary
    public MySQLStorage mySQLStorage(DataSource datasource) {
        return MySQLStorage.newBuilder().datasource(datasource).executor(Runnable::run).build();
    }
}

I have Used zipkin-server-2.23.2-exec jar to lauch server on port localhost:9411 as default port using this command on cmd. java -jar zipkin-server-2.23.2-exec

My zipkin is up and working fine i am just stucked to store the traces in my MySQL DB the tables are getting created properly. But the record are empty. I don't no why i think i am missing any configuration. I followed this article as they shown to store traces in DB. Can anyone suggest me where i am missing to evalute.

My zipkin UI looks like

My MySQL DB



Solution 1:[1]

Please use Spring Cloud Sleuth 3.1.0-SNAPSHOT (via the 2021.0.0-SNAPSHOT train, we should have M1 in June AFAIR). Here you can find a sample https://github.com/spring-cloud-samples/spring-cloud-sleuth-samples/tree/3.1.x/data (remember about this part https://github.com/spring-cloud-samples/spring-cloud-sleuth-samples/blob/3.1.x/data/pom.xml#L33-L38) and the documentation https://docs.spring.io/spring-cloud-sleuth/docs/3.1.0-SNAPSHOT/reference/html/integrations.html#sleuth-jdbc-integration . For your convenience I'm copying the code below

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <spring-cloud.version>2021.0.0-SNAPSHOT</spring-cloud.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- Either p6spy or datasource-proxy -->
        <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.9.1</version>
            <scope>runtime</scope>
        </dependency>
        <!-- Either p6spy or datasource-proxy -->
        <!-- <dependency>
            <groupId>net.ttddyy</groupId>
            <artifactId>datasource-proxy</artifactId>
            <version>1.7</version>
            <scope>runtime</scope>
        </dependency> -->

                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-sleuth</artifactId>
                </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/release</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-plugin-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-plugin-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/release</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

If you don't want to use snapshots, you need to use Brave's p6spy support https://github.com/openzipkin/brave/tree/master/instrumentation/p6spy . Please add io.zipkin.brave:brave-instrumentation-p6spy to the classpath and change the properties as presented in the project's readme

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin Grzejszczak