spring-boot - 无端口微服务网关

标签 spring-boot microservices port gateway spring-cloud-gateway

我需要一个 API 网关,它将成为我所有应用程序的“中心”,但它们都没有端口,因为它们永远不会被直接访问,而且我无法选择端口,因为我不知道服务器是否有该端口空闲。如果可以的话,我还没有找到办法。有教程或一些带有示例的文档吗? 我不知道这是否是一个错误,或者我是否不明白如何做到这一点,但我没有找到太多关于谷歌搜索的信息。

我有一个旧的应用程序,在 Spring 1.5.2 中制作,它使用 Zuul 依赖项,可以在没有端口的情况下向微服务发出请求,我认为他使用 Eureka 的实例 ID,这可以通过 Spring Cloud Gateway 实现吗?

我的 API 网关 application.properties

server.port = 8888
spring.application.name = api-gateway
ribbon.ServerListRefreshInterval = 1
ribbon.eureka.enabled = true
ribbon.eureka.ReadTimeout = 60000
ribbon.eureka.ConnectTimeout = 300000

## EUREKA-SERVICE

eureka.client.serviceUrl.defaultZone = ${EUREKA_URI:http://localhost:8761/eureka}
eureka.instance.instance.preferIpAddress = true
eureka.instance.instance.instance-id = ${spring.application.name}:${server.port}:${random.int}
#eureka.hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 60000
hystrix.command.default.execution.timeout.enabled=false

spring.cloud.gateway.enabled = true
spring.cloud.gateway.x-forwarded.port-enabled = false

## ROUTE 0 -> PERSON-SERVICE

spring.cloud.gateway.routes.0.id = person
spring.cloud.gateway.routes.0.instance = person-service
spring.cloud.gateway.routes.0.uri = http://localhost
spring.cloud.gateway.routes.0.serviceUrl = http://localhost
spring.cloud.gateway.routes.0.predicates = Path=/person/api/**
spring.cloud.gateway.routes.0.ribbon.ReadTimeout = 150000

logging.level.org.springframework.cloud.gateway = DEBUG
logging.level.reactor.netty.http.client = DEBUG

我的个人服务application.properties

## SERVIDOR
server.port=0
server.address=localhost
server.servlet.contextPath=/person/api
spring.application.name = person-service

## EUREKA
eureka.client.healthcheck.enabled=true
eureka.instance.preferIpAddress=1
eureka.instance.instance-id=${spring.application.name}:${server.port}:${random.int}
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}

错误日志:

2021-01-28 10:00:25.402 DEBUG 5340 --- [ctor-http-nio-3] o.s.c.g.h.RoutePredicateHandlerMapping   : Route matched: person
2021-01-28 10:00:25.403 DEBUG 5340 --- [ctor-http-nio-3] o.s.c.g.h.RoutePredicateHandlerMapping   : Mapping [Exchange: GET http://localhost:8888/person/api/users] to Route{id='person', uri=http://localhost:80, order=0, predicate=Paths: [/person/api/**], match trailing slash: true, gatewayFilters=[], metadata={}}
2021-01-28 10:00:25.403 DEBUG 5340 --- [ctor-http-nio-3] o.s.c.g.h.RoutePredicateHandlerMapping   : [5074d3a6-1] Mapped to org.springframework.cloud.gateway.handler.FilteringWebHandler@31dd80d9
2021-01-28 10:00:25.403 DEBUG 5340 --- [ctor-http-nio-3] o.s.c.g.handler.FilteringWebHandler      : Sorted gatewayFilterFactories: [[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RemoveCachedBodyFilter@aa4d8cc}, order = -2147483648], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@242a209e}, order = -2147482648], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@66213a0d}, order = -1], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardPathFilter@70c0a3d5}, order = 0], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@3cb8c8ce}, order = 10000], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter@1835d3ed}, order = 10150], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@5c8e67b9}, order = 2147483646], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@474c9131}, order = 2147483647], [GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@1fde0371}, order = 2147483647]]
2021-01-28 10:00:27.574 ERROR 5340 --- [ctor-http-nio-5] a.w.r.e.AbstractErrorWebExceptionHandler : [5074d3a6-1]  500 Server Error for HTTP GET "/person/api/users"

io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: localhost/127.0.0.1:80
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP GET "/person/api/users" [ExceptionHandlingWebHandler]
Stack trace:
Caused by: java.net.ConnectException: Connection refused: no further information
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_271]
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:715) ~[na:1.8.0_271]
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.58.Final.jar:4.1.58.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
    at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_271]

P.S:抱歉,如果我的英语不好,它仍然是一个 WIP!

最佳答案

好的,现在它可以正常工作了。 首先,我的“person”服务不是同一版本,它运行的是 Spring 2.3.1 而不是 Spring 2.4.2 另外,看起来当您有一个用“@LoadBalanced”注释的“RestTemplate”配置类时,Spring Boot 将您的应用程序视为负载均衡器的另一层,并且您无法仅通过 puttin spring.cloud.gateway 来访问它属性上的 .routes.0.uri = lb://PERSON-SERVICE。从我的 Config 类中删除“@LoadBalanced”就可以了。 那么,运行这个程序需要什么:

1- 作为 Eureka 服务器运行的 API

2- 具有这些属性的 API 网关

spring.cloud.gateway.routes.0.id = pessoa
spring.cloud.gateway.routes.0.uri = lb://PESSOA-SERVICE
spring.cloud.gateway.routes.0.predicates = Path=/pessoa/api/**

3- 使用此名称运行的微服务,如下面的属性

server.port=0
server.servlet.contextPath=/person/api
spring.application.name = person-service

4- 具有“@SpringBootApplication”的类需要使用@EnableDiscoveryClient进行注释。如果您只使用“@EnableEurekaClient”,它将无法工作!

这足以使用没有端口的微服务。 请记住运行 mvn clean 以确保万无一失!

关于spring-boot - 无端口微服务网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65938091/

相关文章:

java - X-CSRF-TOKEN 不是由 Spring Boot 生成的

php - 如何更改 wamp 服务器的端口以及如何创建第一个应用程序

postgresql - 打开端口,以便 Windows 7 上的 pgAdmin 可以连接到 VirtualBox 上的 Debian 上的 PostgreSQL

java - Cassandra - 用于选择和更新的分布式行锁定

Docker 公开的端口在浏览器上不可用,但前者运行正常

java - 在 Java 之前编译 Groovy 后,Grails 应用程序(配置文件rest-api)陷入困惑

java - Spring Data Neo4j - 关系实体的(所有)属性

docker - 如何为 Docker Compose 配置 dns 条目

database - 每个微服务单个数据库真的意味着每个微服务单个数据库服务器吗?

java - com.netflix.discovery.shared.transport.TransportException : Cannot execute request on any known server