GateWay全局网管

编程

API文档地址:https://cloud.spring.io/spring-cloud-gateway/2.2.x/reference/html/#gateway-request-predicates-factories

 

1.导入pom

<dependencies>

<!--gateway 路由-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-gateway</artifactId>

</dependency>

<!-- hystrix -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

<!--SpringCloud consul-server 注册中心consul-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-consul-discovery</artifactId>

</dependency>

<!--&lt;!&ndash;一定要保证安装的zookeeper版本和导入的包版本一致&ndash;&gt;-->

<!--<dependency>-->

<!--<groupId>org.springframework.cloud</groupId>-->

<!--<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>-->

<!--<exclusions>-->

<!--&lt;!&ndash;先排除自带的zookeeper3.5.3&ndash;&gt;-->

<!--<exclusion>-->

<!--<groupId>org.apache.zookeeper</groupId>-->

<!--<artifactId>zookeeper</artifactId>-->

<!--</exclusion>-->

<!--<exclusion>-->

<!--<groupId>org.slf4j</groupId>-->

<!--<artifactId>slf4j-log4j12</artifactId>-->

<!--</exclusion>-->

<!--<exclusion>-->

<!--<groupId>log4j</groupId>-->

<!--<artifactId>log4j</artifactId>-->

<!--</exclusion>-->

<!--</exclusions>-->

<!--</dependency>-->

<!--&lt;!&ndash;添加zookeeper3.4.9版本&ndash;&gt;-->

<!--<dependency>-->

<!--<groupId>org.apache.zookeeper</groupId>-->

<!--<artifactId>zookeeper</artifactId>-->

<!--<version>3.4.6</version>-->

<!--</dependency>-->

<!--注册中心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-starter-actuator</artifactId>-->

<!--</dependency>-->

<!--热部署-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<scope>runtime</scope>

<optional>true</optional>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-autoconfigure</artifactId>

</dependency>

</dependencies>

2 编写yml配置文件

server:

port: 9527

spring:

application:

name: cloud-getway9527

cloud:

gateway:

discovery:

locator:

enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由

routes:

- id: payment_rout #路由的id,没有固定规则但是要求唯一,建议配合服务名

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

uri: lb://cloud-payment-service #匹配后提供服务的路由地址

predicates:

- Path=/payment/** #断言,路径相匹配的进行路由

- id: payment_rout2 #路由的id,没有固定规则但是要求唯一,建议配合服务名

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

uri: lb://cloud-payment-service #匹配后提供服务的路由地址

predicates: #断言,就是匹配规则

- Path=/hystrix/** #路径相匹配的进行路由

# filters: #可以通过配置过滤器进行过滤

eureka:

instance:

hostname: cloud-geteway-service

consul:

# consul注册中心地址

host: localhost

port: 8500

discovery:

hostname: 127.0.0.1

service-name: ${spring.application.name}

3.通过代码的方式配置断言(就是匹配规则)

package com.shi.getwat.config;

import org.springframework.cloud.gateway.route.RouteLocator;

import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* 路由的代码配置方式

*/

@Configuration

public class GateWayConfig {

/**

* myRoutId 是路由id

* 用户访问:http://localhost:9527/shiGateWay 就会跳转到

* https://spring.io/projects/spring-cloud-gateway 这个网址去

*

* @param builder

* @return

*/

@Bean

public RouteLocator myGateWayTest(RouteLocatorBuilder builder) {

RouteLocatorBuilder.Builder routes = builder.routes();

routes.route("myRoutId",

r -> r.path("/shiGateWay")

.uri("https://spring.io/projects/spring-cloud-gateway")).build();

return routes.build();

}

}

4.配置自定义过滤器

package com.shi.getwat.filter;

import org.apache.commons.lang.StringUtils;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;

import org.springframework.cloud.gateway.filter.GlobalFilter;

import org.springframework.core.Ordered;

import org.springframework.http.HttpStatus;

import org.springframework.stereotype.Component;

import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;

/**

* 自定义全局过滤器进行过滤

*/

@Component

public class GlouFilterConfig implements GlobalFilter, Ordered {

/**

* 我这边要求请求参数中必须携带name进行过滤,

*

* @param exchange

* @param chain

* @return

*/

@Override

public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

System.out.println("********全局过滤器开始拦截**********");

String name = exchange.getRequest().getQueryParams().getFirst("name");

if (StringUtils.isEmpty(name)) {

System.out.println("-------name 不存在不合法 禁止通过--------");

exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);

return exchange.getResponse().setComplete();

}

return chain.filter(exchange);

}

@Override

public int getOrder() {

//定义过滤器的顺序,数字越小,优先级越高

return 0;

}

}

5.启动测试

测试全局过滤器:http://localhost:9527/hystrix/ok/1?name=shiye

测试自定义断言: http://localhost:9527/shiGateWay

 

以上是 GateWay全局网管 的全部内容, 来源链接: utcz.com/z/515538.html

回到顶部