Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
Nacos service registry for SpringCloud integration with nacos
The functions of service registration are mainly embodied in:
- Service instances are registered with the service registry at startup and unregistered at shutdown.
- The service consumer queries the service registry for available instances.
- The service registry needs to invoke the health check API of the service instance to verify that the request can be processed
ServiceRegistry is the standard for registering services provided by Spring Cloud. Components that integrate into the Spring Cloud to implement service registration implement this interface.
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(a);
/**
* 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);
}
Copy the code
NacosServiceRegistry implements this interface
Nacos SpringCloud integration
Spring-cloud-commons package 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
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.cloud.client.HostInfoEnvironmentPostProcessor
# Failure Analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.cloud.configuration.CompatibilityNotMetFailureAnalyzer
Copy the code
AutoServiceRegistrationAutoConfiguration is service registry configuration class
@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(a) {
if (this.autoServiceRegistration == null && this.properties.isFailFast()) {
throw new IllegalStateException("Auto Service Registration has "
+ "been requested, but there is no AutoServiceRegistration bean"); }}}Copy the code
Inject AutoServiceRegistration AutoServiceRegistrationAutoConfiguration AutoServiceRegistration is an interface, AbstractAutoServiceRegistration implements this interface, and NacosAutoServiceRegistration inherited AbstractAutoServiceRegistration
AbstractAutoServiceRegistration and to achieve the ApplicationListener interface:
ApplicationListener interface:
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
Copy the code
The purpose of methods in interfaces is to listen for a particular event,
AbstractAutoServiceRegistration implements this interface:
@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();
}
Copy the code
AbstractAutoServiceRegistration WebServerInitializedEvent event monitoring, call the bind method, finally call NacosServiceRegistry register method
To sum up: The functions of service registration are mainly reflected in:
- Service instances are registered with the service registry at startup and unregistered at shutdown.
- The service consumer queries the service registry for available instances.
- The service registry needs to invoke the health check API of the service instance to verify that the request can be processed
This is part of SpringCloud’s integration with nacos. ServiceRegistry is the standard for registering services provided by SpringCloud. If you don’t understand anything, or I write wrong place, welcome to give me a comment, we learn together progress together, together to achieve better themselves, improve technology, service business, service work. We work together and make progress together. See you in the next article.