如何解决Zipkin 未连接到 RabbitMQ
请注意这个问题似乎与this one重复,但事实并非如此。下面我包括我的理性
我正在尝试将 Sleuth/Zipkin 跟踪添加到我的项目中。为此,我遵循了tutorial。
我的项目已经在使用 RabbitMQ 进行不同微服务之间的通信,工作正常。
我的问题是,当我使用 Web 连接时,我能够很好地获取跟踪,但是当我尝试使用 RabbitMQ 进行通信时,却出现了无法连接的错误。
product-composite_1 | 2021-01-28 18:11:46.799 WARN [product-composite,] 1 --- [ main] o.s.c.s.a.z.ZipkinAutoConfiguration :
Check result of the [RabbitMQSender{addresses=[rabbitmq:5672],queue=zipkin}] contains an error
[CheckResult{ok=false,error=java.lang.RuntimeException: Unable to establish connection to RabbitMQ server}]
如第一行所述,我的问题似乎与rabbitmq 主机本身无关,因为它已启动并正在运行,并为我的微服务提供服务。
我确实在配置中遗漏了一些东西,但我找不到它(我还检查了 this post、this 和 this。
docker-compose.yml
version: '3'
services:
product-composite:
build: microservices/product-composite-service
mem_limit: 350m
environment:
- SPRING_PROFILES_ACTIVE=docker
- CONfig_SERVER_USR=${CONfig_SERVER_USR}
- CONfig_SERVER_PWD=${CONfig_SERVER_PWD}
depends_on:
- "rabbitmq"
- "eureka"
eureka:
build: spring-cloud/eureka-server
mem_limit: 350m
environment:
- SPRING_PROFILES_ACTIVE=docker
- CONfig_SERVER_USR=${CONfig_SERVER_USR}
- CONfig_SERVER_PWD=${CONfig_SERVER_PWD}
depends_on:
- "config"
rabbitmq:
image: rabbitmq:3.7.8-management
mem_limit: 350m
ports:
- 5672:5672
- 15672:15672
healthcheck:
test: ["CMD","rabbitmqctl","status"]
interval: 10s
timeout: 5s
retries: 10
# https://hub.docker.com/r/openzipkin/zipkin
zipkin:
image: openzipkin/zipkin-slim
mem_limit: 512m
expose:
- "9411"
ports:
- "9411:9411"
environment:
- RABBIT_ADDRESSES=rabbitmq
- STORAGE_TYPE=mem
depends_on:
- "rabbitmq"
build.gradle
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.b-thinking.microservices.composite.product'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven {
url 'https://repo.spring.io/milestone'
}
}
ext {
resilience4jVersion = "1.6.1"
}
dependencies {
// Local projects depedencies
// implementation files('../../api/build/libs/api.1.0.0-SNAPSHOT.jar')
// implementation files('../../util/build/libs/util.1.0.0-SNAPSHOT.jar')
implementation project(':api')
implementation project(':util')
// Testing: Use JSR330 injection
// implementation 'javax.inject:javax.inject:1'
// Implementations dependencies
// Standard (actuator - for monitoring and Health)
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// WebFlux (asynchronous Web)
implementation 'org.springframework.boot:spring-boot-starter-webflux'
// SpringFox dependencies
implementation "io.springfox:springfox-boot-starter:3+"
implementation('io.springfox:springfox-spring-webflux:3+')
// Implementation: Spring cloud
implementation('org.springframework.cloud:spring-cloud-starter-config')
implementation('org.springframework.cloud:spring-cloud-starter-stream-rabbit')
implementation('org.springframework.cloud:spring-cloud-starter-stream-kafka')
// Eureka: Service discovery client
implementation('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
// Security
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.security:spring-security-oauth2-resource-server')
implementation('org.springframework.security:spring-security-oauth2-jose')
// Spring Cloud Configuration through config server
// https://stackoverflow.com/questions/30016868/spring-cloud-config-client-not-loading-configuration-from-config-server
implementation('org.springframework.cloud:spring-cloud-starter-config')
implementation('org.springframework.cloud:spring-cloud-starter-bootstrap')
implementation('org.springframework.retry:spring-retry')
implementation('org.springframework.boot:spring-boot-starter-aop')
// CircuitBreaker with Resilience4J
// From Spring Cloud (abstraction layer): https://spring.io/projects/spring-cloud-circuitbreaker
// implementation('org.springframework.cloud:spring-cloud-starter-circuitbreaker-reactor-resilience4j')
// Native from github
implementation("io.github.resilience4j:resilience4j-spring-boot2:${resilience4jVersion}")
implementation("io.github.resilience4j:resilience4j-reactor:${resilience4jVersion}")
// Implementation: Tracing
implementation('org.springframework.cloud:spring-cloud-starter-sleuth')
// implementation('org.springframework.cloud:spring-cloud-starter-zipkin:3.0.0-M4')
implementation "org.springframework.cloud:spring-cloud-sleuth-zipkin"
// Test dependencies
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage',module: 'junit-vintage-engine'
}
testImplementation 'io.projectreactor:reactor-test'
testImplementation('org.springframework.cloud:spring-cloud-stream-test-support')
}
dependencyManagement {
imports {
// mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2020.0.0-M5'
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2020.0.0"
}
}
test {
useJUnitPlatform()
}
application.yml
注意以配置文件'docker'开头,所以rabbit的主机是rabbitmq
app:
eureka-username: u
eureka-password: p
eureka-server: localhost
config-server: localhost
zipkin-server: localhost
# Eureka & Ribbon client & instance
eureka:
client:
serviceUrl:
defaultZone: "http://${app.eureka-username}:${app.eureka-password}@${app.eureka-server}:8761/eureka/"
initialInstanceInfoReplicationIntervalSeconds: 5
registryFetchIntervalSeconds: 5
instance:
leaserenewalIntervalInSeconds: 5
leaseExpirationDurationInSeconds: 5
ribbon:
ServerListRefreshInterval: 5000
NFLoadBalancerPingInterval: 5
spring.cloud.stream:
defaultBinder: rabbit
default.contentType: application/json
rabbit.bindings:
input-products.consumer:
autoBindDlq: true
republishToDlq: true
input-recommendations.consumer:
autoBindDlq: true
republishToDlq: true
input-reviews.consumer:
autoBindDlq: true
republishToDlq: true
bindings:
input-products:
destination: products
group: productsGroup
consumer:
maxAttempts: 3
backOffInitialInterval: 500
backOffMaxInterval: 1000
backOffMultiplier: 2.0
input-recommendations:
destination: recommendations
group: recommendationsGroup
consumer:
maxAttempts: 3
backOffInitialInterval: 500
backOffMaxInterval: 1000
backOffMultiplier: 2.0
input-reviews:
destination: reviews
group: reviewsGroup
consumer:
maxAttempts: 3
backOffInitialInterval: 500
backOffMaxInterval: 1000
backOffMultiplier: 2.0
output-products:
destination: products
producer:
required-groups: auditGroup
output-recommendations:
destination: recommendations
producer:
required-groups: auditGroup
output-reviews:
destination: reviews
producer:
required-groups: auditGroup
sleuth-stream-save:
destination: sleuth-stream-save
group: sleuth-stream
content-type: application/json
#endregion: Microservices streams
spring.rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
# Tracing
spring.zipkin.enabled: true
# spring.zipkin.sender.type: web
spring.zipkin.sender.type: rabbit
spring.zipkin.base-url: http://localhost:9411
spring.zipkin.rabbitmq.addresses: localhost:5672
# // Trace all (normally is only 10% with 0.1)
spring.sleuth.sampler.probability: 1.0
# spring.sleuth.sampler.percentage: 1.0
# WARNING: Exposing all management endpoints over http should only be used during development,must be locked down in production!
logging:
level:
root: DEBUG
---
spring.config.activate.on-profile: docker
app:
eureka-server: eureka
config-server: config
zipkin-server: zipkin
spring.rabbitmq.host: rabbitmq
spring.zipkin.base-url: http://zipkin:9411
spring.zipkin.rabbitmq.addresses: rabbitmq:5672
解决方法
我无法使用 spring-cloud-sleuth-sample-zipkin 应用重现您的问题(它对我有用),这是我所做的:
- 在
org.springframework.amqp:spring-rabbit
中添加了pom.xml
- 在
spring.zipkin.sender.type: rabbit
中添加了application.yml
- 使用 this docker-compose.yml 启动 RabbitMQ
- 在 Rabbit 管理 UI 上手动创建了一个队列(名为
zipkin
) - 启动应用并发出请求
- 手动从队列中取出消息并检查它们是否具有正确的有效负载(他们做到了)
解决此问题的一些提示:
- 您应该会在启动时看到此日志事件
Created new connection: rabbitConnectionFactory#21917b6f:0/SimpleConnection@40803682 [delegate=amqp://guest@127.0.0.1:5672/,localPort= 60265]
- 连接是由AbstractConnectionFactory创建的,可以调试
- Telnet (
telnet localhost 5672
) 可以帮助解决连接问题
尝试使用 sample 使其工作,并尝试使工作示例更接近您的应用(通过添加依赖项),看看两者之间有什么区别以及它会在哪里中断。如果您可以创建重现问题的最小示例应用程序(例如:基于 zipkin 示例),请随时在 GH: https://github.com/spring-cloud/spring-cloud-sleuth 上创建问题并标记我 (@jonatan-ivanov
),我可以看看。
自动回复
终于找到了。我有两个问题
1 - 我为我的 zip 容器使用了 zipkin-slim docker 镜像。此图像不包含 rabbitmq 收集器 rabbitmq collector。我已替换为标准 zipkin 图像
2 - 我不知道为什么,但是从 sleuth/zipkin 到 RabbitMQ 的连接没有重试(我将进一步调查)。因此,如果我很着急并很早就进行测试(当 RabbitMQ 尚不可用时)它会失败,并且不会重试。
我的 docker-compose 相关部分现在是这样的:
rabbitmq:
image: rabbitmq:3.7.8-management
mem_limit: 350m
expose:
- "5672"
- "15672"
ports:
- 5672:5672
- 15672:15672
healthcheck:
test: ["CMD","rabbitmqctl","status"]
interval: 10s
timeout: 5s
retries: 10
# https://hub.docker.com/r/openzipkin/zipkin
zipkin:
#image: openzipkin/zipkin-slim
image: openzipkin/zipkin
mem_limit: 512m
expose:
- "9411"
ports:
- "9411:9411"
environment:
- RABBIT_ADDRESSES=rabbitmq
- STORAGE_TYPE=mem
depends_on:
rabbitmq:
condition: service_healthy
再次感谢 Jonatan Ivanov 帮助我!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。