<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> 复制代码
@SpringBootApplication public class GatewayServerApplication { public static void main(String[] args) { SpringApplication.run(GatewayServerApplication.class, args); } } 复制代码
spring: application: name: microservice-gateway cloud: gateway: routes: #设置路由:路由id、路由打微服务的uri、断言 - id: microservice-provider #路由ID,全局唯一 uri: http://127.0.0.1:8001 #目标微服务的请求地址和端口 predicates: - Path=/provider/** #路由条件,采用path路由规则 复制代码
内置断言:
spring: cloud: gateway: routes: - id: after_route uri: https://example.org predicates: - After=2017-01-20T17:42:47.789-07:00[America/Denver] 复制代码
spring: cloud: gateway: routes: - id: before_route uri: https://example.org predicates: - Before=2017-01-20T17:42:47.789-07:00[America/Denver] 复制代码
spring: cloud: gateway: routes: - id: between_route uri: https://example.org predicates: - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver] 复制代码
spring: cloud: gateway: routes: - id: cookie_route uri: https://example.org predicates: - Cookie=chocolate, ch.p 复制代码
spring: cloud: gateway: routes: - id: header_route uri: https://example.org predicates: - Header=X-Request-Id, /d+ 复制代码
spring: cloud: gateway: routes: - id: host_route uri: https://example.org predicates: - Host=**.somehost.org,**.anotherhost.org 复制代码
spring: cloud: gateway: routes: - id: method_route uri: https://example.org predicates: - Method=GET,POST 复制代码
Http请求路径匹配
spring: cloud: gateway: routes: - id: path_route uri: https://example.org predicates: - Path=/red/{segment},/blue/{segment} 复制代码
http请求参数匹配
spring: cloud: gateway: routes: - id: query_route uri: https://example.org predicates: - Query=green 复制代码
spring: cloud: gateway: routes: - id: query_route uri: https://example.org predicates: - Query=red, gree. 复制代码
spring: cloud: gateway: routes: - id: remoteaddr_route uri: https://example.org predicates: - RemoteAddr=192.168.1.1/24 复制代码
根据权重负载
spring: cloud: gateway: routes: - id: weight_high uri: https://weighthigh.org predicates: - Weight=group1, 8 - id: weight_low uri: https://weightlow.org predicates: - Weight=group1, 2 复制代码
<!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 复制代码
@SpringBootApplication @EnableDiscoveryClient public class GatewayServerApplication { public static void main(String[] args) { SpringApplication.run(GatewayServerApplication.class, args); } } 复制代码
spring: application: name: microservice-gateway cloud: gateway: routes: #设置路由:路由id、路由打微服务的uri、断言 - id: microservice-provider #路由ID,全局唯一 # uri: http://127.0.0.1:8001 #目标微服务的请求地址和端口 uri: lb://microservice-provider # lb://微服务名 根据微服务名称从注册中心获取地址 predicates: - Path=/provider/** #路由条件,采用path路由规则 ##eureka配置信息 eureka: client: service-url: #注册中心地址列表 defaultZone: http://server7001:7001/eureka/,http://server7002:7002/eureka/,http://server7003:7003/eureka/ instance: instance-id: microservice-gateway6101 # 自定义控制台中微服务名称 prefer-ip-address: true #eureka控制访问路径显示ip #解决eureka控制台中的Info页面错误问题 info: app.name: com.xyz.microservice build.artifactId: $project.artifactId$ #使用maven内置变量project.artifactId和project.version完成对变量的赋值 build.version: $project.version$ 复制代码
通过RewritePath的过滤器,实现把请求路径重写成实际后端微服务的地址
spring: application: name: microservice-gateway cloud: gateway: routes: #设置路由:路由id、路由打微服务的uri、断言 - id: microservice-provider #路由ID,全局唯一 # uri: http://127.0.0.1:8001 #目标微服务的请求地址和端口 uri: lb://microservice-provider # lb://微服务名 根据微服务名称从注册中心获取地址 predicates: - Path=/product/** #路由条件,采用path路由规则 filters: # 实现重写转发路径: http://localhost:6101/product/provider/list --> http://localhost:6101/provider/list - RewritePath=/product/(?<segment>.*), /$/{segment} 复制代码
spring.cloud.gateway.discovery.locator.enabled=true开启后默认通过微服务名称进行匹配
spring: application: name: microservice-gateway cloud: gateway: discovery: locator: enabled: true #开启通过微服务名称默认进行转发规则:如http://localhost:6101/microservice-provider/provider/list lower-case-service-id: true #微服务名称通过小写的方式请求 复制代码
todo 开启spring.cloud.gateway.discovery.locator.enabled=true,启动服务报错