Service Lifecycle
This chapter will introduce the service life cycle in the spring cloud ecosystem.
Service Lifecycle
What is the Service Lifecycle? To answer the question of what is the service declaration cycle, we should divide the term into two parts: the first part is the service, and the second part is the life cycle.
- Service: in the microservice architecture, an application that can provide external processing capacity is called service.
- Life cycle: the cycle of a thing from zero to one and then to destruction.
Given the above two concepts, we will put them into the spring cloud ecosystem. Service refers to spring web applications, and life cycle refers to the registration and deregistration of spring web applications in the registry.
The value of the service life cycle:
- Manage the service registration process.
- Customize service instances.
Spring cloud service registration lifecycle implementation
The entry method sh of service registration in spring cloud org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration#start
Basic definition of abstractautoserviceregistration :
public abstract class AbstractAutoServiceRegistration<R extends Registration>
implements AutoServiceRegistration, ApplicationContextAware, ApplicationListener<WebServerInitializedEvent> {}
The registration instance R is defined in the abstractautoserviceregistration class in a generic way. It is a sub interface of the serviceinstance interface. There are different implementations of R in various registry projects.
- The implementation in Nacos is com alibaba. cloud. nacos. registry. NacosRegistration
- The implementation in consumer is org springframework. cloud. consul. serviceregistry. ConsulRegistration
Return to the registered instance R and analyze its parent serviceinstance. The definition code of serviceinstance interface is as follows.
public interface ServiceInstance {default String getInstanceId() {
return null;
}String getServiceId();String getHost();int getPort();boolean isSecure();URI getUri();Map<String, String> getMetadata();default String getScheme() {
return null;
}}
In the above code, how to customize the service instance mainly depends on the meta information for design, and mainly uses the GetMetadata method for processing. Note that this method only obtains, and the specific setting meta information is not directly defined in this interface.
Return to the start method responsible for completing the registration of service instances. The specific code is as follows.
public void start() {
if (!isEnabled()) {
if (logger.isDebugEnabled()) {
logger.debug("Discovery Lifecycle disabled. Not starting");
}
return;
}
if (!this.running.get()) {
this.context.publishEvent(new InstancePreRegisteredEvent(this, getRegistration()));
register();
if (shouldRegisterManagement()) {
registerManagement();
}
this.context.publishEvent(new InstanceRegisteredEvent<>(this, getConfiguration()));
this.running.compareAndSet(false, true);
}}
There are mainly two registration methods in the above code.
- The method register is used for registration of type registration.
- The method registermanagement is used for the registration of managementregistration type.
After clarifying the functions of these two methods, you can start to write the service instance registration life cycle, mainly to execute some behaviors before and after the implementation of these two methods.
So far, the general implementation method has been clarified, and now we will start the specific development, mainly dealing with the registration life cycle of the registration type. The processing flow of the managementregistration type is similar, and there is no relevant description. Define the interface registrationlife cycle, and the definition code is as follows.
public interface RegistrationLifecycle<R extends Registration> extends Ordered {/**
* default order.
*/
int DEFAULT_ORDER = 0;/**
* A method executed before registering the local service with the
* {@link ServiceRegistry}.
* @param registration registration
*/
void postProcessBeforeStartRegister(R registration);/**
* A method executed after registering the local service with the
* {@link ServiceRegistry}.
* @param registration registration
*/
void postProcessAfterStartRegister(R registration);/**
* A method executed before de-registering the local service with the
* {@link ServiceRegistry}.
* @param registration registration
*/
void postProcessBeforeStopRegister(R registration);/**
* A method executed after de-registering the local service with the
* {@link ServiceRegistry}.
* @param registration registration
*/
void postProcessAfterStopRegister(R registration);default int getOrder() {
return DEFAULT_ORDER;
}}
In the above code, there are two methods about the service instance registration phase:
1. The method postprocessbeforestartregister is used to register the pre-processing process.
2. The method postprocessafterstartregister is to register the post-processing process.
There are two methods about the service instance destruction phase:
1. The method postprocessbeforestopregister destroys the pre-processing process.
2. The method postprocessafterstopregister destroys the post-processing process.
After the interface definition is completed, you need to think about how to obtain the specific interface implementation class in the abstractautoserviceregistration class. The most direct way can be through the constructor. The relevant processing code is as follows.
protected AbstractAutoServiceRegistration(ServiceRegistry<R> serviceRegistry,
AutoServiceRegistrationProperties properties,
List<RegistrationManagementLifecycle<R>> registrationManagementLifecycles,
List<RegistrationLifecycle<R>> registrationLifecycles) {
this.serviceRegistry = serviceRegistry;
this.properties = properties;
this.registrationManagementLifecycles = registrationManagementLifecycles;
this.registrationLifecycles = registrationLifecycles;
}
Through the above code, the registrationlifecycle interface can be stored in the abstractautoserviceregistration class. Now let’s code the specific use, mainly to logically supplement the start method. The specific processing code is as follows.
public void start() {
if (!isEnabled()) {
if (logger.isDebugEnabled()) {
logger.debug("Discovery Lifecycle disabled. Not starting");
}
return;
}
if (!this.running.get()) {
this.context.publishEvent(new InstancePreRegisteredEvent(this, getRegistration()));
registrationLifecycles.forEach(
registrationLifecycle -> registrationLifecycle.postProcessBeforeStartRegister(getRegistration()));
register();
this.registrationLifecycles.forEach(
registrationLifecycle -> registrationLifecycle.postProcessAfterStartRegister(getRegistration()));
if (shouldRegisterManagement()) {
this.registrationManagementLifecycles
.forEach(registrationManagementLifecycle -> registrationManagementLifecycle
.postProcessBeforeStartRegisterManagement(getManagementRegistration()));
this.registerManagement();
registrationManagementLifecycles
.forEach(registrationManagementLifecycle -> registrationManagementLifecycle
.postProcessAfterStartRegisterManagement(getManagementRegistration()));}
this.context.publishEvent(new InstanceRegisteredEvent<>(this, getConfiguration()));
this.running.compareAndSet(false, true);
}}