我的Java版本是17
父模块
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<alibaba-cloud.version>2022.0.0.0alibaba-cloud.version>
<spring-cloud.version>2022.0.4spring-cloud.version>
<spring-boot.version>3.0.9spring-boot.version>
<circuit-breaker.version>3.0.3circuit-breaker.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${alibaba-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
子模块
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
dependencies>
上面的问题,如果引入了网关,由网关统一管理,客户端只需要和网关对接,网关就负责授权、限流、路由转发等。
在网关模块添加这个依赖就行了,注意我这个是子模块,我的父模块管理了SpringCloud的依赖,所以不需要添加版本
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-loadbalancerartifactId>
dependency>
然后写路由就可以使用lb://servername
了。
@Bean
public RouteLocator usrRout(RouteLocatorBuilder builder) {
ZonedDateTime zonedDateTime = LocalDateTime.now()
.plusSeconds(20)
.atZone(ZoneId.systemDefault());
return builder.routes()
.route("user", r -> r.path("/xxxx").uri("lb://oraone"))
.build();
}
@Configure
public class Config {
public RouteLocator config(RouteLocatorBuilder build) {
ZoneDateTime before = ZoneDateTime.now()
.plusSeconds(36)
.atZone(ZoneId.systemDefault());
ZoneDateTime after = ZoneDateTime.now()
.plusSeconds(900)
.atZone(ZoneId.systemDefault());
return build.routes()
// 在before之后永久路由失效
.route("before", r -> r.before(before).uri("lb://servername"))
// 在after之后路由生效。
.route("after", r -> r.after(after).uri("lb://servername"))
// 在[before, after]之内路由生效。
.between("between", r -> r.between(before, after).uri("lb://servername"))
.build();
}
}
@Configure
public class RouteConfig {
@Bean
public RouteLocator route(RouteLocatorBuilder build) {
return build.routes()
// 请求必须有name,和token这两个请求头,token必须是32位指定字符。
// 请求方法必须是get,只能本地请求。
// 参数必须携带id
.route("route", r -> e.header("name")
and().header("token", "[a-zA-Z0-9]{32, 32}")
and().method(HttpMethod.GET)
and().Host("localhost")
and().query("id")
.uri("lb://servername"))
.build();
}
}
@Configure
public class RouteConfig {
@Bean
public RouteLocator route(RouteLocatorBuilder build) {
return build.routes()
.route("path", r -> r.path("/gateway/user/**").uri("lb://servername"))
.build();
}
}
IP地址和网段
RemoteAdd:两个参数,第一个是IP地址,第二个是网段。
权重
Weight:设置接口的权重,权重越高,那么转发到该接口的请求数就越多。有两个参数,第一个个是GroupName,第二个是一个Weight数值。
@Configure
public class RouteConfig {
@Bean
public RouteLocator route(RouteLocatorBuilder build) {
String path = "/gateway/user/**";
String groupName = "user";
return build.routes()
.route("weight1", r -> r.path(path)
and().weight(groupName, 80).uri("lb://servername1"))
.route("weight2", r -> r.path(path)
.and().weight(groupName, 20).uri("lb://servername2")))
.build();
}
}
内置的过滤器大体分为请求头、响应头、跳转、参数处理、响应状态、熔断、限速器。
@Configure
public class RouteConfig {
@Bean
public RouteLocator route(RouteLocatorBuilder build) {
return build.routes()
.route("route", r -> r.path("/gateway/usr/local/**")
.filters(f -> f.rewritePath("/gateway/usr/($.*)" , "/user/$\\{segment}"))
.uri("lb://servername"))
// 最后访问路径是 lb://servername/order/**
.route("route1", r -> r.path("/gateway/order/**")
.filters(f -> f.stripPrefix(1)).uri("lb://servername"))
.build();
}
}
gateway最终需要的资源是RouteLocator,RouteLocator里面包含了路由、断言、过滤器三个种资源,它们的依赖关系如下:
Buildalbe就一个方法build,AbstractBuilder负责构建Route,源码如下:
五个属性,都是路由的基本组成,还看到了GatewayFilter 局部过滤器。
public abstract static class AbstractBuilder<B extends AbstractBuilder<B>> implements Buildable<Route> {
protected String id;
protected URI uri;
protected int order = 0;
protected List<GatewayFilter> gatewayFilters = new ArrayList<>();
protected Map<String, Object> metadata = new HashMap<>();
省略....
}
最后是Buidler和AsyncBuidler,它两负责构建断言,只是构建的类型不同,至于作用是什么目前还不清楚,它们两分别构建Predicate和AsyncPredicate。
SpringCloudGateway提供了很多GatewayFilter过滤器,UriSpec就是这些过滤器的配置,UriSpec里面包含了Route.Builder / RouteLocatorBuilder.Builder,配置的断言和过滤器最终都会流入Route.Builder。
增强器和构造器的协作关系,Route.AsyncBuilder和RouteLocatorBuidler.Builder都组合在UriSpec中。
public static class Builder {
private List<Buildable<Route>> routes = new ArrayList<>();
private ConfigurableApplicationContext context;
public Builder(ConfigurableApplicationContext context) {
this.context = context;
}
// 创建一个Buildable,放到routes中,ID是自命名。
// apply会生成一个PredicateSpec,提供给用户选择过滤器,最后通过UriSpec的uri方法,返回一个Buildable对象。
public Builder route(String id, Function<PredicateSpec, Buildable<Route>> fn) {
Buildable<Route> routeBuilder = fn.apply(new RouteSpec(this).id(id));
add(routeBuilder);
return this;
}
// 创建一个Buildable,放到routes中,ID随机生成。
// apply会生成一个PredicateSpec,提供给用户选择过滤器,最后通过UriSpec的uri方法,返回一个Buildable对象。
public Builder route(Function<PredicateSpec, Buildable<Route>> fn) {
Buildable<Route> routeBuilder = fn.apply(new RouteSpec(this).randomId());
add(routeBuilder);
return this;
}
// 执行所有的Buildable,构造RouteLocator。
public RouteLocator build() {
return () -> Flux.fromIterable(this.routes).map(routeBuilder -> routeBuilder.build());
}
ConfigurableApplicationContext getContext() {
return context;
}
void add(Buildable<Route> route) {
routes.add(route);
}
}
public class UriSpec {
final Route.AsyncBuilder routeBuilder;
final RouteLocatorBuilder.Builder builder;
UriSpec(Route.AsyncBuilder routeBuilder, RouteLocatorBuilder.Builder builder) {
this.routeBuilder = routeBuilder;
this.builder = builder;
}
public UriSpec customize(Consumer<Route.AsyncBuilder> routeConsumer) {
routeConsumer.accept(this.routeBuilder);
return this;
}
public UriSpec replaceMetadata(Map<String, Object> metadata) {
this.routeBuilder.replaceMetadata(metadata);
return this;
}
public UriSpec metadata(Map<String, Object> metadata) {
this.routeBuilder.metadata(metadata);
return this;
}
public UriSpec metadata(String key, Object value) {
this.routeBuilder.metadata(key, value);
return this;
}
// 设置uri,这里会抛出一个Buildalbe,对应构造器的写法:build.routes().route(r -> r.uri()).build();
public Buildable<Route> uri(String uri) {
return this.routeBuilder.uri(uri);
}
/**
* Set the URI for the route.
* @param uri the URI for the route.
* @return a {@link Route.AsyncBuilder}S
*/
public Buildable<Route> uri(URI uri) {
return this.routeBuilder.uri(uri);
}
<T> T getBean(Class<T> type) {
return this.builder.getContext().getBean(type);
}
}
GatewayFilterSpec有filter方法,可以直接添加自己的过滤器。
@Configure
public class Config{
@Bean
public RouteLocator usrRout(RouteLocatorBuilder builder) {
ZonedDateTime zonedDateTime = LocalDateTime.now()
.plusSeconds(20)
.atZone(ZoneId.systemDefault());
return builder.routes()
.route("user", r -> r.before(zonedDateTime).uri("lb://oraone"))
.route("user1", predicate -> predicate
.path("/strip/prefix/**")
.filters(filter -> filter.stripPrefix(2)
// 自定义GatewayFilter
.filter(new MyGatewayFilter()))
.uri("lb://oraone"))
.build();
}
class MyGatewayFilter implements GatewayFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// 什么都不做。
return chain.filter(exchange);
}
}
}
还可以继承GatewayFilterFactory,实现它的apply方法。或者继承AbstractGatewayFilterFactory,实现apply方法。