问题描述
学习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/**