Spring FeignClient:解析服务端口时出现NumberFormatException
Spring FeignClient: NumberFormatException when parsing service port
我正在使用 Netflix Eureka + Netlifx Zuul 并尝试使用 Feign 客户端将请求从一个微服务发送到另一个微服务。我可以通过 zuul 访问所有微服务,但是,当我尝试从一个微服务向另一个微服务发出请求时,出现错误。
我已经弄清楚了,它与端口解析有关。它抛出 NumberFormatException,因为它在制作 url 时无法解析“8084:80”。如果端口只是 8084,它会工作正常,因为 8084 是我尝试将请求发送到的服务的端口。
feign.RetryableException: Error at index 4 in: "8084:80" executing POST http://prediction/task
at feign.FeignException.errorExecuting(FeignException.java:249) ~[feign-core-10.10.1.jar:na]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:129) ~[feign-core-10.10.1.jar:na]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-10.10.1.jar:na]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) ~[feign-core-10.10.1.jar:na]
at com.sun.proxy.$Proxy98.runTask(Unknown Source) ~[na:na]
at lj.uni.fri.scheduler.service.SchedulerService.setCurrentTime(SchedulerService.java:51) ~[classes/:na]
at jdk.internal.reflect.GeneratedMethodAccessor71.invoke(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
Caused by: java.net.MalformedURLException: Error at index 4 in: "8084:80"
at java.base/java.net.URL.<init>(URL.java:706) ~[na:na]
at java.base/java.net.URL.<init>(URL.java:568) ~[na:na]
at java.base/java.net.URL.<init>(URL.java:515) ~[na:na]
at feign.Client$Default.convertAndSend(Client.java:148) ~[feign-core-10.10.1.jar:na]
at feign.Client$Default.execute(Client.java:103) ~[feign-core-10.10.1.jar:na]
at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:93) ~[spring-cloud-openfeign-core-2.2.3.BUILD-20200514.112700-81.jar:2.2.3.BUILD-SNAPSHOT]
at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:56) ~[spring-cloud-openfeign-core-2.2.3.BUILD-20200514.112700-81.jar:2.2.3.BUILD-SNAPSHOT]
at com.netflix.client.AbstractLoadBalancerAwareClient.call(AbstractLoadBalancerAwareClient.java:104) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:303) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:287) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at rx.internal.util.ScalarSynchronousObservable.call(ScalarSynchronousObservable.java:231) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.util.ScalarSynchronousObservable.call(ScalarSynchronousObservable.java:228) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.drain(OnSubscribeConcatMap.java:286) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.onNext(OnSubscribeConcatMap.java:144) ~[rxjava-1.3.8.jar:1.3.8]
at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:185) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:180) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.call(OperatorRetryWithPredicate.java:127) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:73) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:79) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:45) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Subscriber.setProducer(Subscriber.java:209) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.subscribe(Observable.java:10423) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.subscribe(Observable.java:10390) ~[rxjava-1.3.8.jar:1.3.8]
at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:443) ~[rxjava-1.3.8.jar:1.3.8]
at rx.observables.BlockingObservable.single(BlockingObservable.java:340) ~[rxjava-1.3.8.jar:1.3.8]
at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:83) ~[spring-cloud-openfeign-core-2.2.3.BUILD-20200514.112700-81.jar:2.2.3.BUILD-SNAPSHOT]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119) ~[feign-core-10.10.1.jar:na]
... 15 common frames omitted
Caused by: java.lang.NumberFormatException: Error at index 4 in: "8084:80"
at java.base/java.lang.NumberFormatException.forCharSequence(NumberFormatException.java:84) ~[na:na]
at java.base/java.lang.Integer.parseInt(Integer.java:735) ~[na:na]
at java.base/java.net.URLStreamHandler.parseURL(URLStreamHandler.java:232) ~[na:na]
at java.base/java.net.URL.<init>(URL.java:701) ~[na:na]
... 57 common frames omitted
Process finished with exit code -1
这是我用的feign客户端:
package lj.uni.fri.scheduler.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.time.LocalDateTime;
@FeignClient(name = "prediction")
public interface PredictionServiceFeign {
@PostMapping("/task")
String runTask(@RequestBody LocalDateTime dateTime);
}
这是端口 8084 上服务 运行 的配置:
bootstrap.yml
server:
port: ${PORT:8084}
spring:
application:
name: prediction
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URL:http://localhost:8761}/eureka/
application.yml
eureka:
instance:
non-secure-port: 80
hostname: ${DOMAIN_NAME:localhost:${server.port}}
password: ${EUREKA_USER_PASSWORD:password}
security:
user:
password: ${eureka.password}
prediction:
http:
auth-token-header-name: api-key
auth-token: ${MICROSERVICE_API_KEY:verysafetoken}
我试过删除 "non-secure-port" 但随后它抛出了“8084:8084”的错误
如在调试器中所见,url 除了端口之外是正确的:
我正在使用:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
PS:如果我尝试将 RestTemplate 与 @LoadBalanced 一起使用,也会发生同样的错误。
我发现我在配置文件中犯了一个错误。我不得不改变:
eureka:
instance:
non-secure-port: 80
hostname: ${DOMAIN_NAME:localhost:${server.port}}
收件人:
eureka:
instance:
non-secure-port: 80
hostname: ${DOMAIN_NAME:localhost}
我正在使用 Netflix Eureka + Netlifx Zuul 并尝试使用 Feign 客户端将请求从一个微服务发送到另一个微服务。我可以通过 zuul 访问所有微服务,但是,当我尝试从一个微服务向另一个微服务发出请求时,出现错误。
我已经弄清楚了,它与端口解析有关。它抛出 NumberFormatException,因为它在制作 url 时无法解析“8084:80”。如果端口只是 8084,它会工作正常,因为 8084 是我尝试将请求发送到的服务的端口。
feign.RetryableException: Error at index 4 in: "8084:80" executing POST http://prediction/task
at feign.FeignException.errorExecuting(FeignException.java:249) ~[feign-core-10.10.1.jar:na]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:129) ~[feign-core-10.10.1.jar:na]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-10.10.1.jar:na]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) ~[feign-core-10.10.1.jar:na]
at com.sun.proxy.$Proxy98.runTask(Unknown Source) ~[na:na]
at lj.uni.fri.scheduler.service.SchedulerService.setCurrentTime(SchedulerService.java:51) ~[classes/:na]
at jdk.internal.reflect.GeneratedMethodAccessor71.invoke(Unknown Source) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
Caused by: java.net.MalformedURLException: Error at index 4 in: "8084:80"
at java.base/java.net.URL.<init>(URL.java:706) ~[na:na]
at java.base/java.net.URL.<init>(URL.java:568) ~[na:na]
at java.base/java.net.URL.<init>(URL.java:515) ~[na:na]
at feign.Client$Default.convertAndSend(Client.java:148) ~[feign-core-10.10.1.jar:na]
at feign.Client$Default.execute(Client.java:103) ~[feign-core-10.10.1.jar:na]
at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:93) ~[spring-cloud-openfeign-core-2.2.3.BUILD-20200514.112700-81.jar:2.2.3.BUILD-SNAPSHOT]
at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.execute(FeignLoadBalancer.java:56) ~[spring-cloud-openfeign-core-2.2.3.BUILD-20200514.112700-81.jar:2.2.3.BUILD-SNAPSHOT]
at com.netflix.client.AbstractLoadBalancerAwareClient.call(AbstractLoadBalancerAwareClient.java:104) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:303) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:287) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at rx.internal.util.ScalarSynchronousObservable.call(ScalarSynchronousObservable.java:231) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.util.ScalarSynchronousObservable.call(ScalarSynchronousObservable.java:228) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.drain(OnSubscribeConcatMap.java:286) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.onNext(OnSubscribeConcatMap.java:144) ~[rxjava-1.3.8.jar:1.3.8]
at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:185) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at com.netflix.loadbalancer.reactive.LoadBalancerCommand.call(LoadBalancerCommand.java:180) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.call(OperatorRetryWithPredicate.java:127) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:73) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:79) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:45) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Subscriber.setProducer(Subscriber.java:209) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.subscribe(Observable.java:10423) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.subscribe(Observable.java:10390) ~[rxjava-1.3.8.jar:1.3.8]
at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:443) ~[rxjava-1.3.8.jar:1.3.8]
at rx.observables.BlockingObservable.single(BlockingObservable.java:340) ~[rxjava-1.3.8.jar:1.3.8]
at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
at org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:83) ~[spring-cloud-openfeign-core-2.2.3.BUILD-20200514.112700-81.jar:2.2.3.BUILD-SNAPSHOT]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119) ~[feign-core-10.10.1.jar:na]
... 15 common frames omitted
Caused by: java.lang.NumberFormatException: Error at index 4 in: "8084:80"
at java.base/java.lang.NumberFormatException.forCharSequence(NumberFormatException.java:84) ~[na:na]
at java.base/java.lang.Integer.parseInt(Integer.java:735) ~[na:na]
at java.base/java.net.URLStreamHandler.parseURL(URLStreamHandler.java:232) ~[na:na]
at java.base/java.net.URL.<init>(URL.java:701) ~[na:na]
... 57 common frames omitted
Process finished with exit code -1
这是我用的feign客户端:
package lj.uni.fri.scheduler.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.time.LocalDateTime;
@FeignClient(name = "prediction")
public interface PredictionServiceFeign {
@PostMapping("/task")
String runTask(@RequestBody LocalDateTime dateTime);
}
这是端口 8084 上服务 运行 的配置:
bootstrap.yml
server:
port: ${PORT:8084}
spring:
application:
name: prediction
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URL:http://localhost:8761}/eureka/
application.yml
eureka:
instance:
non-secure-port: 80
hostname: ${DOMAIN_NAME:localhost:${server.port}}
password: ${EUREKA_USER_PASSWORD:password}
security:
user:
password: ${eureka.password}
prediction:
http:
auth-token-header-name: api-key
auth-token: ${MICROSERVICE_API_KEY:verysafetoken}
我试过删除 "non-secure-port" 但随后它抛出了“8084:8084”的错误
如在调试器中所见,url 除了端口之外是正确的:
我正在使用:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
PS:如果我尝试将 RestTemplate 与 @LoadBalanced 一起使用,也会发生同样的错误。
我发现我在配置文件中犯了一个错误。我不得不改变:
eureka:
instance:
non-secure-port: 80
hostname: ${DOMAIN_NAME:localhost:${server.port}}
收件人:
eureka:
instance:
non-secure-port: 80
hostname: ${DOMAIN_NAME:localhost}