服务链路追踪(Spring Cloud Sleuth) sping boot 集成 zipkin 的 Demo(一)

一、首先建立四个项目

1、服务端  项目名称 ZipkinServer 

pom文件如下

其中主要是zipkin的依赖



	4.0.0

	com.zipkinServer
	ZipkinServer
	0.0.1-SNAPSHOT
	jar

	ZipkinServer
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.8.RELEASE
		
	

	
		UTF-8
		UTF-8
		1.8
		Camden.SR6
	

	

		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			io.zipkin.java
			zipkin-server
		
		
			io.zipkin.java
			zipkin-autoconfigure-ui
		

		
		
			io.zipkin.java
			zipkin
			1.28.0
		
		
			io.zipkin.java
			zipkin-autoconfigure-storage-elasticsearch-http
			1.28.0
		

		
		
			net.logstash.logback
			logstash-logback-encoder
			4.9
		

	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

 

application.properties 文件如下

server.port=11008
spring.application.name=ZipkinServer

在启动主方法类中加上注解   @EnableZipkinServer,声明这是一个zipkin的服务端

package com.zipkinServer.ZipkinServer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.EnableZipkinServer;

@SpringBootApplication
@EnableZipkinServer
public class ZipkinServerApplication {

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

}

2、客户端A  项目名称 ZipkinHello

pom文件如下, 同样主要是zipkin的依赖



	4.0.0

	com.ZipkinHello
	ZipkinHello
	0.0.1-SNAPSHOT
	jar

	ZipkinHello
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.8.RELEASE
		
	

	
		UTF-8
		UTF-8
		1.8
		Camden.SR6
	

	

		
			org.springframework.boot
			spring-boot-starter-web
		
        
		
			org.springframework.cloud
			spring-cloud-starter-zipkin
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
		
			net.logstash.logback
			logstash-logback-encoder
			4.9
		

	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


application.properties文件如下

server.port=8989
spring.zipkin.base-url=http://localhost:11008
spring.application.name=ZipkinHello

集成日志工具logback往ELK输出所以logback.xml文件如下










    ZipkinHello
    

    
    
    
    
    
    
    
    

    
    
        
            DEBUG
        
        
            
                ${CONSOLE_LOG_PATTERN}
                utf8
            
        
    
    

    
    
        
        
        
            
            ERROR
            
            ACCEPT
            
            DENY
        
        
        
            
            
                ${log.home_dir}/error/%d{yyyy-MM-dd}/errorLog-%i.log
            
            
            ${log.maxHistory}
            
                
                    2MB
                
        
        
            
                ${CONSOLE_LOG_PATTERN}
                utf8
            
        
    
    

    
    
        
            WARN
            ACCEPT
            DENY
        
        
            
                ${log.home_dir}/warn/%d{yyyy-MM-dd}/warnLog-%i.log
            
            ${log.maxHistory}
            
                2MB
            
        
        
            
                ${CONSOLE_LOG_PATTERN}
                utf8
            
        
    
    

    
    
        
            INFO
            ACCEPT
            DENY
        
        
            ${log.home_dir}/info/%d{yyyy-MM-dd}/infoLog-%i.log
            ${log.maxHistory}
            
                2MB
            
        
        
            
                ${CONSOLE_LOG_PATTERN}
                utf8
            
        
    
    

    
    
        
            DEBUG
            ACCEPT
            DENY
        
        
            ${log.home_dir}/debug/%d{yyyy-MM-dd}/debugLog-%i.log
            ${log.maxHistory}
            
                2MB
            
        
        
            
                ${CONSOLE_LOG_PATTERN}
                utf8
            
        
    
    

    
        
            
                %d{yyyy-MM-dd HH:mm:ss SSS} [%thread] %-5level %logger{36} - %msg%n
            
        
    

    
    
        localhost:9601
        1048576
        
    

    
        
    
        
        
        
        
        
        
        
        
    

    
    
    
    
    
    

程序入口代码

package com.zipkinServer.ZipkinServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SpringBootApplication
@RestController
public class ZipkinHelloApplication {

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

	private final static Logger logger = LoggerFactory.getLogger(ZipkinHelloApplication.class);
	private final static Logger kibanaLogger = LoggerFactory.getLogger("kibanaLog");

	@Autowired
	private RestTemplate restTemplate;

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

	@RequestMapping("/sayhello")
	public String sayhello() {
		logger.info("----sayhello----");
		kibanaLogger.info("======kibana:sayhello======");
		ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:8999/helloToGoodBye?name={1}", String.class, "8989张三");
		return responseEntity.getBody();
	}
	@RequestMapping("/goodBye")
	public String goodBye(String name) {
		logger.info("----goodBye----");
		kibanaLogger.info("======kibana:goodBye======");
		return "goodBye,"+name;
	}

}

第一个sayHello是调用服务的发起者

第二个goodBye服务是被调用方

客户端A  ZipkinHello 完毕

3、客户端B ZipkinUnicom 该服务作为 A C 服务的中转服务存在  具体设置与客户端A类似主要取别如下

application.properties文件

server.port=8999
spring.zipkin.base-url=http://localhost:11008
spring.application.name=ZipkinUnicom

程序代码

package com.zipkinServer.ZipkinServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SpringBootApplication
@RestController
public class ZipkinUnicomApplication {

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

	private final static Logger logger = LoggerFactory.getLogger(ZipkinUnicomApplication.class);
	private final static Logger kibanaLogger = LoggerFactory.getLogger("kibanaLog");

	@Autowired
	private RestTemplate restTemplate;

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

	@RequestMapping("/helloToGoodBye")
	public String helloToGoodBye(String name) {
		logger.info("====helloToGoodBye====");
		kibanaLogger.info("=========kibana:helloToGoodBye=========");
		ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:8988/hello?name={1}", String.class, name);
		return responseEntity.getBody();
	}
	@RequestMapping("/goodByeToHello")
	public String goodByeToHello(String name) {
		logger.info("====goodByeToHello====");
		kibanaLogger.info("=========kibana:goodByeToHello=========");
		ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:8989/goodBye?name={1}", String.class, name);
		return responseEntity.getBody();
	}

}

4、客户端C ZipkinGoodBye 

application.properties代码如下

server.port=8988
spring.zipkin.base-url=http://localhost:11008
spring.application.name=ZipkinGoodBye

程序代码如下

package com.zipkinServer.ZipkinServer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
public class ZipkinGoodByeApplication {

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

	private final static Logger logger = LoggerFactory.getLogger(ZipkinGoodByeApplication.class);
	private final static Logger kibanaLogger = LoggerFactory.getLogger("kibanaLog");

	@Autowired
	private RestTemplate restTemplate;

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

	@RequestMapping("/sayGoodBye")
	public String sayGoodBye() {
		logger.info("----sayGoodBye----");
		kibanaLogger.info("=========kibana:sayGoodBye=========");
		ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:8999/goodByeToHello?name={1}", String.class, "8988李四");
		return responseEntity.getBody();
	}
	@RequestMapping("/hello")
	public String sayHello2(String name) {
		logger.info("----hello----");
		kibanaLogger.info("=========kibana:hello=========");
		return "hello,"+name;
	}

}

二、启动这四个服务

启动顺序为  ZipkinServer -->ZipkinHello --> ZipkinUnicom --> ZipkinGoodBye 要按顺序启动

启动后打开http://localhost:11008/可看到界面

三、访问地址http://localhost:8988/sayGoodBye,http://localhost:8989/sayhello

可以看到返回信息界面

四、查看控制台打印的日志

三个客户端服务分别输出下列日志

2018-11-09 17:45:53.953[3b823e4538d5bd90,3b823e4538d5bd90] [http-nio-8989-exec-4] INFO  c.zipkinServer.ZipkinServer.ZipkinHelloApplication - ----sayhello----

 

2018-11-09 17:45:53.958[3b823e4538d5bd90,053423295201b4c4] [http-nio-8999-exec-4] INFO  c.z.ZipkinServer.ZipkinUnicomApplication - ====helloToGoodBye====
2018-11-09 17:45:53.966[3b823e4538d5bd90,dce48c58b696242f] [http-nio-8988-exec-5] INFO  c.z.ZipkinServer.ZipkinGoodByeApplication - ----hello----

因为logback服务定义的输出日志格式是

    
    
[%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] 的部分都是“3b823e4538d5bd90”  表明它是一次调用的一个过程

这样就能通过日志查看服务的调用情况了

源码:https://gitee.com/zhangchai/ZipkinClient.git

你可能感兴趣的:(服务链路追踪(Spring Cloud Sleuth) sping boot 集成 zipkin 的 Demo(一))