3 nacos 服务注册之 SpringCloud 集成 nacos
服务注册的功能主要体现在:
ServiceRegistry 是 Spring Cloud 提供的服务注册的标准。集成到 Spring Cloud 中实现服务注册的组件,都会实现该接口。
ServiceRegistry 接口
public interface ServiceRegistry<R extends Registration> {
/**
* Registers the registration. A registration typically has information about an
* instance, such as its hostname and port.
* @param registration registration meta data
*/
void register(R registration);
/**
* Deregisters the registration.
* @param registration registration meta data
*/
void deregister(R registration);
/**
* Closes the ServiceRegistry. This is a lifecycle method.
*/
void close();
/**
* Sets the status of the registration. The status values are determined by the
* individual implementations.
* @param registration The registration to update.
* @param status The status to set.
* @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint
*/
void setStatus(R registration, String status);
/**
* Gets the status of a particular registration.
* @param registration The registration to query.
* @param <T> The type of the status.
* @return The status of the registration.
* @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint
*/
<T> T getStatus(R registration);
}
复制代码
这个接口定义的方法有 register()注册方法,deregister()不进行注册的方法,close()关闭注册的方法。
NacosServiceRegistry 实现该接口
SpringCloud 集成 nacos
自动装配
spring-cloud-commons 包的 META-INF/spring.factories 自动装配:
# AutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.client.CommonsClientAutoConfiguration,\
org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration,\
org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration,\
org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration,\
org.springframework.cloud.client.hypermedia.CloudHypermediaAutoConfiguration,\
org.springframework.cloud.client.loadbalancer.AsyncLoadBalancerAutoConfiguration,\
org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration,\
org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancerAutoConfiguration,\
org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration,\
org.springframework.cloud.commons.httpclient.HttpClientConfiguration,\
org.springframework.cloud.commons.util.UtilAutoConfiguration,\
org.springframework.cloud.configuration.CompatibilityVerifierAutoConfiguration,\
org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration
复制代码
EnableAutoConfiguration 中定义了自动配置的类 AutoServiceRegistrationAutoConfiguration,AutoServiceRegistrationAutoConfiguration 是服务注册相关的配置类,我们看一下这个类做了什么
AutoServiceRegistrationAutoConfiguration 类
@Configuration(proxyBeanMethods = false)
@Import(AutoServiceRegistrationConfiguration.class)
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
matchIfMissing = true)
public class AutoServiceRegistrationAutoConfiguration {
@Autowired(required = false)
private AutoServiceRegistration autoServiceRegistration;
@Autowired
private AutoServiceRegistrationProperties properties;
@PostConstruct
protected void init() {
if (this.autoServiceRegistration == null && this.properties.isFailFast()) {
throw new IllegalStateException("Auto Service Registration has "
+ "been requested, but there is no AutoServiceRegistration bean");
}
}
}
复制代码
AutoServiceRegistrationAutoConfiguration 类中注入了 AutoServiceRegistration,AutoServiceRegistration 是个接口,而 AbstractAutoServiceRegistration 实现了这个接口,而 NacosAutoServiceRegistration 继承了 AbstractAutoServiceRegistrationAbstractAutoServiceRegistration 同时实现了 ApplicationListener 接口:
ApplicationListener 接口
ApplicationListener 接口的定义如下
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
复制代码
接口中方法的作用是监听某个特定事件,
AbstractAutoServiceRegistration 实现了这个接口
AbstractAutoServiceRegistration 类
AbstractAutoServiceRegistration 类
@Override
@SuppressWarnings("deprecation")
public void onApplicationEvent(WebServerInitializedEvent event) {
bind(event);
}
@Deprecated
public void bind(WebServerInitializedEvent event) {
ApplicationContext context = event.getApplicationContext();
if (context instanceof ConfigurableWebServerApplicationContext) {
if ("management".equals(((ConfigurableWebServerApplicationContext) context)
.getServerNamespace())) {
return;
}
}
this.port.compareAndSet(0, event.getWebServer().getPort());
this.start();
}
复制代码
AbstractAutoServiceRegistration 监听 WebServerInitializedEvent 事件,调用 bind 方法 ,最终调用 NacosServiceRegistry 的 register 方法
总结
这就是 SpringCloud 是如何集成 nacos 的,说到底就是利用了 spring.factories 的自动装配机制,自动装配了 AutoServiceRegistrationAutoConfiguration 类,这个类中又注入了 AutoServiceRegistration 接口的实现类 NacosAutoServiceRegistration 的实例,NacosAutoServiceRegistration 的父类 AbstractAutoServiceRegistration 有监听方法,用来进行服务的启动注册。
总结一下:服务注册的功能主要体现在:
这就是 SpringCloud 集成 nacos 的部分,ServiceRegistry 是 Spring Cloud 提供的服务注册的标准。如果你有什么不懂的地方,或者我写错的地方,欢迎给我留言评论,我们一起学习一起进步,一起成就更好的自己,提升技术,服务业务,服务工作。我们一起努力,共同进步,我们下篇文章见,下篇文章我们将从这个 start()方法入手,分析一下 nacos 是怎么实现服务注册的
评论