1.依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2.常用配置
spring:
application:
name: cloud-gateway-server
cloud:
# 配置网关
gateway:
#通过注册中心服务名配置路由
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_route #路由ID,没有固定规则,但是唯一
uri: lb://CLOUD-PAYMENT-SERVICE #lb是负载均衡,CLOUD-PAYMENT-SERVICE是注册中心的服务名
predicates:
- Path=/payment/selectPaymentById/** #断言,路径匹配的进行路由,**通配符,代表参数
- id: payment_route1
uri: lb://CLOUD-PAYMENT-SERVICE
predicates:
- Path=/payment/lb
- Cookie=username,wn
# #不使用注册中心服务名的路由
# routes: #路由,可以配置多个
# - id: payment_route #路由ID,没有固定规则,但是唯一
# uri: http://localhost:8001 #匹配后提供服务的路由地址
# predicates:
# - Path=/payment/selectPaymentById/** #断言,路径匹配的进行路由,**通配符,代表参数
#
# - id: payment_route1
# uri: http://localhost:8001
# predicates:
# - Path=/payment/lb