问题描述
学习SpringCloud使用Gateway网关访问项目时,出现503错误。
503跟404错误很像,503是资源不允许访问,404是访问资源不存在。
其中application.yml配置路由等信息如下:
server:
port: 10086 # 网关端口
spring:
application:
name: gateway
cloud:
nacos:
server-addr: localhost:8848
gateway:
routes:
- id: user-service
uri: lb://userservice # 路由的目标地址 lb就是负载均衡,后面跟服务名称
predicates:
- Path=/user/**
但是将配置修改成后,503错误又消失了,修改路由目标地址:
server:
port: 10086
spring:
application:
name: gateway
cloud:
nacos:
server-addr: localhost:8848
gateway:
routes:
- id: user-service
uri: http://127.0.0.1:8081 # 修改此处
predicates:
- Path=/user/**
原因分析:
这也是nacos中设置了隔离空间的原因。
只要在配置gateway配置文件中添加和userservice项目相同的隔离空间,即可完成负载均衡方式的访问,而不是使用固定的ip地址。
具体为什么会这样笔者也不清楚,也希望有 达神 从源码方面讲解一下,或者有无好的阅读连接发送在评论区,希望得到源码方面的解答,谢谢。
server:
port: 10010
spring:
application:
name: gateway
cloud:
nacos:
server-addr: localhost:8848
discovery:
namespace: d06b349d-5833-4986-8015-10d13084f2be # 配置隔离空间
gateway:
routes:
- id: user-service
uri: lb://userservice # 负载均衡方式访问
predicates:
- Path=/user/**
本文探讨了在使用SpringCloud Gateway时遇到503错误的问题,并通过对比两种不同的路由配置,揭示了错误产生的原因及解决方案。指出正确配置Nacos的隔离空间对于实现负载均衡访问的重要性。
3589

被折叠的 条评论
为什么被折叠?



