问题描述
我对微服务架构很陌生。我已经设置了一个 Spring Gateway、Spring Eureka Server 和一个连接到 MongoDB 的简单 Spring Web 微服务。
当我启动所有三个时,它们似乎都能正常工作并正确重定向。但是如果我启动两个微服务而不是一个,然后关闭一个,我会收到 500: Internal Server 错误。
网关中的错误:
2021-04-08 17:23:05.648 ERROR 14088 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [1ff6b9d0-28] 500 Server Error for HTTP GET "/spots/606ee9fd912b5c28b4dc8968"
io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: host.docker.internal/192.168.178.185:60161
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.web.cors.reactive.CorsWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ HTTP GET "/spots/606ee9fd912b5c28b4dc8968" [ExceptionHandlingWebHandler]
Stack trace:
Caused by: java.net.ConnectException: Connection refused: no further information
at java.base/sun.nio.ch.socketChannelImpl.checkConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.socketChannelImpl.finishConnect(SocketChannelImpl.java:779) ~[na:na]
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330) ~[netty-transport-4.1.60.Final.jar:4.1.60.Final]
at io.netty.channel.nio.AbstractNioChannel$AbstractNIoUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.60.Final.jar:4.1.60.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707) ~[netty-transport-4.1.60.Final.jar:4.1.60.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655) ~[netty-transport-4.1.60.Final.jar:4.1.60.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581) ~[netty-transport-4.1.60.Final.jar:4.1.60.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.60.Final.jar:4.1.60.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.60.Final.jar:4.1.60.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.60.Final.jar:4.1.60.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.60.Final.jar:4.1.60.Final]
网关:
application.properties 在网关中:
server.port=8080
spring.application.name=spots-gateway
# Eureka
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka
eureka.client.instance.preferIpAddress = true
eureka.instance.instance-id=${spring.application.name}:${random.uuid}
应用:
@SpringBootApplication
public class SpotsGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(SpotsGatewayApplication.class,args);
}
@Bean
public RouteLocator routes(RouteLocatorBuilder builder,AuthFilter authFilter) {
return builder.routes()
.route(r -> r.path("/spots/**")
.filters(f -> f.filter(authFilter.apply(new AuthFilter.Config())))
.uri("lb://SPOT-SERVICE"))
.build();
}
}
尤里卡服务器:
尤里卡中的application.properties:
server.port=8761
spring.application.name=spots-eureka
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.eviction-interval-timer-in-ms=1000
微服务:
application.properties 服务中:
server.port=0
spring.application.name=spot-service
# MongoDB
spring.data.mongodb.database=Spots
# Eureka
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka
eureka.client.instance.preferIpAddress = true
eureka.instance.instance-id=${spring.application.name}:${random.uuid}
应用:
@EnablediscoveryClient
@SpringBootApplication
public class SpotserviceApplication {
public static void main(String[] args) {
SpringApplication.run(SpotserviceApplication.class,args);
}
}
其余片段:
@RestController
public class SpotsController {
@Autowired
private ISpotsRepository repository;
@GetMapping(value="/spots")
public @ResponseBody
ResponseEntity<List<Spot>> spots() {
return new ResponseEntity<>(repository.findAll(),HttpStatus.OK);
}
我希望并期望 Eureka 只为网关提供工作服务。如何实现?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)