Overwrite the default health check rule that modifies spring cloud

Source: Internet
Author: User

The MicroServices gateway is the socket long connection with the payment company docking, the gateway needs to provide the HTTP interface to the internal system call, when the socket is not established connection (Gateway service is high availability is haproxy, some services socket may not be connected to the payment company), so, At this point the HTTP service of the gateway does not let the internal other call system discover.

Spring Cloud project built by Gradle

Added in Build.gradle:

    Compile ' org.springframework.retry:spring-retry:1.1.2. RELEASE '    compile ' org.springframework.boot:spring-boot-actuator:1.4.5.RELEASE '

Engineering Structure:

Copy the two Java files for health check in Spring-cloud-consul-core to your project as follows.

/** Copyright 2013-2015 the original author or authors. * Licensed under the Apache License, Version 2.0 (the "Licens E "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageOrg.springframework.cloud.consul;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.springframework.boot.actuate.autoconfigure.ConditionalOnEnabledHealthIndicator;ImportOrg.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;ImportOrg.springframework.boot.actuate.endpoint.Endpoint;Importorg.springframework.boot.autoconfigure.aop.AopAutoConfiguration;ImportOrg.springframework.boot.autoconfigure.condition.ConditionalOnClass;ImportOrg.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;Importorg.springframework.boot.context.properties.EnableConfigurationProperties;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.annotation.Import;ImportOrg.springframework.retry.annotation.EnableRetry;Importorg.springframework.retry.annotation.Retryable;ImportOrg.springframework.retry.interceptor.RetryInterceptorBuilder;ImportOrg.springframework.retry.interceptor.RetryOperationsInterceptor;Importcom.ecwid.consul.v1.ConsulClient;/** * @authorSpencer Gibb*/@Configuration @enableconfigurationproperties@conditionalonconsulenabled Public classconsulautoconfiguration {@Bean @ConditionalOnMissingBean Publicconsulproperties consulproperties () {return Newconsulproperties (); } @Bean @ConditionalOnMissingBean Publicconsulclient consulclient (consulproperties consulproperties) {return Newconsulclient (Consulproperties.gethost (), Consulproperties.getport ()); } @Configuration @ConditionalOnClass (Endpoint.class)    protected Static classconsulhealthconfig {@Bean @ConditionalOnMissingBean @ConditionalOnEnabledEndpoint ("Consul")         Publicconsulendpoint consulendpoint (consulclient consulclient) {return NewConsulendpoint (consulclient); } @Bean @ConditionalOnMissingBean @ConditionalOnEnabledHealthIndicator ("Consul")         Publicposconsulhealthindicator consulhealthindicator (consulclient consulclient) {return NewPosconsulhealthindicator (consulclient); }} @ConditionalOnClass ({retryable.class, Aspect.class, Aopautoconfiguration.class}) @Configuration @EnableRetry (Proxytargetclass=true) @Import (aopautoconfiguration.class) @EnableConfigurationProperties (retryproperties.class)    protected Static classretryconfiguration {@Bean (name= "Consulretryinterceptor") @ConditionalOnMissingBean (name= "Consulretryinterceptor")         Publicretryoperationsinterceptor Consulretryinterceptor (retryproperties properties) {returnRetryinterceptorbuilder. Stateless (). Backoffoptions (Properties.getinitialinter Val (), Properties.getmultiplier (), Properties.getmaxinterval ()). maxattempts        (Properties.getmaxattempts ()). build (); }    }}
/** Copyright 2013-2015 the original author or authors. * Licensed under the Apache License, Version 2.0 (the "Licens E "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageOrg.springframework.cloud.consul;Importjava.util.List;ImportJava.util.Map;ImportOrg.springframework.boot.actuate.health.AbstractHealthIndicator;ImportOrg.springframework.boot.actuate.health.Health;Importcom.ecwid.consul.v1.ConsulClient;ImportCom.ecwid.consul.v1.QueryParams;ImportCom.ecwid.consul.v1.Response;Importcom.ecwid.consul.v1.agent.model.Self;ImportCom.ecwid.consul.v1.agent.model.Self.Config;ImportCom.dxz.IoSessionHelper;ImportCom.dxz.utils.SpringUtil;/** * @authorSpencer Gibb*/ Public classPosconsulhealthindicatorextendsAbstracthealthindicator {PrivateConsulclient Consul; PrivateIosessionhelper Iosessionhelper;  PublicPosconsulhealthindicator (consulclient Consul) { This. Consul =Consul; }        Privateiosessionhelper Getiosessionhelper () {if(NULL==iosessionhelper) {Iosessionhelper= Springutil.getbean ("Iosessionhelper", Iosessionhelper.class); }        returnIosessionhelper; }        Private BooleanIosessioncheck () {returngetiosessionhelper (). Iosessioncheck (); } @Overrideprotected voidDohealthcheck (Health.builder Builder)throwsException {Try{Response<Self> self =consul.getagentself (); Config config=Self.getvalue (). GetConfig (); Response<map<string, list<string>>> services =Consul. Getcatalogservices (Queryparams.default); Builder. Withdetail ("Services", Services.getvalue ()). Withdetail ("Advertiseaddress", Config.getadvertiseaddress ()). Withdetail ("Datacenter", Config.getdatacenter ()). Withdetail ("Domain", Config.getdomain ()). Withdetail ("NodeName", Config.getnodename ()). Withdetail ("Bindaddress", Config.getbindaddress ()). Withdetail ("Clientaddress", Config.getclientaddress ()); if(Iosessioncheck ()) {builder.up (); } Else{Builder.outofservice ().  Withdetail ( "description", "Iosession not Available"); }                    }        Catch(Exception e) {Builder.down (e); }    }}

Overwrite the Dohealthcheck () method, increase the socket connection check to return the health check results to Consul, other microservices clients perceive which Gateway service is available.

Overwrite the default health check rule that modifies spring cloud

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.