弊端说明
实际情况下为了安全性考虑,建议 spring.cloud.gateway.discovery.locator.enabled设置为false, 这样就不对外暴露微服务名称,微服务属于内部的服务,没有必要暴露给前端,如果暴露给前端之后,前端调用接口用微服务名称去调用接口的话,那么就会出现一系列维护的问题,比如说前端直接在前的代码写死你微服务名称去调用的,那么后续就不能修改服务名了.
代码地址
https://gitee.com/zjj19941/ZJJ_Neaten5.10/tree/master/ZJJ_Gateway/demo01
配置
spring.cloud.gateway.discovery.locator.enabled默认为false,设为true开启通过微服务创建路由的功能,即可以通过微服务名访问服务
spring:application:name: mall-gateway#配置nacos注册中心地址cloud:nacos:discovery:server-addr: zjj101:8848gateway:discovery:locator:# 默认为false,设为true开启通过微服务创建路由的功能,即可以通过微服务名访问服务# http://localhost:8888/mall-order/order/findOrderByUserId/1enabled: true# 是否开启网关enabled: true
测试
http://localhost:8888/mall-order/order/findOrderByUserId/1
直接通过mall-order去nacos找到对应的服务,然后去访问了

结果
{"msg": "success","code": 0,"orders": [{"id": 1,"userId": "1","commodityCode": "1","count": 1,"amount": 1}]}
