Spring Boot micro-tracking service log

If problems occur during the operation of the application, most developers are difficult to trace log. This can be solved by Spring Cloud Sleuth and ZipKin for Spring Boot server applications.

Spring Cloud Sleuth

Spring cloud Sleuth journal printed in the following format -

[application-name,traceid,spanid,zipkin-export] 
Java

In the above format,

Application-name = name of the application
Traceid = traceid each request and response is the same when a call to the same service or service to another.
Spanid = Span Id printed with Trace Id. The service call for a response to each request and another service, Span Id is different.
Zipkin-export = by default false. If so, then the server logs exported to Zipkin.

Now add Spring Cloud Starter Sleuth dependencies in profiling, as follows -

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

Gradle users can build.gradle add the following file dependencies -

compile('org.springframework.cloud:spring-cloud-starter-sleuth')

Now, added to Logs Rest Controller Spring Boot application class files, as follows -

package com.yiibai.sleuthapp;

import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class SleuthappApplication { private static final Logger LOG = Logger.getLogger(SleuthappApplication.class.getName()); public static void main(String[] args) { SpringApplication.run(SleuthappApplication.class, args); } @RequestMapping("/") public String index() { LOG.log(Level.INFO, "Index API is calling"); return "Welcome Sleuth!"; } } 
Java

Now, application.properties add the application name of the file, as shown below -

spring.application.name = tracinglogs
Shell

The complete code profiling is as follows -

Maven build file - 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.yiibai</groupId> <artifactId>sleuthapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sleuthapp</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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> 
XML

Gradle build file: build.gradle -

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.yiibai'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = 'Edgware.RELEASE'
}
dependencies {
   compile('org.springframework.cloud:spring-cloud-starter-sleuth')
   compile('org.springframework.boot:spring-boot-starter-web')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

You can create an executable JAR file, and run Spring Boot application uses the following Gradle or Maven command.

For Maven, you can use the following command -

mvn clean install
Shell

In the "BUILD SUCCESS" After that, you can find the JAR files in the target directory.

For Gradle, you can use the following command -

gradle clean build
Shell

In the "BUILD SUCCESSFUL" After, you can build / libs locate the JAR file directory.

Now, use the following command to run the JAR file -

java –jar <JARFILE>
Shell

Now, URL in the Web browser => http://localhost:8080/, and then view the output in the console log.

See the following log in the console window, log printing in the following format [application-name,traceid,spanid,zipkin-export].

Zipkin server

Zipkin application is a monitoring and management applications Spring Boot Spring Cloud Sleuth log. To build Zipkin server, you need to add Zipkin UI and Zipkin Server dependencies in build configuration file.

Maven users can pom.xml to add the following file dependencies -

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

Gradle users can build.gradle add the following file dependencies -

compile('io.zipkin.java:zipkin-autoconfigure-ui')
compile('io.zipkin.java:zipkin-server')

Now, in the application configuration properties file server.port = 9411.
For properties ( the Properties ) file users, please application.propertiesadd the following properties file.

server.port = 9411
Shell

For YAML user, please application.yml add the following properties file.

server:
   port: 9411 
Yamla

Add Spring Boot in the main application class file @EnableZipkinServerannotations. @EnableZipkinServerNotes for the application server to act as Zipkin.

package com.yiibai.zipkinapp;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import zipkin.server.EnableZipkinServer; @SpringBootApplication @EnableZipkinServer public class ZipkinappApplication { public static void main(String[] args) { SpringApplication.run(ZipkinappApplication.class, args); } } 
Java

Construction of the complete profile of the code is shown below.
Maven build file - 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.yiibai</groupId> <artifactId>zipkinapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>zipkinapp</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> </properties> <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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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> 
XML

Gradle build file - build.gradle contents are as follows:

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.yiibai'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = 'Edgware.RELEASE'
}
dependencies {
   compile('io.zipkin.java:zipkin-autoconfigure-ui')
   compile('io.zipkin.java:zipkin-server')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

You can create an executable JAR file, and use the following command to run Gradle or Maven Spring Boot application -
for Maven, use the command given below -

mvn clean install
Shell

After "BUILD SUCCESS", you can targetfind the JAR file directory.

For Gradle, use the command given below -

gradle clean build
Shell

After the "BUILD SUCCESSFUL", can build/libsfind the JAR file directory.

JAR file using the command to run the show -

java –jar <JARFILE>
Shell

Now the application in Tomcat port has been 9411started on.

Now, access = the URL of> HTTP: // localhost: 9411 / Zipkin / , and view Zipkin server UI.

Then, add the following dependencies in the client service application, and pointed out Zipkin Server URL through micro Zipkin UI tracking service log.

Now add Spring Cloud Starter Zipkin dependencies in profiling, as follows -

Maven users can pom.xml to add the following file dependencies -

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

Gradle users can build.gradle add the following file dependencies -

compile('org.springframework.cloud:spring-cloud-sleuth-zipkin')

Now, add Always Sampler Bean in Spring Boot application, export the logs to Zipkin server.

@Bean
public AlwaysSampler defaultSampler() { return new AlwaysSampler(); } 
Java

If you add AlwaysSampler Bean, the automatic Spring Sleuth Zipkin Export option will falsechange true.

Next, the client service application.properties configuration Zipkin Server Base URL files.

spring.zipkin.baseUrl = http://localhost:9411/zipkin/

Then, provide tracking ID and look at the track in Zipkin UI.

http://localhost:9411/zipkin/traces/{traceid}/
Shell

Guess you like

Origin www.cnblogs.com/borter/p/12423859.html