一、搭建方式
Zipkin项目搭建方式分老版本和新版本,新老版本对Springboot版本和SpringCloud版本有要求,不然会报一些莫名其妙的错误,
这里是经过N次尝试,找到的对应版本。
Springboot版本:2.1.4.RELEASE
SpringCloud版本:Greenwich.SR4
项目截图:
二、搭建过程
------------旧版本搭建--------------
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 https://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>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.zipkin</groupId>
<artifactId>springcloud-zipkin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-zipkin</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
<zipkin.version>2.12.3</zipkin.version>
<mysql-connector.version>8.0.14</mysql-connector.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Eureka客户端配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入Feign支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--添加断路器配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--添加监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--添加zipkin -->
<!--zipkin-server 依赖包 -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>${zipkin.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</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>
application.xml文件:
server:
port: 8095
servlet:
context-path: /
spring:
application:
name: springcloud-zipkin
#允许bean被重写
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
defaultZone: http://admin:admin@eureka-server2:10002/eureka/,http://admin:admin@eureka-server1:10001/eureka/
instance:
instance-id: springcloud-zipkin-8095 #定义微服务的名称
prefer-ip-address: true #是否显示IP和端口
feign:
hystrix:
enabled: true #启动熔断器机制
#zipkin的ui界面配置
management:
metrics:
web:
server:
auto-time-requests: false
Application启动类文件:
package com.example.zipkin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import zipkin2.server.internal.EnableZipkinServer;
@EnableZipkinServer //注解的作用: 开启zipkin功能
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudZipkinApplication.class, args);
}
}
通过上述项目搭建好后,启动项目,可以在注册中心Eureka可看到服务,如下图:
使用postman进行接口调用,如下图:
根据上述接口调用,查看Zipkin的调用链路,如下:
------------新版本搭建--------------
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 https://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>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.zipkin</groupId>
<artifactId>springcloud-zipkin-new</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-zipkin-new</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Eureka客户端配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入Feign支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--添加断路器配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--添加监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--zipkin服务端依赖-->
<dependency>
<groupId>io.zipkin</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.23.2</version>
</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>
application.yml配置文件:
# 自定义 zipkin server地址
armeria:
ports[0]:
ip: 127.0.0.1
port: 8095
protocol: HTTP
bootstrap.yml配置文件:
server:
port: 8095
servlet:
context-path: /
spring:
application:
name: springcloud-zipkin-new
profiles:
active: dev
eureka:
client:
service-url:
defaultZone: http://admin:admin@eureka-server2:10002/eureka/,http://admin:admin@eureka-server1:10001/eureka/
instance:
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
feign:
hystrix:
enabled: true #启动熔断器机制
Application启动类:
package com.example.zipkin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import zipkin2.server.internal.EnableZipkinServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableZipkinServer
public class SpringcloudZipkinNewApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudZipkinNewApplication.class, args);
}
}
postman调用接口,如下:
根据上述接口调用,查看Zipkin的调用链路,如下:
三、上述总结
zipkin可以很清楚的呈现接口调用链路,每个链路点的时间,给我们分析接口性能提供了凭证,目前手动搭建zipkin是为了学习,为了方便可以直接使用官网给出的jar包,下载下来直接运行即可。
zipkin官网jar包下载地址:https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/2.12.9/
下一篇: Apollo配置中心搭建(7).