nacos服务注册之SpringCloud 集成nacos
3 nacos服务注册之SpringCloud 集成nacos
服务注册的功能主要体现在:
- 服务实例在启动时注册到服务注册表,并在关闭时注销。
- 服务消费者查询服务注册表,获得可用实例。
- 服务注册中心需要调用服务实例的健康检查API来验证 是否能够处理请求
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继承了AbstractAutoServiceRegistration
AbstractAutoServiceRegistration同时实现了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有监听方法,用来进行服务的启动注册。
总结一下:
服务注册的功能主要体现在:
- 服务实例在启动时注册到服务注册表,并在关闭时注销。
- 服务消费者查询服务注册表,获得可用实例。
- 服务注册中心需要调用服务实例的健康检查API来验证 是否能够处理请求
这就是SpringCloud集成nacos的部分,ServiceRegistry是Spring Cloud提供的服务注册的标准。如果你有什么不懂的地方,或者我写错的地方,欢迎给我留言评论,我们一起学习一起进步,一起成就更好的自己,提升技术,服务业务,服务工作。我们一起努力,共同进步,我们下篇文章见,下篇文章我们将从这个start()方法入手,分析一下nacos是怎么实现服务注册的
- 点赞
- 收藏
- 关注作者
评论(0)