配置文件如下,注:服务发现什么配置我都是放在配置中心里面的
spring:
application:
#f服务名称
name: gateway-service
cloud:
nacos:
config:
#配置中心地址
server-addr: 127.0.0.1:8848
#配置文件格式
file-extension: yml
#命名空间
namespace: dev
gateway:
routes:
#路由ID
- id: order-service
#符合路由规则后访问的地址
#第一种:ws(websocket)方式: uri: ws://localhost:9000
#第二种:http方式: uri: http://localhost:8130/
#第三种:lb(注册中心中服务名字)方式: uri: lb://order-service
uri: lb://order-service
predicates:
#此处通过符合查询参数进行跳转uri
#如http://localhost:88/order/create?url=baz
- Query=url,baz
第一个问题:版本不一致问题
springboot2.2.2.RELEASE对应的Spring cloud alibaba是2.1.0.RELEASE ,spring cloud 是Hoxton.SR1
具体参考官网:https://start.spring.io/actuator/info
第二个问题:Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.
在gateway.pom文件里面移除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
第一个问题:Invalid host: lb://order_service
这个问题是因为既然是以lb的方式访问,那么能被这种方式识别到的命名规则一定要满足“a-zA-Z:.”,而我这里加了“_”,所以出现此错误
参考:
gateway:https://cloud.tencent.com/developer/article/1403887
nacos:https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md