springcloud-zipkin的安装与使用
1、什么是zipkin
一个分布式系统的调用跟踪监控系统,把每次微服务调用都埋上点,打印固定格式的日志,然后收集到zipkin中,然后zipkin做数据的统计与收集;
zipkin的收集有两种;
1、http收集
2、消息中间件收集
二、快速demo(以rabbitmq收集为例子)
项目工程步骤如下
创建springcloud的eureka,trace-1,trace-2,zipkin-server服务
1、springcloud的eureka
创建springboot工程
pom文件复制黏贴
<?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.didispace</groupId> <artifactId>eureka-server</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>eureka-server</name> <description>Spring Cloud In Action</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-actuator</artifactId>--> <!--</dependency>--> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</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>
application.properties文件如下:
spring.application.name=eureka-server server.port=1001 eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
boot启动类加注解:
@EnableEurekaServer
2、创建trace-1、trace-2工程
pom文件
<?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.didispace</groupId> <artifactId>trace-1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>trace-1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.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> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-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-sleuth-zipkin</artifactId>--> <!-- </dependency>--> <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.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>4.6</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</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>
trace-1的配置文件
spring.application.name=trace-1 server.port=9101 eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/ #eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/ spring.zipkin.enabled=true spring.sleuth.sampler.percentage=1 spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
trace-2的配置文件
spring.application.name=trace-2 server.port=9102 eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/ #eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/ spring.zipkin.enabled=true spring.sleuth.sampler.percentage=1 spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
trace-1启动类
@RestController @EnableDiscoveryClient @SpringBootApplication public class Trace1Application { private final Logger logger = Logger.getLogger(getClass()); @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } @RequestMapping(value = "/trace-1", method = RequestMethod.GET) public String trace() { logger.info("===call trace-1==="); return restTemplate().getForEntity("http://trace-2/trace-2", String.class).getBody(); } public static void main(String[] args) { SpringApplication.run(Trace1Application.class, args); } }
trace-2启动类
@RestController @EnableDiscoveryClient @SpringBootApplication public class DemoApplication { private final Logger logger = LoggerFactory.getLogger(getClass()); @RequestMapping(value = "/trace-2", method = RequestMethod.GET) public String trace(HttpServletRequest request) { logger.info("===<call trace-2, TraceId={}, SpanId={}>===", request.getHeader("X-B3-TraceId"), request.getHeader("X-B3-SpanId")); return "Trace"; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
3、新建zipkin工程
pom文件
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> </parent> <groupId>com.zipin</groupId> <artifactId>zipin</artifactId> <version>0.0.1-SNAPSHOT</version> <name>zipin</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <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>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</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>
配置文件
spring.application.name=zipkin-server server.port=9411 spring.sleuth.enabled=false spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
启动类加上注解
@EnableZipkinStreamServer
二、四个工程一起启动
发起请求
localhost:9101/trace-1
浏览器进入localhost:9411
查看调用链
项目github地址:https://github.com/hd-eujian/zipik-learn.git