获取app的信息的方法是在DiscoveryClientRouteDefinitionLocator类型的getRouteDefinitions()方法中:
return Flux.fromIterable(discoveryClient.getServices())
.map(discoveryClient::getInstances)
.filter(instances -> !instances.isEmpty())
.map(instances -> instances.get(0))
.filter(instance -> {
Boolean include = includeExpr.getValue(evalCtxt, instance, Boolean.class);
if (include == null) {
return false;
}
return include;
上面的信息是从discoveryClient属性中获取到的,这个discoveryClient是DiscoveryClientRouteDefinitionLocator类中的一个属性:
关于discoveryClient存储了spring cloud gateway的初始节点services,那么这个节点是怎么读入进来的呢,如下
public DiscoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {
this.discoveryClient = discoveryClient;
this.properties = properties;
if (StringUtils.hasText(properties.getRouteIdPrefix())) {
this.routeIdPrefix = properties.getRouteIdPrefix();
} else {
this.routeIdPrefix = this.discoveryClient.getClass().getSimpleName() + "_";
}
}
@Bean
@ConditionalOnBean(DiscoveryClient.class)
@ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled")
public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) {
return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
}
@Bean
@Primary
public CompositeDiscoveryClient compositeDiscoveryClient(List discoveryClients) {
return new CompositeDiscoveryClient(discoveryClients);
}
@Bean
@ConditionalOnMissingBean
// currently means auto-registration is false. That will change when ZookeeperServiceDiscovery is gone
public ZookeeperDiscoveryClient zookeeperDiscoveryClient(ServiceDiscovery serviceDiscovery) {
return new ZookeeperDiscoveryClient(serviceDiscovery, this.zookeeperDependencies);
}
public ServiceDiscovery build()
{
if ( serializer == null )
{
serializer(new JsonInstanceSerializer(payloadClass));
}
return new ServiceDiscoveryImpl(client, basePath, serializer, thisInstance, watchInstances);
}
@Override
public ServiceDiscovery customize(ServiceDiscoveryBuilder builder) {
// @formatter:off
return builder
.client(this.curator)
.basePath(this.properties.getRoot())
.serializer(this.instanceSerializer)
.build();
// @formatter:on
}
@Bean
@ConditionalOnMissingBean(ServiceDiscoveryCustomizer.class)
public DefaultServiceDiscoveryCustomizer defaultServiceDiscoveryCustomizer(
CuratorFramework curator, ZookeeperDiscoveryProperties properties,
InstanceSerializer serializer) {
return new DefaultServiceDiscoveryCustomizer(curator, properties, serializer);
}
private String root = "/services";
至此,就可以看出spring cloud gateway的根目录是/services。