0
点赞
收藏
分享

微信扫一扫

Gateway的简单使用

笑望叔叔 2022-11-30 阅读 197


构建项目

provider-8001

provider-8002

gateway-9527

eureka-7001

依赖,注意不要加web依赖 

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

application.yml

server:
port: 9527


spring:
application:
name: gateway-9527

 路由规则小案例

静态路由

配置yml方式

server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh #没有固定的规则但是要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由

此处设置的路由规则是 ​​http://localhost:9527/payment/get/​​**   会路由到  ​​http://localhost:8001/payment/get/​​**

而不是 ​​http://localhost:9527/payment/get/​​*  路由到 ​​http://localhost:8001/​​*

此处我修改上面的代码把

uri: http://localhost:8001 #匹配后提供的路由地址

修改为

uri: http://localhost:8001/abc #匹配后提供的路由地址

访问 ​​http://localhost:9527/payment/get/​​1 仍然能 路由到 ​​http://localhost:8001/payment/get/​​1

说明后面的/abc配置没有作用,而且官网里的小案例就是 只有 ipd地址或者 服务名称  

编码方式

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){

RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("payment_routh"
, r->r.path("/payment/get/**").uri("http://localhost:8001"))
.build();
return routes.build();
}

等价于

spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh #没有固定的规则但是要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由

动态路由

以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>

修改启动类 

@EnableEurekaClient

修改yml配置文件

Gateway的简单使用_eureka

server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启注册中心路由功能
routes:
- id: payment_routh #没有固定的规则但是要求唯一,建议配合服务名
uri: lb://CLOUD-PROVIDER-PAYMENT #此处如果有问题,请注意依赖spring-cloud-starter-netflix-eureka-client依赖不能错
#uri: http://localhost:8001 #匹配后提供的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由

Route Predicate路由断言

就是下图中的箭头处下面的规则

Gateway的简单使用_java_02

下面是官网提供的配置规则

​​Spring Cloud Gateway​​

GateWay的Filter

官网地址

​​Spring Cloud Gateway​​

​​Spring Cloud Gateway​​

下面是复制官网上的一段代码,添加请求头  key=X-request-red,value=biue

spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: https://example.org
filters:
- AddRequestHeader=X-Request-red, blue

自定义过滤器

@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("***********come in MyLogGateWayFilter: "+new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("uname");//每次进来后判断带不带uname这个key
if(uname == null){
log.info("*********用户名为null ,非法用户,o(╥﹏╥)o");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); //uname为null非法用户
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}


举报

相关推荐

0 条评论