zuul核心人员走了两个,zuul2的研发过久,spring公司等不及,自己研发的Gateway网关。
zuul路由网管,阳哥没讲,跳过,没笔记。
https://github.com/Netflix/zuul/wiki
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/
新建模块cloud-gateway-gateway9527
pom
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
<dependency>
<groupId>com.angenin.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>${project.version}version>
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>
yml
server:
port: 9527
spring:
application:
name: cloud-gateway
eureka:
instance:
hostname: cloud-gateway-service
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
主启动类GatewayMain9527
@SpringBootApplication
@EnableEurekaClient
public class GatewayMain9527 {
public static void main(String[] args) {
SpringApplication.run(GatewayMain9527.class, args);
}
}
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_route # 路由的id,没有规定规则但要求唯一,建议配合服务名
#匹配后提供服务的路由地址
uri: http://localhost:8001
predicates:
- Path=/payment/get/** # 断言,路径相匹配的进行路由
- id: payment_route2
uri: http://localhost:8001
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
eureka:
instance:
hostname: cloud-gateway-service
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
测试,启动7001,cloud-provider-payment8001,9527
http://localhost:8001/payment/get/1
http://localhost:9527/payment/get/1
在配置文件中配置
在配置文件yml中配置
在配置类中配置
代码中注入RouteLocator的Bean
新建config.GatewayConfig
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("path_route_angenin", //id
r -> r.path("/guonei") //访问 http://localhost:9527/guonei
.uri("http://news.baidu.com/guonei")); //就会转发到 http://news.baidu.com/guonei
routes.route("path_route_angenin2", //id
r -> r.path("/guoji") //访问 http://localhost:9527/guoji
.uri("http://news.baidu.com/guoji")); //就会转发到 http://news.baidu.com/guonji
return routes.build();
}
// @Bean
// public RouteLocator customRouteLocator2(RouteLocatorBuilder routeLocatorBuilder){
// RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
//
// routes.route("path_route_angenin2", //id
// r -> r.path("/guoji") //访问 http://localhost:9527/guoji
// .uri("http://news.baidu.com/guoji")); //就会转发到 http://news.baidu.com/guonji
//
// return routes.build();
// }
}
测试,启动7001,8001,9527
http://localhost:9527/guonei
http://localhost:9527/guoji
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名称进行路由(默认false)
routes:
- id: payment_route #路由的id,没有规定规则但要求唯一,建议配合服务名
# uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://cloud-payment-service
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- id: payment_route2
# uri: http://localhost:8001
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
测试,启动7001,8001,8002,9527
http://localhost:9527/payment/lb
官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gateway-request-predicates-factories
新建测试类T2
public class T2 {
public static void main(String[] args) {
//获取当前时间串
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
//2020-06-17T11:53:40.325+08:00[Asia/Shanghai]
}
}
然后在yml中的predicates:
加上
#指定时间后才能访问(After)时间往后写一小时
- After=2020-06-17T12:53:40.325+08:00[Asia/Shanghai]
测试,启动7001,8001,8002,9527
http://localhost:9527/payment/lb
Before和Between作用一样:
#指定时间前才能访问(Before)
- Before=2020-06-17T11:53:40.325+08:00[Asia/Shanghai]
#指定时间内才能访问(Between)
- Between=2020-06-17T11:53:40.325+08:00[Asia/Shanghai],2020-06-17T12:53:40.325+08:00[Asia/Shanghai]
在yml中的predicates:
加上(记得把after的时间改成已经过去的时间,时间没到访问不了)
- Cookie=username,angenin #带Cookie,并且username的值为angenin
不带cookie访问
打开终端,输入curl http://localhost:9527/payment/lb
(直接访问失败)
带cookie访问
输入curl http://localhost:9527/payment/lb --cookie "username=angenin"
# - After=2020-06-17T12:53:40.325+08:00[Asia/Shanghai]
# - Cookie=username,angenin #带Cookie,username的值为angenin
- Header=X-Request-Id, \d+ #请求头要有 X-Request-Id属性并且值为整数的正则表达式
重启9527,然后在终端输入http://localhost:9527/payment/lb -H "X-Request-Id:123"
- Host=**.angenin.com #Host: xxx.angenin.com 请求是Host必须有**.angenin.com
重启9527
http://localhost:9527/payment/lb -H "Host: www.angenin.com"
- Method=GET #只允许get请求访问
#访问的url地址有 /payment/lb/ 才能访问
- Path=/payment/lb/**
已经用过了,这里不进行演示。
- Query=username, \d+ #url请求地址必须带上username参数,并且值必须为整数
http://localhost:9527/payment/lb?username=110
GatewayFilter(31种)
Global Filter(10种)
这里以AddRequestParameter
为代表。
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("**************come in MyLogGateWayFilter:" + new Date());
//获取request中的uname参数
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if(uname == null){
log.info("*******用户名为null,非法用户!!");
//设置响应,不被接受
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
//返回chain.filter(exchange),放行
return chain.filter(exchange);
}
@Override
public int getOrder() {
//返回值是过滤器的优先级,越小优先级越高(最小-2147483648,最大2147483648)
return 0;
}
}
启动7001,8001,8002,9527
http://localhost:9527/payment/lb?uname=111
http://localhost:9527/payment/lb?xxx=111
下一篇笔记:SpringCloud入门学习笔记(13-14初级部分,服务配置【Config】与消息总线【Bus】)
学习视频(p65-p73):https://www.bilibili.com/video/BV18E411x7eT?p=65