2021年1月写于博客园的,现迁移到此。
前言
Gateway Ribbon Nacos分别属于不同组织开发的框架,但是能共同服务于微服务框架中,它们是如何配合的呢
Gateway
1.在自动配置类GatewayDiscoveryClientAutoConfiguration中有一段代码
@Bean
@ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled")
public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
ReactiveDiscoveryClient discoveryClient,
DiscoveryLocatorProperties properties) {
return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
}
当spring.cloud.gateway.discovery.locator.enabled为true,这段代码会从ReactiveDiscoveryClient的实现类中获取服务名称列表封装成路由定义,而Nacos中有对应的实现类为NacosReactiveDiscoveryClient,这是它们的联系点之一
2.当外部请求进来时,例如https://host/服务名/xxx,会经过一个全局过滤器,为LoadBalancerClientFilter,这个过滤器的作用是将url的服务名称改成真正服务所在的ip,里面有段关键代码是
protected ServiceInstance choose(ServerWebExchange exchange) {
return loadBalancer.choose(
((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).getHost());
}
上面起作用的是loadBalancer,对应的类型是LoadBalancerClient,这个是gateway框架定义的接口,真正实现类是Ribbon的RibbonLoadBalancerClient,这是gateway和ribbon的关联点
Ribbon
再来看看RibbonLoadBalancerClient的choose方法
public ServiceInstance choose(String serviceId, Object hint) {
Server server = getServer(getLoadBalancer(serviceId), hint);
if (server == null) {
return null;
}
return new RibbonServer(serviceId, server, isSecure(server, serviceId),
serverIntrospector(serviceId).getMetadata(server));
}
上面关键代码是getLoadBalancer(serviceId),如下
protected ILoadBalancer getLoadBalancer(String serviceId) {
return this.clientFactory.getLoadBalancer(serviceId);
}
这段代码目的是取出ILoadBalancer的实现类,其为ZoneAwareLoadBalancer,而它又是继承DynamicServerListLoadBalancer,构造函数如下
public DynamicServerListLoadBalancer(IClientConfig clientConfig, IRule rule, IPing ping,
ServerList<T> serverList, ServerListFilter<T> filter,
ServerListUpdater serverListUpdater) {
super(clientConfig, rule, ping);
this.serverListImpl = serverList;
this.filter = filter;
this.serverListUpdater = serverListUpdater;
if (filter instanceof AbstractServerListFilter) {
((AbstractServerListFilter) filter).setLoadBalancerStats(getLoadBalancerStats());
}
restOfInit(clientConfig);
}
上面的构造函数会自动注入类型ServerList的serverList,而Nacos的NacosServerList实现了其接口,这里就是ribbon和nacos的连接点.
Nacos
再来看看nacos什么时候提供了NacosServerList,看下面配置类
@Configuration(proxyBeanMethods = false)
@ConditionalOnRibbonNacos
public class NacosRibbonClientConfiguration {
@Autowired
private PropertiesFactory propertiesFactory;
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config,
NacosDiscoveryProperties nacosDiscoveryProperties) {
if (this.propertiesFactory.isSet(ServerList.class, config.getClientName())) {
ServerList serverList = this.propertiesFactory.get(ServerList.class, config,
config.getClientName());
return serverList;
}
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(config);
return serverList;
}
@Bean
@ConditionalOnMissingBean
public NacosServerIntrospector nacosServerIntrospector() {
return new NacosServerIntrospector();
}
}
在此配置类中,nacos提供了NacosServerList.另外,NacosServerList里面有方法getServers
private List<NacosServer> getServers() {
try {
String group = discoveryProperties.getGroup();
List<Instance> instances = discoveryProperties.namingServiceInstance()
.selectInstances(serviceId, group, true);
return instancesToServerList(instances);
}
catch (Exception e) {
throw new IllegalStateException(
"Can not get service instances from nacos, serviceId=" + serviceId,
e);
}
}
selectInstances方法主要逻辑是向nacos服务端发起http请求,获取服务的地址列表.