Gateway笔记

构建项目

provider-8001

provider-8002

gateway-9527

eureka-7001

 

依赖,注意不要加web依赖 

1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-gateway</artifactId> 4 </dependency> 5

yml 

1server: 2 port: 9527 3 4 5spring: 6 application: 7 name: gateway-9527 8

官方路由案例

请求localhost:9527/get会转发到http://httpbin.org:80 ,并且添加了一个请求头

1 @Bean 2 public RouteLocator myRoutes(RouteLocatorBuilder builder) { 3 return builder.routes() 4 .route(p -> p 5 .path("/get") 6 .filters(f -> f.addRequestHeader("Hello", "World")) 7 .uri("http://httpbin.org:80")) 8 .build(); 9 } 10

按照上面的例子

1 @Bean 2 public RouteLocator myRoutes(RouteLocatorBuilder builder) { 3 return builder.routes() 4 .route(p -> p 5 .path("/get1/**") 6 .uri("http://localhost:8001")) 7 .route(p -> p 8 .path("/get2/**") 9 .uri("http://localhost:8002")) 10 11 .build(); 12 } 13

访问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中心读取服务数据

1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-gateway</artifactId> 4 </dependency> 5 <dependency> 6 <groupId>org.springframework.cloud</groupId> 7 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 8 </dependency> 9

配置的路由规则为 /get/** 路由到  provider/get/**

方式一:通过yml配置

1server: 2 port: 9527 3 4spring: 5 application: 6 name: gateway-9587 7 cloud: 8 gateway: 9 routes: 10 - id: route_provider #随便起 11 uri: lb://provider # uri以lb://开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称 12 predicates: 13 - Path=/get/** 14 15eureka: 16 client: 17 service-url: 18 defaultZone: http://localhost:7001/eureka/ 19

方式二:通过Bean方式

1@Bean 2 public RouteLocator myRoutes(RouteLocatorBuilder builder) { 3 return builder.routes() 4 .route(p -> p 5 .path("/get/**") 6 .uri("lb://provider") 7 .order(0) 8 .id("route_provider ") 9 ) 10 .build(); 11 } 12

 

1server: 2 port: 9527 3 4spring: 5 application: 6 name: gateway-9587 7 8 9eureka: 10 client: 11 service-url: 12 defaultZone: http://localhost:7001/eureka/ 13
1@EnableEurekaClient 2

http://localhost:9527/get/123123   就请求到 http://provider/get/123123

 

PrefixPath 过滤器

在URL路径前面添加一部分的前缀

1server: 2 port: 9527 3spring: 4 cloud: 5 gateway: 6 routes: 7 - id: route_provider123123 #随便起 8 uri: lb://provider # uri以lb://开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称 9 predicates: 10 - Path=/get/** 11 filters: 12 - PrefixPath=/mypath #在URL路径前面添加一部分的前缀 13

或者

1 @Bean 2 public RouteLocator myRoutes(RouteLocatorBuilder builder) { 3 return builder.routes() 4 .route(p -> p 5 .path("/get/**") 6 .filters(f -> f.prefixPath("/mypath")) 7 .uri("lb://provider") 8 .order(0) 9 .id("route_provider ") 10 ) 11 .build(); 12 } 13

 

访问 localhost:9527/get/abc   路由到   provider/mypath/get/abc

 StripPrefix 过滤器

去掉部分URL路径

1server: 2 port: 9527 3 4spring: 5 application: 6 name: gateway-9587 7 cloud: 8 gateway: 9 routes: 10 - id: route_provider123123 #随便起 11 uri: lb://provider # uri以lb://开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称 12 predicates: 13 - Path=/abc/get/** 14 filters: 15 - StripPrefix=1 # 表示在转发时去掉path中的abc 16

或者

1 @Bean 2 public RouteLocator myRoutes(RouteLocatorBuilder builder) { 3 return builder.routes() 4 .route(p -> p 5 .path("/abc/get/**") 6 .filters(f -> f.stripPrefix(1)) 7 .uri("lb://provider") 8 .order(0) 9 .id("route_provider ") 10 ) 11 .build(); 12 } 13

 

访问http://localhost:9527/abc/get/123 路由到  provider/get/123

 

1- StripPrefix=2 2
1Path=/abc/def/get/** 2

访问 http://localhost:9527/abc/def/get/123路由到 provider/get/123

 

 

 

 

代码交流 2021