springcloud:Finchley.SR2
springboot:2.0.6.RELEASE
zipkin:zipkin-server-2.11.5-exec.jar
zipkin2.0之后官方不再建议自定义zipkin,建议使用官方提供的zipkin.jar包。
启动zipkin服务:
创建数据表:
CREATE TABLE IF NOT EXISTS zipkin_spans (
`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
`trace_id` BIGINT NOT NULL,
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`parent_id` BIGINT,
`debug` BIT(1),
`start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
`duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`) COMMENT 'ignore insert on duplicate';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`) COMMENT 'for joining with zipkin_annotations';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';
CREATE TABLE IF NOT EXISTS zipkin_annotations (
`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
`trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
`span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
`a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
`a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
`a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
`a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
`endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
`endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces';
CREATE TABLE IF NOT EXISTS zipkin_dependencies (
`day` DATE NOT NULL,
`parent` VARCHAR(255) NOT NULL,
`child` VARCHAR(255) NOT NULL,
`error_count` BIGINT,
`call_count` BIGINT
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);
windows:
java -jar zipkin-server-2.11.5-exec.jar --zipkin.collector.rabbitmq.addresses=localhost:5672 --zipkin.collector.rabbitmq.username=guest --zipkin.collector.rabbitmq.password=guest --zipkin.storage.mysql.host=localhost --zipkin.storage.mysql.port=3306 --zipkin.storage.mysql.password=123456 --zipkin.storage.mysql.username=root
服务启动,并且端口号为9411,访问localhost:9411就能看到zipkin的页面
docker启动:
zipkin版本(2.12.3)
docker pull openzipkin/zipkin:2.12.3
创建配置文件
mkdir zipkin
touch zipkin/docker-compose.yml
编辑文件
vim zipkin/docker-compose.yml
按 i 进入编辑模式粘贴下面配置(记得修改配置)
version: "2.2"
services:
zipkin:
image: openzipkin/zipkin:2.12.3
container_name: zipkin
environment:
- STORAGE_TYPE=mysql
- MYSQL_DB=zipkin
- MYSQL_USER=root
- MYSQL_PASS=123456
- MYSQL_HOST=localhost
- MYSQL_TCP_PORT=3306
- RABBIT_ADDRESSES=localhost:5672
- RABBIT_USER=guest
- RABBIT_PASSWORD=guest
- RABBIT_QUEUE=zipkin
- RABBIT_VIRTUAL_HOST=/
ports:
- 9411:9411
保存退出
按esc退出编辑模式
:wq 保存退出
运行docker-compose.yml文件
docker-compose -f docker-compose.yml up -d
运行成功后docker ps -a查看所有docker镜像,状态为up就是起来了,状态为exited就没起来,起来了就访问ip加9411就能进入zipkin页面
微服务传递消息给zipkin
1、添加依赖(会跟随springcloud和springboot的版本而变化:我这版本是2.0.2.RELEASE)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
2、添加配置
spring:
application:
name: trade-server-backstage
sleuth:
sampler:
probability: 1.0 //收集比例。默认为0.1
zipkin:
base-url: http://localhost:5555/ //如果为web(http)发送需指向zipkin地址,其他形式可删除此配置
service:
name: sleuth-backstage //删除默认拿spring.application.name
sender:
type: rabbit //发送方式web、rabbit、kafka
rabbitmq:
host: localhost
port: 5672
virtual-host: /
username: guest
password: guest
如果启动报错:
Description:
Parameter 2 of method reporter in org.springframework.cloud.sleuth.zipkin2.ZipkinAutoConfiguration required a bean of type 'zipkin2.reporter.Sender' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'kafkaSender' in 'ZipkinKafkaSenderConfiguration' not loaded because @ConditionalOnClass did not find required class 'org.apache.kafka.common.serialization.ByteArraySerializer'
- Bean method 'rabbitSender' in 'ZipkinRabbitSenderConfiguration' not loaded because @ConditionalOnBean (types: org.springframework.amqp.rabbit.connection.CachingConnectionFactory; SearchStrategy: all) did not find any beans of type org.springframework.amqp.rabbit.connection.CachingConnectionFactory
- Bean method 'restTemplateSender' in 'ZipkinRestTemplateSenderConfiguration' not loaded because ZipkinSender org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRestTemplateSenderConfiguration rabbit sender type
Action:
Consider revisiting the entries above or defining a bean of type 'zipkin2.reporter.Sender' in your configuration.
则需要添加一个配置类:(注意导入的类)
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import zipkin2.reporter.Sender;
import zipkin2.reporter.amqp.RabbitMQSender;
/**
* @author Natsu
* @date 2019/10/17
*/
@Configuration
public class ZipkinConfig {
@Autowired
private RabbitProperties rabbitProperties;
@Bean
Sender rabbitSender2() {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(rabbitProperties.getHost());
connectionFactory.setPort(rabbitProperties.getPort());
connectionFactory.setPassword(rabbitProperties.getPassword());
connectionFactory.setUsername(rabbitProperties.getUsername());
return RabbitMQSender.newBuilder()
.connectionFactory(connectionFactory)
.queue("zipkin")
.addresses(rabbitProperties.getHost()+":"+rabbitProperties.getPort())
.build();
}
}
如果报错;
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/cloud/netflix/zuul/web/ZuulHandlerMapping
java.lang.NoClassDefFoundError: org/springframework/cloud/netflix/zuul/web/ZuulHandlerMapping
java.lang.ClassNotFoundException: org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping
则需要加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
启动成功后:
可看日志绿色部分有逗号分隔成四个部分,第一部分是服务名,第二部分是自己的traceId,第四是是否传输成功。如果日志没有四个点也没关系,是日志打印的方式不对