没有端口的微服务网关
Gateway for micro services without ports
我需要一个 API 网关,它将成为我所有应用程序的“集线器”,但其中 none 将具有端口,因为它们永远不会被直接访问,而我可以'我没有选择一个端口,因为我不知道服务器是否会空闲该端口。如果可能的话,我没有找到这样做的方法。是否有教程或一些带有示例的文档?
我不知道这是一个错误还是我不明白该怎么做,但我没有找到太多关于谷歌搜索的信息。
我有一个旧应用程序,在 Spring 1.5.2 中制作,它使用 Zuul 依赖项,可以在没有端口的情况下向微服务发出请求,我认为他使用 Eureka 的实例 ID,这可能与 Spring云网关?
我的 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.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
at io.netty.util.internal.ThreadExecutorMap.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!
好的,现在它可以正常工作了。
首先,我的“个人”服务不是同一版本,它是 运行ning Spring 2.3.1 而不是 Spring 2.4.2
此外,看起来当您有一个“RestTemplate”配置 class 注释为“@LoadBalanced”时,Spring 引导将您的应用程序视为负载均衡器的另一层,您无法仅通过 puttin 访问它spring.cloud.gateway.routes.0.uri = lb://PERSON-SERVICE
关于属性。从我的配置 class 中删除“@LoadBalanced”就可以了。
那么,您需要 运行 这个:
1- API 运行ning 作为您的 Eureka 服务器
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”的class需要用@EnableDiscoveryClient
注解。如果您只使用“@EnableEurekaClient”,它将无法工作!
这就足以使用没有端口的微服务了。
记得运行一个mvn clean
以防万一!
我需要一个 API 网关,它将成为我所有应用程序的“集线器”,但其中 none 将具有端口,因为它们永远不会被直接访问,而我可以'我没有选择一个端口,因为我不知道服务器是否会空闲该端口。如果可能的话,我没有找到这样做的方法。是否有教程或一些带有示例的文档? 我不知道这是一个错误还是我不明白该怎么做,但我没有找到太多关于谷歌搜索的信息。
我有一个旧应用程序,在 Spring 1.5.2 中制作,它使用 Zuul 依赖项,可以在没有端口的情况下向微服务发出请求,我认为他使用 Eureka 的实例 ID,这可能与 Spring云网关?
我的 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.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.58.Final.jar:4.1.58.Final]
at io.netty.util.internal.ThreadExecutorMap.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!
好的,现在它可以正常工作了。
首先,我的“个人”服务不是同一版本,它是 运行ning Spring 2.3.1 而不是 Spring 2.4.2
此外,看起来当您有一个“RestTemplate”配置 class 注释为“@LoadBalanced”时,Spring 引导将您的应用程序视为负载均衡器的另一层,您无法仅通过 puttin 访问它spring.cloud.gateway.routes.0.uri = lb://PERSON-SERVICE
关于属性。从我的配置 class 中删除“@LoadBalanced”就可以了。
那么,您需要 运行 这个:
1- API 运行ning 作为您的 Eureka 服务器
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”的class需要用@EnableDiscoveryClient
注解。如果您只使用“@EnableEurekaClient”,它将无法工作!
这就足以使用没有端口的微服务了。
记得运行一个mvn clean
以防万一!