Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

spring boot - Gateway for micro services without ports

I need an API Gateway who'll be the "hub" for all my applications, but none of them will have ports cause they'll never be accessed directly and I can't chose a port since I don't know if the server will have that port free. If it is possible, I didn't found a way of doing it. Is there a tutorial or some document with example of that? I don't know if it's a bug or if I didn't understand how to do it but I didn't found much info about that googling around.

I have an old application, made in Spring 1.5.2 who's using Zuul dependencies who can make requests to micro services without ports, I think he uses the Eureka's instance ID, is this possible with Spring Cloud Gateway?

My API Gateway 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

My Person Service 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}

The error log:

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: Sorry if my English is bad, it's still a WIP!

question from:https://stackoverflow.com/questions/65938091/gateway-for-micro-services-without-ports

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay, now it works somehow. First, my "person" service wasn't at the same version, it was running Spring 2.3.1 and not Spring 2.4.2 Also, looks like when you have a "RestTemplate" config class annotated with "@LoadBalanced", Spring Boot treat your application as another layer of the Load Balancer and you can't access it just by puttin spring.cloud.gateway.routes.0.uri = lb://PERSON-SERVICE on the properties. Removing the "@LoadBalanced" from my Config class did the trick. So, what you'll need to run this:

1- An API running as your Eureka Server

2- API Gateway with those properties

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- A micro service running with this name, like the properties below

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

4- The class that have "@SpringBootApplication" need to be annotated with @EnableDiscoveryClient. If you're using JUST "@EnableEurekaClient" it won't work!

And that's enough to use a micro service without port. Remember to run a mvn clean just to be sure!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...