Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of ConsulServiceRegistry in spring cloud

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "the use of ConsulServiceRegistry in spring cloud". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Order

This paper mainly studies the ConsulServiceRegistry of spring cloud.

ServiceRegistry

Springhouse cloudhouse commonshouse 2.1.2.RELEASEsultasources.jarUniverse orgspringframeworkmax cloudbank clienthip serviceregistryhip ServiceRegistry.java

Public interface ServiceRegistry {/ * * 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 The type of the status. * @ return The status of the registration. * @ see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint * / T getStatus (R registration);}

The ServiceRegistry interface defines register, deregister, close, setStatus, getStatus methods.

ConsulServiceRegistry

Springhouse cloudhouse consortium house discoverylue 2.1.2.RELEASEMethsources.jarlemacho orgframeworkqcloudlxxxxxxxxxxxx

Public class ConsulServiceRegistry implements ServiceRegistry {private static Log log = LogFactory.getLog (ConsulServiceRegistry.class); private final ConsulClient client; private final ConsulDiscoveryProperties properties; private final TtlScheduler ttlScheduler; private final HeartbeatProperties heartbeatProperties; public ConsulServiceRegistry (ConsulClient client, ConsulDiscoveryProperties properties, TtlScheduler ttlScheduler, HeartbeatProperties heartbeatProperties) {this.client = client This.properties = properties; this.ttlScheduler = ttlScheduler; this.heartbeatProperties = heartbeatProperties;} @ Override public void register (ConsulRegistration reg) {log.info ("Registering service with consul:" + reg.getService ()) Try {this.client.agentServiceRegister (reg.getService (), this.properties.getAclToken ()); NewService service = reg.getService () If (this.heartbeatProperties.isEnabled () & & this.ttlScheduler! = null & & service.getCheck ()! = null & & service.getCheck (). GetTtl ()! = null) {this.ttlScheduler.add (reg.getInstanceId ()) }} catch (ConsulException e) {if (this.properties.isFailFast ()) {log.error ("Error registering service with consul:" + reg.getService (), e) ReflectionUtils.rethrowRuntimeException (e);} log.warn ("Failfast is false. Error registering service with consul: "+ reg.getService (), e);}} @ Override public void deregister (ConsulRegistration reg) {if (this.ttlScheduler! = null) {this.ttlScheduler.remove (reg.getInstanceId ()) } if (log.isInfoEnabled ()) {log.info ("Deregistering service with consul:" + reg.getInstanceId ());} this.client.agentServiceDeregister (reg.getInstanceId (), this.properties.getAclToken ()) } @ Override public void close () {} @ Override public void setStatus (ConsulRegistration registration, String status) {if (status.equalsIgnoreCase (OUT_OF_SERVICE.getCode () {this.client.agentServiceSetMaintenance (registration.getInstanceId (), true) } else if (status.equalsIgnoreCase (UP.getCode () {this.client.agentServiceSetMaintenance (registration.getInstanceId (), false);} else {throw new IllegalArgumentException ("Unknown status:" + status) } @ Override public Object getStatus (ConsulRegistration registration) {String serviceId = registration.getServiceId (); Response response = this.client.getHealthChecksForService (serviceId, QueryParams.DEFAULT); List checks = response.getValue () For (Check check: checks) {if (check.getServiceId (). Equals (registration.getInstanceId () {if (check.getName (). EqualsIgnoreCase ("Service Maintenance Mode")) {return OUT_OF_SERVICE.getCode () }} return UP.getCode ();}}

ConsulServiceRegistry implements the ServiceRegistry method, and its constructor receives ConsulClient, ConsulDiscoveryProperties, TtlScheduler, HeartbeatProperties parameters

Its regiter method uses ConsulClient's agentServiceRegister registration service, and if heartbeat is turned on and ttl is set, it registers a deferred task of heartbeat; the deregister method uses ConsulClient's agentServiceDeregister to log out of the service and removes the deferred task of the instance from ttlScheduler

The setStatus method executes client.agentServiceSetMaintenance (registration.getInstanceId (), true) on OUT_OF_SERVICE and client.agentServiceSetMaintenance (registration.getInstanceId (), false) on UP; the getStatus method gets the Check list through client.getHealthChecksForService (serviceId,QueryParams.DEFAULT) and returns OUT_OF_SERVICE as soon as the check of Service Maintenance Mode is available

ConsulRegistration

SpringMurray cloudhouse consortium house discoveryMuray 2.1.2.RELEASEMethsources.jarlemachorgframeworkqcloudlxxxxxxxxxx

Public class ConsulRegistration implements Registration {private final NewService service; private ConsulDiscoveryProperties properties; public ConsulRegistration (NewService service, ConsulDiscoveryProperties properties) {this.service = service; this.properties = properties;} public NewService getService () {return this.service;} protected ConsulDiscoveryProperties getProperties () {return this.properties } public String getInstanceId () {return getService () .getId ();} public String getServiceId () {return getService () .getName ();} @ Override public String getHost () {return getService () .getAddress () } @ Override public int getPort () {return getService () .getPort ();} @ Override public boolean isSecure () {return this.properties.getScheme () .equalsIgnoreCase ("https");} @ Override public URI getUri () {return DefaultServiceInstance.getUri (this) @ Override public Map getMetadata () {return ConsulServerUtils.getMetadata (getService () .getTags ());}}

ConsulRegistration implements the Registration interface, and its getMetadata method returns ConsulServerUtils.getMetadata (getService (). GetTags ())

ConsulServiceRegistryAutoConfiguration

SpringMurray cloudhouse consortium house discoverycombe 2.1.2.RELEASEMethsources.jarlemachorgAccording springframeworkqcloudlerbank serviceregistrylap ConsulServiceRegistryAutoConfiguration.java

@ Configuration@ConditionalOnConsulEnabled@ConditionalOnProperty (value = "spring.cloud.service-registry.enabled", matchIfMissing = true) @ AutoConfigureBefore (ServiceRegistryAutoConfiguration.class) public class ConsulServiceRegistryAutoConfiguration {@ Autowired (required = false) private TtlScheduler ttlScheduler @ Bean @ ConditionalOnMissingBean public ConsulServiceRegistry consulServiceRegistry (ConsulClient consulClient, ConsulDiscoveryProperties properties, HeartbeatProperties heartbeatProperties) {return new ConsulServiceRegistry (consulClient, properties, this.ttlScheduler, heartbeatProperties) } @ Bean @ ConditionalOnMissingBean @ ConditionalOnProperty ("spring.cloud.consul.discovery.heartbeat.enabled") public TtlScheduler ttlScheduler (ConsulClient consulClient, HeartbeatProperties heartbeatProperties) {return new TtlScheduler (heartbeatProperties, consulClient);} @ Bean @ ConditionalOnMissingBean public HeartbeatProperties heartbeatProperties () {return new HeartbeatProperties () @ Bean @ ConditionalOnMissingBean public ConsulDiscoveryProperties consulDiscoveryProperties (InetUtils inetUtils) {return new ConsulDiscoveryProperties (inetUtils);}}

ConsulServiceRegistryAutoConfiguration registered ConsulServiceRegistry, TtlScheduler, HeartbeatProperties, ConsulDiscoveryProperties

Summary

ConsulServiceRegistry implements the ServiceRegistry method, and its constructor receives ConsulClient, ConsulDiscoveryProperties, TtlScheduler, HeartbeatProperties parameters

Its regiter method uses ConsulClient's agentServiceRegister registration service, and if heartbeat is turned on and ttl is set, it registers a deferred task of heartbeat; the deregister method uses ConsulClient's agentServiceDeregister to log out of the service and removes the deferred task of the instance from ttlScheduler

The setStatus method executes client.agentServiceSetMaintenance (registration.getInstanceId (), true) on OUT_OF_SERVICE and client.agentServiceSetMaintenance (registration.getInstanceId (), false) on UP; the getStatus method gets the Check list through client.getHealthChecksForService (serviceId,QueryParams.DEFAULT) and returns OUT_OF_SERVICE as soon as the check of Service Maintenance Mode is available

This is the end of the introduction to "the use of ConsulServiceRegistry in spring cloud". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report