ZipKin HTTP服务跟踪

项目结构:

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.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.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>
		<jetty.version>8.1.20.v20160902</jetty.version>  
	    <brave.version>3.16.0</brave.version>  
	    <zipkin-reporter.version>0.6.9</zipkin-reporter.version>  
	</properties>

	<dependencies>
		<!-- <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency> -->
		<!-- <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</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-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>  
	        <groupId>io.zipkin.brave</groupId>  
	        <artifactId>brave-core-spring</artifactId>  
	        <version>${brave.version}</version>  
	    </dependency>  
	    <dependency>  
	      <groupId>io.zipkin.reporter</groupId>  
	      <artifactId>zipkin-sender-okhttp3</artifactId>  
	      <version>${zipkin-reporter.version}</version>  
	    </dependency>  
	    <dependency>  
	      <groupId>io.zipkin.reporter</groupId>  
	      <artifactId>zipkin-sender-libthrift</artifactId>  
	      <version>${zipkin-reporter.version}</version>  
	    </dependency>  
	    <dependency>  
	      <groupId>io.zipkin.reporter</groupId>  
	      <artifactId>zipkin-sender-kafka08</artifactId>  
	      <version>${zipkin-reporter.version}</version>  
	    </dependency>  
	    <dependency>  
	        <groupId>io.zipkin.brave</groupId>  
	        <artifactId>brave-spring-web-servlet-interceptor</artifactId>  
	        <version>${brave.version}</version>  
	    </dependency>  
	    <dependency>  
	      <groupId>io.zipkin.brave</groupId>  
	      <artifactId>brave-spring-resttemplate-interceptors</artifactId>  
	      <version>${brave.version}</version>  
	    </dependency>
	</dependencies>

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


</project>

相关代码:

package com.example.demo;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import zipkin.Span;
import zipkin.reporter.AsyncReporter;
import zipkin.reporter.Reporter;
import zipkin.reporter.Sender;
import zipkin.reporter.okhttp3.OkHttpSender;

import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import com.github.kristofa.brave.http.SpanNameProvider;
import com.github.kristofa.brave.spring.BraveClientHttpRequestInterceptor;
import com.github.kristofa.brave.spring.ServletHandlerInterceptor;

/**
 * 服务配置
 * @author gaosiling
 *
 */
@Configuration
@Import({ BraveClientHttpRequestInterceptor.class,
		ServletHandlerInterceptor.class })
public class WebTracingConfiguration extends WebMvcConfigurerAdapter {

	/** 发送器配置 */
	@Bean
	Sender sender() {
		return OkHttpSender.create("http://127.0.0.1:9411/api/v1/spans");
		// return LibthriftSender.create("127.0.0.1");
		// return KafkaSender.create("127.0.0.1:9092");
	}

	/** 用什么方式显示span信息 */
	@Bean
	Reporter<Span> reporter() {
		// 取消注释,日志打印span信息
		// return new LoggingReporter();

		return AsyncReporter.builder(sender()).build();
	}

	@Bean
	Brave brave() {
		return new Brave.Builder("brave-webmvc").reporter(reporter())
				.build();
	}

	// span命名提供者,默认为http方法.
	@Bean
	SpanNameProvider spanNameProvider() {
		return new DefaultSpanNameProvider();
	}

	@Autowired
	private ServletHandlerInterceptor serverInterceptor;

	@Autowired
	private BraveClientHttpRequestInterceptor clientInterceptor;

	@Autowired
	private RestTemplate restTemplate;

	// 添加rest template拦截器
	@PostConstruct
	public void init() {
		List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
				restTemplate.getInterceptors());
		interceptors.add(clientInterceptor);
		restTemplate.setInterceptors(interceptors);
	}

	// 添加Severlet拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(serverInterceptor);
	}
}

package com.example.demo;

import java.util.Random;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
import org.springframework.web.client.RestTemplate;  
import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
  
  
@RestController  
@EnableWebMvc  
@Configuration  
public class ExampleController {  
  
  
  @Bean RestTemplate template() {  
    return new RestTemplate();  
  }  
  
  
  @Autowired RestTemplate template;  
  
  
  @RequestMapping("/brave-webmvc/a")  
  public String a() throws InterruptedException {  
    Random random = new Random();  
    Thread.sleep(random.nextInt(1000));  
  
  
    return template.getForObject("http://localhost:8080/brave-webmvc/b", String.class);  
  }  
  
  
  @RequestMapping("/brave-webmvc/b")  
  public String b() throws InterruptedException {  
  
  
    Random random = new Random();  
    Thread.sleep(random.nextInt(1000));  
  
  
    return "b";  
  }  
}
package com.example.demo;

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;

@RestController
@SpringBootApplication
public class DemoApplication {

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

}

Zipkin监控信息:



猜你喜欢

转载自blog.csdn.net/gaosilingqwer/article/details/79878473