Gateway三大核心概念:
Route(路由):路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
Predicate(断言):参考的是java8的java.util.function.Predicate开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
Filter(过滤);指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
创建端口9527的gateway网关:
依赖:
<dependencies>
<!--新增gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<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>
<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>
</dependencies>
</project>
application.yml:
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,由微服务进行路由
routes:
- id: payment_routh
uri: http://localhost:8001 #匹配后提供服务的路由地址
# uri: lb://cloud-payment-service
predicates:
- Path=/payment/get/**
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
#是否让自己注册EurekaServer 默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息, 默认为true 单节点无所谓,集群必须设置为true,才能配合ribbon负载均衡
fetch-registry: true
defaultZone: http://localhost:7001/eureka
config类:
*/
@Configuration
public class GateWayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("path_route",
r -> r.path("/guonei").uri("https://news.baidu.com/guonei")).build();
return routes.build();
}
}
主启动类:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class GatewayMain9527 {
public static void main(String[] args) {
SpringApplication.run(GatewayMain9527.class,args);
}
}
然后启动eureka7001,payment8001,gateway9527
通过9527来访问8001服务器,让8001服务器没有对外暴露,
且通过9527网关只能访问断言里的路径----/payment/get/**
即localhost:9527/payment/get/**,未断言的则无法通过网关访问
接着是配置文件中通过设置访问localhost:9527/guonei跳转到
https://news.baidu.com/guonei
自定义过滤器:
@Component
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println();
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if(uname==null){
System.out.println("用户名为null");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
实现结果为通过gateway9527网关访问时,必须带关键字uname才能访问。
例如http://localhost:9527/payment/get/213?uname=a
不带uname时: