SpringCloud生态的网关(第二代),未来会取代Zuul(第一代),Gateway是基于Netty(网络通信框架,可以实现高性能的服务端和客户端)、Reactor以及WebFlux(基于Reactive的Web框架)构建
SpringCloudGateway的优点
SpringCloudGateway的缺点
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.itmuch
gateway
0.0.1-SNAPSHOT
gateway
小程序网关
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-actuator
org.projectlombok
lombok
provided
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-zipkin
org.junit.jupiter
junit-jupiter-api
5.5.0
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.cloud
spring-cloud-alibaba-dependencies
0.9.0.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin
server:
port: 8040
spring:
application:
name: gateway
zipkin:
base-url: http://localhost:9411/
sleuth:
sampler:
#抽样率,默认0.1(10%),设置为1.0表示所有的数据都会上报给Zipkin
probability: 1.0
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
# 让gateway通过服务发现组件找到其他微服务
enabled: true
routes:
# - id: user_route
# uri: lb://user-center
# predicates:
# - Path=/users/**
# - id: content_route
# uri: lb://content-center
# predicates:
# - Path=/shares/**,/admin/**
#自定义路由谓词工厂
- id: after_route
uri: lb://content-center
predicates:
#时间格式必须按照以下格式,参考(注意idea编码集必须改为utf8)TimeBetweenRoutePredicateFactory#main
- TimeBetween=上午9:00, 下午10:00
filters:
#自定义过滤器工厂类PreLogGatewayFilterFactory,随意传两个参数a和b,a=config.getName(),b=config.getValue()
- PreLog=a,b
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
logging:
level:
org.springframework.cloud.gateway: trace
访问内容中心/shares/1
原路径http://localhost:8010
现在使用gateway网关路径http://localhost:8040,网关转发成功,实现了反向代理
转发规律:访问${GATEWAYY_URL}/{微服务X}/** 会转发到微服务X的/**路径
参考:https://www.imooc.com/article/290804
实现需求:设置网络访问的时间段,超出不可访问
spring:
application:
name: gateway
zipkin:
base-url: http://localhost:9411/
discoveryClientEnabled: false
sleuth:
sampler:
# 抽样率,默认是0.1(10%)
probability: 1.0
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
# 让gateway通过服务发现组件找到其他微服务
enabled: true
routes:
#自定义路由谓词工厂
- id: after_route
uri: lb://content-center
predicates:
#时间格式必须按照以下格式,参考(注意idea编码集必须改为utf8)TimeBetweenRoutePredicateFactory#main
- TimeBetween=上午9:00, 下午5:00
附录:IDEA修改编码位utf8的方法
package com.itmuch.gateway;
import lombok.Data;
import java.time.LocalTime;
@Data
public class TimeBeweenConfig {
private LocalTime start;
private LocalTime end;
}
package com.itmuch.gateway;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
@Component
public class TimeBetweenRoutePredicateFactory
extends AbstractRoutePredicateFactory {
public TimeBetweenRoutePredicateFactory() {
super(TimeBeweenConfig.class);
}
/**
* 路由谓词工厂的核心方法
* @param config
* @return
*/
@Override
public Predicate apply(TimeBeweenConfig config) {
LocalTime start = config.getStart();
LocalTime end = config.getEnd();
return exchange -> {
LocalTime now = LocalTime.now();
return now.isAfter(start) && now.isBefore(end);
};
}
/**
* 控制配置类TimeBeweenConfig和配置文件application#predicates的映射关系
* @return
*/
@Override
public List shortcutFieldOrder() {
//TimeBeweenConfig类的两个属性start和end分别对应application中TimeBetween的9:00和17:00两个参数
return Arrays.asList("start", "end");
}
//测试输出SpringCloudGateway的时间格式
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
System.out.println(formatter.format(LocalTime.now()));
}
}
当前时间下午6:49,网关可访问时间为上午9:00-下午5:00所以访问结果应该为404
参考:https://www.imooc.com/article/290816
过滤器生命周期
需求:进入工厂时记录日志
spring:
application:
name: gateway
zipkin:
base-url: http://localhost:9411/
discoveryClientEnabled: false
sleuth:
sampler:
# 抽样率,默认是0.1(10%)
probability: 1.0
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
# 让gateway通过服务发现组件找到其他微服务
enabled: true
routes:
#自定义路由谓词工厂
- id: after_route
uri: lb://content-center
predicates:
#时间格式必须按照以下格式,参考(注意idea编码集必须改为utf8)TimeBetweenRoutePredicateFactory#main
- TimeBetween=上午9:00, 下午10:00
filters:
#自定义过滤器工厂类PreLogGatewayFilterFactory,随意传两个参数a和b,a=config.getName(),b=config.getValue()
- PreLog=a,b
务必注意,自定义的过滤器工厂类必须以GatewayFilterFactory结尾
package com.itmuch.gateway;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
@Slf4j
@Component
public class PreLogGatewayFilterFactory
extends AbstractNameValueGatewayFilterFactory {
@Override
public GatewayFilter apply(NameValueConfig config) {
return ((exchange, chain) -> {
//获取application中配置的两个参数的值,config.getName()=a;config.getValue()=b
log.info("请求进来了...{},{}", config.getName(), config.getValue());
ServerHttpRequest modifiedRequest = exchange.getRequest()
.mutate()
.build();
ServerWebExchange modifiedExchange = exchange.mutate()
.request(modifiedRequest)
.build();
return chain.filter(modifiedExchange);
});
}
}
访问http://localhost:8040/shares/2
参考:https://www.imooc.com/article/290821
三、监控SpringCloudGateway
参考:https://www.imooc.com/article/290828
参考:https://www.imooc.com/article/290824
参考:https://www.imooc.com/article/290828
特别声明:本系列教程(SpringCloudAlibaba)参考自慕课网大目老师提供的网上视频课程,有需要的同学可以自行搜索学习