Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/installation/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 无端口微服务网关_Spring Boot_Microservices_Port_Gateway_Spring Cloud Gateway - Fatal编程技术网

Spring boot 无端口微服务网关

Spring boot 无端口微服务网关,spring-boot,microservices,port,gateway,spring-cloud-gateway,Spring Boot,Microservices,Port,Gateway,Spring Cloud Gateway,我需要一个API网关,它将成为我所有应用程序的“集线器”,但它们都没有端口,因为它们永远不会被直接访问,我无法选择端口,因为我不知道服务器是否有该端口可用。如果可能的话,我没有找到一种方法。有没有一个教程或文档中有这样的例子? 我不知道这是否是一个bug,或者我不知道如何去做,但我并没有发现关于谷歌搜索的很多信息 我有一个在Spring 1.5.2中开发的老应用程序,它使用Zuul依赖项,可以在没有端口的情况下向微服务发出请求,我认为它使用Eureka的实例ID,这在Spring Cloud G

我需要一个API网关,它将成为我所有应用程序的“集线器”,但它们都没有端口,因为它们永远不会被直接访问,我无法选择端口,因为我不知道服务器是否有该端口可用。如果可能的话,我没有找到一种方法。有没有一个教程或文档中有这样的例子? 我不知道这是否是一个bug,或者我不知道如何去做,但我并没有发现关于谷歌搜索的很多信息

我有一个在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
## 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}
我的个人服务
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
## 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]

附言:如果我的英语不好,那很抱歉,它还是一个WIP

好吧,现在它不知怎么起作用了。 首先,我的“person”服务的版本不同,它运行的是Spring2.3.1,而不是Spring2.4.2 另外,当您有一个带有“@LoadBalanced”注释的“restemplate”配置类时,Spring Boot会将您的应用程序视为负载平衡器的另一层,您不能仅通过在属性上插入
Spring.cloud.gateway.routes.0.uri=lb://PERSON-SERVICE
来访问它。从我的配置类中删除“@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
进行注释。如果您只使用“@enableurekaclient”,它将不起作用

这就足够使用没有端口的微服务了。
记住运行
mvn clean
以确保安全

此外,这些行不会更改任何内容
spring.cloud.gateway.enabled=true
spring.cloud.gateway.x-forwarded.port-enabled=false