上一篇:SpringCloud 2020-Hystrix断路器(笔记)
上一代zuul 1.X:https://github.com/Netflix/zuul/wiki
当前gateway:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/
Spring Cloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架
源码架构:
核心逻辑:路由转发+执行过滤器链
1、新建Module cloud-gateway-gateway9527
2、POM
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020artifactId>
<groupId>com.liukai.springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-gateway-gateway9527artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
<dependency>
<groupId>com.liukai.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
3、yml
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- id: payment_routh2
uri: http://localhost:8001
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
4、主启动类GateWayMain9527
package com.liukai.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author liukai
* @version 1.0.0
* @ClassName GateWayMain9527.java
* @Description TODO
* @createTime 2021年03月23日 16:27:00
*/
@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {
public static void main(String[] args) {
SpringApplication.run( GateWayMain9527.class,args);
}
}
5、测试: 访问 http://localhost:9527/payment/get/31
添加网关成功
1、官网案例:
2、百度国内新闻网址,需要外网:http://news.baidu.com/guoji
3、自己写一个通过9527网关访问到外网的百度新闻网址
4、在9527的端口里新建cofig包,下面新建类GateWayConfig
package com.liukai.springcloud.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;
/**
* @author liukai
* @version 1.0.0
* @ClassName GateWayConfig.java
* @Description TODO
* @createTime 2021年03月23日 17:14:00
*/
@Configuration
public class GateWayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("path_rote_liukai", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
routes.route("path_rote_liukai", r -> r.path("/mil").uri("http://news.baidu.com/mil")).build();
routes.route("path_rote_liukai", r -> r.path("/finance").uri("http://news.baidu.com/finance")).build();
return routes.build();
}
}
5、测试:访问 http://localhost:9527/guonei
访问 http://localhost:9527/mil
访问 http://localhost:9527/finance
默认情况下Gateway会根据注册中心的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能
1、修改9527的yml
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
#uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://cloud-payment-service
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- id: payment_routh2
#uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
2、依次启动7001,7002,8001,8002,9527。访问 http://localhost:9527/payment/lb
可以发现通过9527访问支付侧实现负载均衡了
- 1.After Route Predicate:表示只有在规定时间之后才能正常访问
例: - After=2021-03-23T21:12:44.759+08:00[Asia/Shanghai]
可以通过以下代码获取上述日期格式:
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);- 2.Before Route Predicate:表示只有在规定时间之前才能正常访问
例: - Before =2021-03-23T21:12:44.759+08:00[Asia/Shanghai]- 3.Between Route Predicate:表示只有在规定时间段之间才能正常访问
例:- Between=2020-03-08T10:59:34.102+08:00[Asia/Shanghai] , 2020-03-08T10:59:34.102+08:00[Asia/Shanghai]- 4.Cookie Route Predicate:表示需要携带指定Cookie的请求才能正常访问
例:- Cookie=username,liukai- 5.Header Route Predicate:表示需要携带指定的请求头才能正常访问
例:- Header=X-Request-Id, \d+ #请求头中要有X-Request-Id属性并且值为整数的正则表达式- 6.Host Route Predicate:表示只有指定的主机才能正常访问
例: - Host=.liukai.com**- 7.Method Route Predicate:表示只有指定的请求方法才能正常访问
例: - Method=GET- 8.Path Route Predicate:表示请求访问的路径所匹配的路由
- 9.Query Route Predicate:表示需要正确查询条件才能正常访问
例: - Query=username, \d+ #要有参数名称并且是正整数才能路由
使用curl测试,打开cmd输入以下,便可以以默认get的请求方式携带cookie访问http://localhost:9527/payment/lb
curl http://localhost:9527/payment/lb --cookie “username=liukai”
可以发现不便携带cookie会访问失败。
Spring Cloud Gateway自带的Filter:
按照生命周期可分为:pre(执行逻辑前)和post (执行逻辑后)
按照种类可以分为:GatewayFilter(单一)和GlobalFilter(全局)
常用的GatewayFilter:AddRequestParameter
filters:
- AddRequestParameter=X-Request-Id,1024
自定义全局GlobalFilter
1、9527下面新建包filter,下面新建类MyLogGateWayFilter
package com.liukai.springcloud.filter;
import lombok.extern.slf4j.Slf4j;
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;
/**
* @author liukai
* @version 1.0.0
* @ClassName MyLogGateWayFilter.java
* @Description TODO
* @createTime 2021年03月23日 21:58:00
*/
@Component
@Slf4j
public class MyLogGateWayFilter implements Ordered, GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if (uname == null){
log.info("*****用户名为空,非法访问!!!");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange); // uname不为空,通过过滤器
}
@Override
public int getOrder() {
return 0; // 返回值越小,优先级越高
}
}
2、测试:依次启动7001,7002,8001,8002,9527。访问 http://localhost:9527/payment/lb?uname=123
访问 http://localhost:9527/payment/lb
可以发现,携带uname参数就能正常访问,不携带uname就不能正常访问,说明全局过滤器设置成功。
SpringCloud 2020-SpringCloud config分布式配置中心(笔记)
此笔记的视频地址:尚硅谷SprngCloud(H版&alibaba框架开发教程(大牛讲授SpringCloud))