我有一個在本地作業的多服務平臺。它使用 Spring Boot 2.7、Spring Eureka Docker 和 Feign 進行服務間呼叫。現在我已經通過 Docker Compose 啟動了它,當嘗試相互請求時,服務似乎失敗了。然而,我看到在 Eureka 儀表板上注冊的所有服務:

當我在 API-GATEWAY 中收到呼叫時,它會呼叫 IDENTITY-SERVICE。
呼叫者看起來像這樣:主類有:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients("com.xxx.apigateway.service.feign")
public class ApiGatewayApplication {
...
}
呼叫其他服務的服務是:
@FeignClient(name = "identity-service")
public interface IdentityServiceClient {
@RequestMapping(method = RequestMethod.GET, value = "/api/{scannedId}")
ApiKey getApiKey(@PathVariable String scannedId);
}
而application.properties檔案(所有服務基本相同)是:
server.port=8081
eureka.client.serviceUrl.defaultZone=http://eureka:@discovery-service:8010/eureka
eureka.client.register-with-eureka=true
eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.instance.hostname=${HOST_NAME:localhost}
spring.config.import=optional:configserver:http://config-service:8888
spring.cloud.config.uri=http://config-service:8888
spring.cloud.config.name=config-service
spring.cloud.config.discovery.enabled = false
spring.main.web-application-type=reactive
這是 docker-compose 的一部分:
discovery-service:
container_name: discovery-service
image: discovery-service
pull_policy: never
environment:
- SPRING_PROFILES_ACTIVE=docker
expose:
- "8010"
ports:
- "8010:8010"
restart: unless-stopped
networks:
- mynet
api-gateway:
container_name: api-gateway
image: api-gateway
pull_policy: never
environment:
- SPRING_PROFILES_ACTIVE=docker
expose:
- "8081"
ports:
- "8081:8081"
restart: unless-stopped
depends_on:
- config-service
- discovery-service
identity-service:
container_name: identity-service
image: identity-service
pull_policy: never
environment:
- SPRING_PROFILES_ACTIVE=docker
restart: unless-stopped
depends_on:
- config-service
- discovery-service
- api-gateway
嘗試呼叫 IDENTITY-SERIVCE 時 API_GATEWAY 引發的錯誤是:
api-gateway | 2022-09-28 15:10:00.889 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient : [IdentityServiceClient#getAllApiKeys] ---> GET http://identity-service/api/all HTTP/1.1
api-gateway | 2022-09-28 15:10:00.889 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient : [IdentityServiceClient#getAllApiKeys] ---> END HTTP (0-byte body)
api-gateway | 2022-09-28 15:10:00.939 WARN 1 --- [nio-8081-exec-1] o.s.c.l.core.RoundRobinLoadBalancer : No servers available for service: identity-service
api-gateway | 2022-09-28 15:10:00.942 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient : [IdentityServiceClient#getAllApiKeys] <--- HTTP/1.1 503 (51ms)
api-gateway | 2022-09-28 15:10:00.942 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient : [IdentityServiceClient#getAllApiKeys]
api-gateway | 2022-09-28 15:10:00.942 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient : [IdentityServiceClient#getAllApiKeys] Load balancer does not contain an instance for the service identity-service
api-gateway | 2022-09-28 15:10:00.942 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient : [IdentityServiceClient#getAllApiKeys] <--- END HTTP (75-byte body)
api-gateway | 2022-09-28 15:10:00.942 ERROR 1 --- [nio-8081-exec-1] u.f.m.a.service.feign.FeignErrorDecoder : decode exception method: IdentityServiceClient#getAllApiKeys()
api-gateway | 2022-09-28 15:10:00.942 ERROR 1 --- [nio-8081-exec-1] u.f.m.a.service.feign.FeignErrorDecoder : Feign error: HTTP/1.1 503
api-gateway |
api-gateway | feign.Response$ByteArrayBody@7d9a6870
api-gateway | 2022-09-28 15:10:00.942 ERROR 1 --- [nio-8081-exec-1] u.f.m.a.service.feign.FeignErrorDecoder : Throwing exception: null
**...**
api-gateway | feign.RetryableException: Connection refused executing GET http://identity-service/api/all
api-gateway | at feign.FeignException.errorExecuting(FeignException.java:268) ~[feign-core-11.8.jar:na]
api-gateway | Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
api-gateway | Error has been observed at the following site(s):
api-gateway | *__checkpoint ? org.springframework.web.filter.reactive.ServerWebExchangeContextFilter [DefaultWebFilterChain]
api-gateway | *__checkpoint ? org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
api-gateway | *__checkpoint ? HTTP GET "/providers/b691eda6-f021-4534-8b4d-aaa5a27584d" [ExceptionHandlingWebHandler]
api-gateway | Original Stack Trace:
api-gateway | at feign.FeignException.errorExecuting(FeignException.java:268) ~[feign-core-11.8.jar:na]
**...**
api-gateway | Caused by: java.net.ConnectException: Connection refused
由于這在 Docker Compose 之外有效,我認為這是罪魁禍首,但我不確定以及如何解決這個問題。
uj5u.com熱心網友回復:
我以前也遇到過同樣的問題。通過添加這個:
eureka.instance.prefer-ip-address=true
并洗掉它:
# eureka.instance.hostname=${HOST_NAME:localhost}
為我作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/510488.html
標籤:弹簧靴码头工人春云spring-cloud-feign
