微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Eureka 向 Spring Gateway 提供死服务

如何解决Eureka 向 Spring Gateway 提供死服务

我对微服务架构很陌生。我已经设置了一个 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 举报,一经查实,本站将立刻删除。