构建项目
provider-8001
provider-8002
gateway-9527
eureka-7001
依赖,注意不要加web依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
yml
server: port: 9527 spring: application: name: gateway-9527
官方路由案例
请求localhost:9527/get会转发到http://httpbin.org:80 ,并且添加了一个请求头
@Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p .path("/get") .filters(f -> f.addRequestHeader("Hello", "World")) .uri("http://httpbin.org:80")) .build(); }
按照上面的例子
@Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p .path("/get1/**") .uri("http://localhost:8001")) .route(p -> p .path("/get2/**") .uri("http://localhost:8002")) .build(); }
访问localhost:9527/get1/abc 匹配localhost:8001/get1/abc,
localhost:9527/get1/abc 不匹配 localhost:8001/abc
访问localhost:9527/get2/def 匹配localhost:8001/get2/def,
localhost:9527/get2/def 不匹配 localhost:8001/def
从eureka中心读取服务数据
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
配置的路由规则为 /get/** 路由到 provider/get/**
方式一:通过yml配置
server: port: 9527 spring: application: name: gateway-9587 cloud: gateway: routes: - id: route_provider #随便起 uri: lb://provider # uri以lb://开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称 predicates: - Path=/get/** eureka: client: service-url: defaultZone: http://localhost:7001/eureka/
方式二:通过Bean方式
@Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p .path("/get/**") .uri("lb://provider") .order(0) .id("route_provider ") ) .build(); }
server: port: 9527 spring: application: name: gateway-9587 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/
@EnableEurekaClient
http://localhost:9527/get/123123 就请求到 http://provider/get/123123
PrefixPath 过滤器
在URL路径前面添加一部分的前缀
server: port: 9527 spring: cloud: gateway: routes: - id: route_provider123123 #随便起 uri: lb://provider # uri以lb://开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称 predicates: - Path=/get/** filters: - PrefixPath=/mypath #在URL路径前面添加一部分的前缀
或者
@Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p .path("/get/**") .filters(f -> f.prefixPath("/mypath")) .uri("lb://provider") .order(0) .id("route_provider ") ) .build(); }
访问 localhost:9527/get/abc 路由到 provider/mypath/get/abc
StripPrefix 过滤器
去掉部分URL路径
server: port: 9527 spring: application: name: gateway-9587 cloud: gateway: routes: - id: route_provider123123 #随便起 uri: lb://provider # uri以lb://开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称 predicates: - Path=/abc/get/** filters: - StripPrefix=1 # 表示在转发时去掉path中的abc
或者
@Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p .path("/abc/get/**") .filters(f -> f.stripPrefix(1)) .uri("lb://provider") .order(0) .id("route_provider ") ) .build(); }
访问http://localhost:9527/abc/get/123 路由到 provider/get/123
- StripPrefix=2
Path=/abc/def/get/**
访问 http://localhost:9527/abc/def/get/123路由到 provider/get/123