gpt4 book ai didi

spring-boot - Zipkin 无法在 Docker 中工作 - 连接被拒绝

转载 作者:行者123 更新时间:2023-12-03 07:59:29 26 4
gpt4 key购买 nike

Zipkin 在本地运行良好,但在 docker 容器中运行不佳。所有的微服务都在Eureka中注册良好,并且可以很好地通信。但唯一的问题是Zipkin。我收到以下错误:

org.springframework.web.client.ResourceAccessException: I/O error onPOST request for "http://localhost:9411/api/v2/spans": Connect tohttp://localhost:9411 [localhost/127.0.0.1] failed: Connection refused

我的 docker-compose.yaml 如下:

version: '3.8'

services:
currency-exchange:
image: samankt/springboot-udemy-currency-exchange:0.0.1-SNAPSHOT
mem_limit: 512m
ports:
- '8000:8000'
networks:
- saman-network
environment:
EUREKA.CLIENT.SERVICE-URL.DEFAULTZONE: http://naming-server:8761/eureka
EUREKA.INSTANCE.PREFERIPADDRESS: true
SPRING.ZIPKIN.BASE-URL: http://zipkin-server:9411/
RABBIT_URI: amqp://guest:guest@rabbitmq:5672
SPRING_RABBITMQ_HOST: rabbitmq
SPRING_ZIPKIN_SENDER_TYPE: rabbit
depends_on:
- naming-server
- rabbitmq

api-gateway:
image: samankt/springboot-udemy-currency-api-gateway:0.0.1-snapshot
mem_limit: 512m
ports:
- '8765:8765'
networks:
- saman-network
environment:
EUREKA.CLIENT.SERVICE-URL.DEFAULTZONE: http://naming-server:8761/eureka
EUREKA.INSTANCE.PREFERIPADDRESS: true
SPRING.ZIPKIN.BASE-URL: http://zipkin-server:9411/
RABBIT_URI: amqp://guest:guest@rabbitmq:5672
SPRING_RABBITMQ_HOST: rabbitmq
SPRING_ZIPKIN_SENDER_TYPE: rabbit
depends_on:
- naming-server
- rabbitmq

currency-converter:
image: samankt/currency-conversion:0.0.1-SNAPSHOT
mem_limit: 700m
ports:
- '8100:8100'
networks:
- saman-network
environment:
EUREKA.CLIENT.SERVICE-URL.DEFAULTZONE: http://naming-server:8761/eureka
EUREKA.INSTANCE.PREFERIPADDRESS: true
SPRING.ZIPKIN.BASE-URL: http://zipkin-server:9411/
SPRING.ZIPKIN.DISCOVERYCLIENTENABLED: true
RABBIT_URI: amqp://guest:guest@rabbitmq:5672
SPRING_RABBITMQ_HOST: rabbitmq
SPRING_ZIPKIN_SENDER_TYPE: rabbit
depends_on:
- naming-server
- rabbitmq

naming-server:
image: samankt/naming-server:0.0.1-SNAPSHOT
mem_limit: 512m
ports:
- '8761:8761'
environment:
SPRING.ZIPKIN.BASE-URL: http://zipkin-server:9411/
networks:
- saman-network

zipkin-server:
image: openzipkin/zipkin:latest
mem_limit: 400m
ports:
- '9411:9411'
networks:
- saman-network
environment:
RABBIT_URI: amqp://guest:guest@rabbitmq:5672
depends_on:
- rabbitmq
restart: always


rabbitmq:
image: rabbitmq:3.8.12-management
ports:
- '5672:5672'
- '15672:15672'
networks:
- saman-network

networks:
saman-network:

最佳答案

解决方案 1

该错误告诉您 http://localhost:9411/api/v2/spans 上的连接被拒绝。当您在 docker 容器内运行应用程序时,您需要不使用 localhost 发送请求,而是通过 docker 网络(在您的情况下为 saman-network)发送请求。要访问运行 zipkin 的 docker 容器,您需要将请求发送到 http://zipkin-server:9411/api/v2/spans

从 Spring Boot 3 开始,Sleuth 已被弃用,您将需要使用 micrometer 。要使用 zipkin 进行跟踪,您可以将以下 4 个依赖项添加到您的 pom.xml 文件

<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

最后,要更改 Zipkin 端点以发送跟踪信息,您需要将以下内容添加到 application.yaml 文件中。

management:
tracing:
sampling:
probability: 1.0 # only for testing purpose, switch back to 0.1 for production code
zipkin:
tracing:
endpoint: http://localhost:9411/api/v2/spans

使用 Spring 配置文件是一个很好的做法,因此您可以复制application.yaml 文件并将其命名为 application-docker.yaml。在此新文件中,您可以使用 zipkin 容器名称 zipkin-server 修改 Zipkin 端点。

management:
tracing:
sampling:
probability: 1.0 # only for testing purpose, switch back to 0.1 for production code
zipkin:
tracing:
endpoint: http://zipkin-server:9411/api/v2/spans

您可以在环境中传递 SPRING_PROFILES_ACTIVATE=docker 来激活 Spring 配置文件。


解决方案 2

对于您来说,这个解决方案实现起来更快,但我没有测试它。您将 Zipkin URL 作为 docker-compose.yml 内的环境值 SPRING.ZIPKIN.BASE-URL 传递。此属性与 Sleuth 包一起使用。使用 Spring Boot 3 和 micrometer 尝试传递: MANAGEMENT.ZIPKIN.TRACING.ENDPOINT: http://zipkin-server:9411/api/v2/spans

关于spring-boot - Zipkin 无法在 Docker 中工作 - 连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74877968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com