In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use the ConsulServer of spring cloud. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
Order
This paper mainly studies the ConsulServer of spring cloud.
ConsulServer
SpringMurray cloudhouse consortium house discoveryMuray 2.1.2.RELEASEMethsources.jarlemet orgAccording frameworkhand cloudbank consortium discoverybind ConsulServer.java
Public class ConsulServer extends Server {private final MetaInfo metaInfo; private final HealthService service; private final Map metadata; public ConsulServer (final HealthService healthService) {super (findHost (healthService), healthService.getService () .getPort ()); this.service = healthService; this.metadata = ConsulServerUtils.getMetadata (this.service) This.metaInfo = new MetaInfo () {@ Override public String getAppName () {return ConsulServer.this.service.getService () .getService () } @ Override public String getServerGroup () {return getMetadata () .get ("group") } @ Override public String getServiceIdForDiscovery () {return null } @ Override public String getInstanceId () {return ConsulServer.this.service.getService () .getId ();}}; setAlive (isPassingChecks ()) } @ Override public MetaInfo getMetaInfo () {return this.metaInfo;} public HealthService getHealthService () {return this.service;} public Map getMetadata () {return this.metadata } public boolean isPassingChecks () {for (Check check: this.service.getChecks ()) {if (check.getStatus ()! = Check.CheckStatus.PASSING) {return false;}} return true;}}
ConsulServer inherits com.netflix.loadbalancer.Server; and its constructor calls the isPassingChecks method to setAlive, which obtains the state of checks through HealthService
ConsulServerList
SpringMurray cloudhouse consortium house discoveryMuray 2.1.2.RELEASEMethsources.jarlemet orgAccorde springframeworkstop cloudbank consortium discoverybind ConsulServerList.java
Public class ConsulServerList extends AbstractServerList {private final ConsulClient client; private final ConsulDiscoveryProperties properties; private String serviceId; public ConsulServerList (ConsulClient client, ConsulDiscoveryProperties properties) {this.client = client; this.properties = properties;} protected ConsulClient getClient () {return this.client } protected ConsulDiscoveryProperties getProperties () {return this.properties;} protected String getServiceId () {return this.serviceId;} @ Override public void initWithNiwsConfig (IClientConfig clientConfig) {this.serviceId = clientConfig.getClientName () } @ Override public List getInitialListOfServers () {return getServers ();} @ Override public List getUpdatedListOfServers () {return getServers ();} private List getServers () {if (this.client = = null) {return Collections.emptyList () } String tag = getTag (); / / null is ok Response response = this.client.getHealthServices (this.serviceId, tag, this.properties.isQueryPassing (), createQueryParamsForClientRequest (), this.properties.getAclToken ()) If (response.getValue () = = null | | response.getValue () .isEmpty ()) {return Collections.emptyList ();} return transformResponse (response.getValue ());} / * * Transforms the response from Consul in to a list of usable {@ link ConsulServer} s. * @ param healthServices the initial list of servers from Consul. Guaranteed to be * non-empty list * @ return ConsulServer instances * @ see ConsulServer#ConsulServer (HealthService) * / protected List transformResponse (List healthServices) {List servers = new ArrayList (); for (HealthService service: healthServices) {ConsulServer server = new ConsulServer (service) If (server.getMetadata () .containsKey (this.properties.getDefaultZoneMetadataName () {server.setZone (server.getMetadata () .get (this.properties.getDefaultZoneMetadataName () } servers.add (server);} return servers;} / * This method will create the {@ link QueryParams} to use when retrieving the services * from Consul. By default {@ link QueryParams#DEFAULT} is used. In case a datacenter * is specified for the current serviceId {@ link QueryParams#datacenter} is set. * @ return an instance of {@ link QueryParams} * / protected QueryParams createQueryParamsForClientRequest () {String datacenter = getDatacenter (); if (datacenter! = null) {return new QueryParams (datacenter);} return QueryParams.DEFAULT } protected String getTag () {return this.properties.getQueryTagForService (this.serviceId);} protected String getDatacenter () {return this.properties.getDatacenters () .get (this.serviceId);} @ Override public String toString () {final StringBuilder sb = new StringBuilder ("ConsulServerList {") Sb.append ("serviceId='") .append (this.serviceId) .append ('\'); sb.append (", tag=") .append (getTag ()); sb.append ('}'); return sb.toString ();}}
ConsulServerList inherits com.netflix.loadbalancer.AbstractServerList; 's getInitialListOfServers and getUpdatedListOfServers methods to call the getServers method; it gets the HealthService list of the specified serviceId through ConsulClient.getHealthServices, and then wraps it as a ConsulServer list through the transformResponse method
ConsulRibbonClientConfiguration
SpringMurray cloudhouse consortium house discoveryMuray 2.1.2.RELEASEMethsources.jarroomUniverse orgAccording to springframeworkhowcloudlash consultanddiscoverypassion Consulting RibbonClientConfiguration.java
@ Configurationpublic class ConsulRibbonClientConfiguration {protected static final String VALUE_NOT_SET = "_ not__set__"; protected static final String DEFAULT_NAMESPACE = "ribbon"; @ Autowired private ConsulClient client; @ Value ("${ribbon.client.name}") private String serviceId = "client"; public ConsulRibbonClientConfiguration () {} public ConsulRibbonClientConfiguration (String serviceId) {this.serviceId = serviceId } @ Bean @ ConditionalOnMissingBean public ServerList ribbonServerList (IClientConfig config, ConsulDiscoveryProperties properties) {ConsulServerList serverList = new ConsulServerList (this.client, properties); serverList.initWithNiwsConfig (config); return serverList;} @ Bean @ ConditionalOnMissingBean public ServerListFilter ribbonServerListFilter () {return new HealthServiceServerListFilter () @ Bean @ ConditionalOnMissingBean public IPing ribbonPing () {return new ConsulPing ();} @ Bean @ ConditionalOnMissingBean public ConsulServerIntrospector serverIntrospector () {return new ConsulServerIntrospector ();} @ PostConstruct public void preprocess () {setProp (this.serviceId, DeploymentContextBasedVipAddresses.key (), this.serviceId) SetProp (this.serviceId, EnableZoneAffinity.key (), "true");} protected void setProp (String serviceId, String suffix, String value) {/ / how to set the namespace properly? String key = getKey (serviceId, suffix); DynamicStringProperty property = getProperty (key); if (property.get (). Equals (VALUE_NOT_SET)) {ConfigurationManager.getConfigInstance (). SetProperty (key, value);} protected DynamicStringProperty getProperty (String key) {return DynamicPropertyFactory.getInstance (). GetStringProperty (key, VALUE_NOT_SET) } protected String getKey (String serviceId, String suffix) {return serviceId + "." + DEFAULT_NAMESPACE + "." + suffix;}}
ConsulRibbonClientConfiguration injects ribbonServerList, which creates ConsulServerList
Summary
ConsulServer inherits com.netflix.loadbalancer.Server; and its constructor calls the isPassingChecks method to setAlive, which obtains the state of checks through HealthService
ConsulServerList inherits com.netflix.loadbalancer.AbstractServerList; 's getInitialListOfServers and getUpdatedListOfServers methods to call the getServers method; it gets the HealthService list of the specified serviceId through ConsulClient.getHealthServices, and then wraps it as a ConsulServer list through the transformResponse method
ConsulRibbonClientConfiguration injects ribbonServerList, which creates ConsulServerList
Doc
ConsulServer
The above is how to use spring cloud's ConsulServer. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.