Spring Cloud Eureka

作者: 52HzBoo | 来源:发表于2018-06-03 17:21 被阅读16次

    Eureka Register 服务注册

    客户端注解 :

    @EnableEurekaClient

    • 如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient
    • @EnableEurekaClient注解是基于spring-cloud-netflix依赖,只能为eureka作用;

    @EnableDiscoveryClient

    • 注解是基于spring-cloud-commons依赖,并且在classpath中实现;
    • 如果是其他的注册中心,比如:consul, zookeeper , 那么推荐使用EnableDiscoveryClient
    服务端注解

    @EnableEurekaServer

    • 服务端注解,表示这是个Eureka注册中心

    当Eureka客户端向Eureka Server注册时,它提供自身的元数据,比如IP地址、端口,运行状况指示符URL,主页等。

    # Eureka配置 发现注册中心地址
    eureka:
      client:
        serviceUrl:
    # 集成了Spring Security ,需访问密码
          defaultZone: http://1595****86:bruceliu@127.0.0.1:8000/eureka/
    

    Eureka Renew 服务续约

    • Eureka Client 30s 向 Eureka Server 注册中心发送一次心跳用于续约,若注册中心超过90s未收到续约,默认服务将被剔除。

    Eureka Cancel 服务下线

    Eureka客户端在程序关闭时向Eureka服务器发送取消请求。 发送请求后,该客户端实例信息将从服务器的实例注册表中删除。该下线请求不会自动完成,它需要调用以下内容:
    DiscoveryManager.getInstance().shutdownComponent()

    Eureka DiscoveryClient register 源码

        /**
         * Register with the eureka service by making the appropriate REST call.
         */
        boolean register() throws Throwable {//服务注册
            logger.info(PREFIX + appPathIdentifier + ": registering service...");
            EurekaHttpResponse<Void> httpResponse;
            try {
                httpResponse = eurekaTransport.registrationClient.register(instanceInfo);
            } catch (Exception e) {
                logger.warn("{} - registration failed {}", PREFIX + appPathIdentifier, e.getMessage(), e);
                throw e;
            }
            if (logger.isInfoEnabled()) {
                logger.info("{} - registration status: {}", PREFIX + appPathIdentifier, httpResponse.getStatusCode());
            }
            return httpResponse.getStatusCode() == 204;
        }
    
        /**
         * Renew with the eureka service by making the appropriate REST call
         */
        boolean renew() {//服务续约
            EurekaHttpResponse<InstanceInfo> httpResponse;
            try {
                httpResponse = eurekaTransport.registrationClient.sendHeartBeat(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo, null);
                logger.debug("{} - Heartbeat status: {}", PREFIX + appPathIdentifier, httpResponse.getStatusCode());
                if (httpResponse.getStatusCode() == 404) {
                    REREGISTER_COUNTER.increment();
                    logger.info("{} - Re-registering apps/{}", PREFIX + appPathIdentifier, instanceInfo.getAppName());
                    return register();
                }
                return httpResponse.getStatusCode() == 200;
            } catch (Throwable e) {
                logger.error("{} - was unable to send heartbeat!", PREFIX + appPathIdentifier, e);
                return false;
            }
        }
    
    Eureka DiscoveryManager shutdownComponent Cancel
       /**
         * Shuts down the <tt>Discovery Client</tt> which unregisters the
         * information about this instance from the <tt>Discovery Server</tt>.
         */
        public void shutdownComponent() {// 服务下线
            if (discoveryClient != null) {
                try {
                    discoveryClient.shutdown();
                    discoveryClient = null;
                } catch (Throwable th) {
                    logger.error("Error in shutting down client", th);
                }
            }
        }
    

    相关文章

      网友评论

        本文标题:Spring Cloud Eureka

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