美文网首页
SpringCloud使用Consul时,服务注销的操作方式

SpringCloud使用Consul时,服务注销的操作方式

作者: eaglewa | 来源:发表于2019-02-18 17:01 被阅读3次

    1、为什么要服务注销

    当服务升级上线时,为了平滑过渡,一般会先把老的服务从注册中心上摘除(服务本身不停止),等该服务完全没有流量时,再进行下线的操作。采用SpringCloudConsul时,内部默认每3s和注册中心同步一次心跳,并以此刷新各个服务的ip列表。当某个服务从注册中心注销时,不需要进行广播,由心跳机制保证该注册中心剩余的所有服务能够感知到。

    2、SpringCloudConsul提供的方法

    SpringCloudConsul提供了一个ConsulAutoServiceRegistration类,该类主要提供服务的注册和注销功能,源码如下:

    /*
     * Copyright 2013-2016 the original author or authors.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "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.
     */
    
    package org.springframework.cloud.consul.serviceregistry;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.boot.web.context.WebServerInitializedEvent;
    import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
    import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
    import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
    import org.springframework.retry.annotation.Retryable;
    import org.springframework.util.Assert;
    import org.springframework.util.StringUtils;
    
    /**
     * @author Spencer Gibb
     */
    public class ConsulAutoServiceRegistration extends AbstractAutoServiceRegistration<ConsulRegistration> {
    
        private static Log log = LogFactory.getLog(ConsulAutoServiceRegistration.class);
    
        private ConsulDiscoveryProperties properties;
        private ConsulAutoRegistration registration;
    
        public ConsulAutoServiceRegistration(ConsulServiceRegistry serviceRegistry,
                AutoServiceRegistrationProperties autoServiceRegistrationProperties,
                ConsulDiscoveryProperties properties, ConsulAutoRegistration registration) {
            super(serviceRegistry, autoServiceRegistrationProperties);
            this.properties = properties;
            this.registration = registration;
        }
    
        void setPortIfNeeded(int port) {
            getPort().compareAndSet(0, port);
        }
    
        @Override
        protected ConsulAutoRegistration getRegistration() {
            if (this.registration.getService().getPort() == null && this.getPort().get() > 0) {
                this.registration.initializePort(this.getPort().get());
            }
            Assert.notNull(this.registration.getService().getPort(), "service.port has not been set");
            return this.registration;
        }
    
        @Override
        protected ConsulAutoRegistration getManagementRegistration() {
            return this.registration.managementRegistration();
        }
    
        @Override
        @Retryable(interceptor = "consulRetryInterceptor")
        public void start() {
            super.start();
        }
    
        @Override
        protected void register() {
            if (!this.properties.isRegister()) {
                log.debug("Registration disabled.");
                return;
            }
    
            super.register();
        }
    
        @Override
        protected void registerManagement() {
            if (!this.properties.isRegister()) {
                return;
            }
            super.registerManagement();
    
        }
    
        @Override
        protected Object getConfiguration() {
            return properties;
        }
    
        @Override
        protected void deregister() {
            if (!this.properties.isRegister() || !this.properties.isDeregister()) {
                return;
            }
            super.deregister();
        }
    
        @Override
        protected void deregisterManagement() {
            if (!this.properties.isRegister() || !this.properties.isDeregister()) {
                return;
            }
            super.deregisterManagement();
        }
    
        @Override
        protected boolean isEnabled() {
            return this.properties.getLifecycle().isEnabled();
        }
    
        @Override
        @SuppressWarnings("deprecation")
        protected String getAppName() {
            String appName = properties.getServiceName();
            return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
        }
    
        @Override
        public void bind(WebServerInitializedEvent event) {
            // do nothing so we can listen for this event in a different class
            // this ensures start() can be retried if spring-retry is available
        }
    }
    

    其中服务注销的方法逻辑是:

    image-20190218160430479

    该方法会最终是调用consul提供的restfulAPI去进行服务的注销:

    image-20190218160611472

    3、测试SpringCloudConsul的注销方法

    我们写一段代码进行服务注销的测试:

    @RestController
    public class ConsulController {
    
        private final ConsulAutoServiceRegistration serviceRegistration;
    
        public ConsulController(ConsulAutoServiceRegistration serviceRegistration) {
            this.serviceRegistration = serviceRegistration;
        }
    
        @DeleteMapping("deregister")
        public void deregister() {
            serviceRegistration.stop();
        }
        
    }
    

    服务启动后consul界面显示服务状态为绿色,代表正常:

    image-20190218160840196

    执行该接口后,正常情况下consul会摘除这个服务的注册信息,但是多试验几次后,会发现服务信息还在,这个时候如果服务停止的话,必然会出现调用异常的情况,consul上的服务状态为橙色:

    image-20190218161830400 image-20190218162026267

    4、完整的注销操作

    显然以上的做法并不能真正的完成服务注销的操作,consul的注销操作需要到集群中的节点上才能完成,具体代码如下:

    @RestController
    @Slf4j
    public class ConsulController {
    
        private final ConsulClient consulClient;
        private final ConsulRegistration consulRegistration;
    
        public ConsulController(ConsulClient consulClient, ConsulRegistration consulRegistration) {
            this.consulClient = consulClient;
            this.consulRegistration = consulRegistration;
        }
    
        @DeleteMapping("api/deregister")
        public void deregister() {
            String currentInstanceId = consulRegistration.getInstanceId();
            List<Member> members = consulClient.getAgentMembers().getValue();
            for (Member member : members) {
                String address = member.getAddress();
                ConsulClient clearClient = new ConsulClient(address);
                try {
                    Map<String, Service> serviceMap = clearClient.getAgentServices().getValue();
                    for (Entry<String, Service> entry : serviceMap.entrySet()) {
                        Service service = entry.getValue();
                        String instanceId = service.getId();
                        if (currentInstanceId.equals(instanceId)) {
                            log.warn("在{}客户端上的服务 :{}为无效服务,准备清理...................", address, currentInstanceId);
                            clearClient.agentServiceDeregister(currentInstanceId);
                        }
                    }
                } catch (Exception e) {
                    log.error("异常信息: {}", e);
                }
            }
        }
    
    }
    

    5、如何让服务在consul上进入维护模式(临时摘除)

    • 暴露/service-registry 端点(需要引入spring-boot-starter-actuator支持)

    • SpringCloudConsul提供【上线】和【离线】两种服务状态的设置,具体代码在ConsulServiceRegistry类中

      image-20190218164331970
    • 发送POST请求到/actuator/service-registry 端点,此时状态应为OUT_OF_SERVICE

      curl -X "POST" "http://localhost:8081/actuator/service-registry?status=OUT_OF_SERVICE" -H "Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8"
      
    • 此时consul中显示服务状态进入维护模式:

      image-20190218164643941
    • 恢复操作时,只需要将状态变更为UP即可,执行完毕后consul上的服务状态恢复:

      curl -X "POST" "http://localhost:8081/actuator/service-registry?status=UP" -H "Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8"
      
      image-20190218164921868

    6、参考文档:

    1、http://www.itmuch.com/spring-cloud-sum/how-to-unregister-service-in-eureka/

    2、http://blog.51cto.com/qiangmzsx/2086174

    相关文章

      网友评论

          本文标题:SpringCloud使用Consul时,服务注销的操作方式

          本文链接:https://www.haomeiwen.com/subject/tvqzeqtx.html