spring cloud eureka部分源码分析及微服务管理功能

简介:

eureka原生的管理页面只有查看服务节点和一些信息,没有动态启用停用服务节点的功能

一. EurekaClient获取所有注册的服务

eureka客户端会加载一个定时任务去获取注册中心的服务,任务的配置在:com.netflix.discovery.DiscoveryClient,刷新的线程是:CacheRefreshThread。
获取的注册中心服务的时候,会把所有服务都拉取下来,但是默认会过滤掉状态不是UP的服务。
获取服务的具体代码在:DiscoveryClient.getAndStoreFullRegistry()方法

/**
 * Gets the full registry information from the eureka server and stores it locally.
 * When applying the full registry, the following flow is observed:
 *
 * if (update generation have not advanced (due to another thread))
 *   atomically set the registry to the new registry
 * fi
 *
 * @return the full registry information.
 * @throws Throwable
 *             on error.
 */
private void getAndStoreFullRegistry() throws Throwable {
    long currentUpdateGeneration = fetchRegistryGeneration.get();

    logger.info("Getting all instance registry info from the eureka server");

    Applications apps = null;
    EurekaHttpResponse<Applications> httpResponse = clientConfig.getRegistryRefreshSingleVipAddress() == null
            ? eurekaTransport.queryClient.getApplications(remoteRegionsRef.get())
            : eurekaTransport.queryClient.getVip(clientConfig.getRegistryRefreshSingleVipAddress(), remoteRegionsRef.get());
    if (httpResponse.getStatusCode() == Status.OK.getStatusCode()) {
        apps = httpResponse.getEntity();
    }
    logger.info("The response status is {}", httpResponse.getStatusCode());

    if (apps == null) {
        logger.error("The application is null for some reason. Not storing this information");
    } else if (fetchRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {
        localRegionApps.set(this.filterAndShuffle(apps));
        logger.debug("Got full registry with apps hashcode {}", apps.getAppsHashCode());
    } else {
        logger.warn("Not updating applications as another thread is updating it already");
    }
}

过滤服务状态的代码在:DiscoveryClient.filterAndShuffle()方法

/**
 * Gets the <em>applications</em> after filtering the applications for
 * instances with only UP states and shuffling them.
 *
 * <p>
 * The filtering depends on the option specified by the configuration
 * {@link EurekaClientConfig#shouldFilterOnlyUpInstances()}. Shuffling helps
 * in randomizing the applications list there by avoiding the same instances
 * receiving traffic during start ups.
 * </p>
 *
 * @param apps
 *            The applications that needs to be filtered and shuffled.
 * @return The applications after the filter and the shuffle.
 */
private Applications filterAndShuffle(Applications apps) {
    if (apps != null) {
        if (isFetchingRemoteRegionRegistries()) {
            Map<String, Applications> remoteRegionVsApps = new ConcurrentHashMap<String, Applications>();
            apps.shuffleAndIndexInstances(remoteRegionVsApps, clientConfig, instanceRegionChecker);
            for (Applications applications : remoteRegionVsApps.values()) {
                applications.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances());
            }
            this.remoteRegionVsApps = remoteRegionVsApps;
        } else {
            apps.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances());
        }
    }
    return apps;
}

由此可以看出,是否需要过滤服务状态的配置是在clientConfig中,下一步寻找这个配置具体要怎么写。在类中可以看出,clientConfig对应的是EurekaClientConfig,但这是个接口,有两个实现

  • EurekaClientConfig

    • DefaultEurekaClientConfig
    • EurekaClientConfigBean

仅看名字以为是使用的DefaultEurekaClientConfig,找到对应的配置方法是:

/*
 * (non-Javadoc)
 *
 * @see
 * com.netflix.discovery.EurekaClientConfig#shouldFilterOnlyUpInstances()
 */
@Override
public boolean shouldFilterOnlyUpInstances() {
    return configInstance.getBooleanProperty(
            namespace + SHOULD_FILTER_ONLY_UP_INSTANCES_KEY, true).get();
}

于是找到namespace以及对应的常量,组合起来的配置应该是:eureka.shouldFilterOnlyUpInstances,然而,加上配置后并没有什么用。仔细看看这是eureka包里的,而不是spring包里的,所以这应该是独立使用eureka的时候配置的方法,回过头来看,另外一个配置的实现(EurekaClientConfigBean)是spring cloud包里的,找找在哪里有用到?果然不出所料,在EurekaClientAutoConfiguration自动配置中有初始化此bean,并且EurekaClientConfigBean上有@ConfigurationProperties注解,其实也就是个properties。所以结论出来了,EurekaClientConfigBean实际是将properties配置和对EurekaClientConfig接口的实现放一起了。。。,这样就能找到配置不自动过滤状态为UP的服务的方法了

eureka.client.filterOnlyUpInstances=false

二. 动态更新EurekaClient的状态

首先,spring cloud官方文档中,介绍了一个endpoint(/service-registry/instance-status),提供了get和post方法,get方法用来获取节点状态,post用来修改节点状态

Service Registry Actuator Endpoint

A /service-registry actuator endpoint is provided by Commons. This endpoint relys on a Registration bean in the Spring Application Context. Calling /service-registry/instance-status via a GET will return the status of the Registration. A POST to the same endpoint with a String body will change the status of the current Registration to the new value. Please see the documentation of the ServiceRegistry implementation you are using for the allowed values for updating the status and the values retured for the status.

spring cloud eureka client默认是没有开启endpoint的,需要自己引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

不过,引入依赖后会不止这一个endpoint,并且需要安全认证,当然也可以配置不需要安全认证:

management.security.enabled=false

为了不依赖actutator包,并且了解spring cloud具体是如何更新节点状态的,找到了endpoint中修改状态的具体实现:ServiceRegistryEndpoint。可以看出,主要是通过ServiceRegistry和Registration实现的,而这两个接口并不是actuator包里的,所以尝试自己实(拷)现(贝)一下。自己写一个controller,注入以上两个对象,然后将ServiceRegistryEndpoint中的获取和修改状态的方法复制粘贴,源码如下:

import javax.annotation.Resource;

import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.itopener.framework.ResultMap;

@RestController
@RequestMapping("eureka-client")
public class ServiceRegistryController {

    @Resource
    private ServiceRegistry<EurekaRegistration> serviceRegistry;
    
    @Resource
    private EurekaRegistration registration;
    
    @RequestMapping(value = "status", method = RequestMethod.GET)
    public ResultMap getStatus(){
        return ResultMap.buildSuccess().put("status", serviceRegistry.getStatus(registration));
    }
    
    @RequestMapping(value = "status", method = RequestMethod.POST)
    public ResultMap setStatus(String status){
        serviceRegistry.setStatus(registration, status);
        return ResultMap.buildSuccess();
    }
}

需要注意的是

  • ServiceRegistry有Registration接口的实现类的泛型,如果不对应会注入失败,这样看具体是哪个实现类?ServiceRegistry只有一个实现类:EurekaServiceRegistry,所以结果就显而易见了
    当然,这两个bean的初始化也会在自动配置类中(EurekaClientAutoConfiguration)
@Bean
public EurekaServiceRegistry eurekaServiceRegistry() {
    return new EurekaServiceRegistry();
}

@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
public EurekaRegistration eurekaRegistration(EurekaClient eurekaClient, CloudEurekaInstanceConfig instanceConfig,
    ApplicationInfoManager applicationInfoManager) {
    return EurekaRegistration.builder(instanceConfig)
        .with(applicationInfoManager)
        .with(eurekaClient)
        .with(healthCheckHandler)
        .build();
}

三. 动态管理spring cloud eureka服务

基于以上两点,就可以动态停用或启用eureka中注册的服务节点了。创建一个独立的web应用,与普通的服务一样注册到eureka中心,当然为了还是需要与其他服务有些不一样的配置

#本节点不注册到eureka
eureka.client.register-with-eureka=false
#可以从eureka拉取注册的服务
eureka.client.fetch-registry=true
#不过滤服务节点的UP状态,即需要使用所有的服务节点
eureka.client.filterOnlyUpInstances=false

然后就可以使用EurekaClient获取注册中心的服务了

@Resource
private EurekaClient eurekaClient;

/**
 * @description 获取服务数量和节点数量
 * @author fuwei.deng
 * @date 2017年7月21日 下午3:36:24
 * @version 1.0.0
 * @return
 */
@RequestMapping(value = "home", method = RequestMethod.GET)
public ResultMap home(){
    List<Application> apps = eurekaClient.getApplications().getRegisteredApplications();
    int appCount = apps.size();
    int nodeCount = 0;
    for(Application app : apps){
        nodeCount += app.getInstancesAsIsFromEureka().size();
    }
    return ResultMap.buildSuccess().put("appCount", appCount).put("nodeCount", nodeCount);
}

/**
 * @description 获取所有服务节点
 * @author fuwei.deng
 * @date 2017年7月21日 下午3:36:38
 * @version 1.0.0
 * @return
 */
@RequestMapping(value = "apps", method = RequestMethod.GET)
public ResultMap apps(){
    List<Application> apps = eurekaClient.getApplications().getRegisteredApplications();
    Collections.sort(apps, new Comparator<Application>() {
        public int compare(Application l, Application r) {
            return l.getName().compareTo(r.getName());
        }
    });
    return ResultMap.buildSuccess().put("list", apps);
}

如果需要动态修改节点的状态,以达到停用和启用服务节点的目的,可以使用http调用对应节点的接口

@RequestMapping(value = "status/{appName}", method = RequestMethod.POST)
public ResultMap status(@PathVariable String appName, String instanceId, String status){
    Application application = eurekaClient.getApplication(appName);
    InstanceInfo instanceInfo = application.getByInstanceId(instanceId);
    HttpUtil.post(instanceInfo.getHomePageUrl() + "eureka-client/status", "status=" + status);
    return ResultMap.buildSuccess();
}

当然如果是使用服务节点的actuator endpoint接口,调用接口的地址不一样(还有安全认证,此处代码未涉及),需要注意的是,endpoint接收的参数是@RequestBody(并且使用的jackson转换,fastjson转换是会出现异常的)

@RequestMapping(value = "status/{appName}", method = RequestMethod.POST)
public ResultMap status(@PathVariable String appName, String instanceId, String status){
    Application application = eurekaClient.getApplication(appName);
    InstanceInfo instanceInfo = application.getByInstanceId(instanceId);
    HttpUtil.post(instanceInfo.getHomePageUrl() + "service-registry/instance-status", status);
    return ResultMap.buildSuccess();
}

由于eureka注册中心没有通知的功能,只能由节点自己发起刷新请求,所以修改状态后,需要等到相关节点下一次刷新后才会生效。节点刷新是通过定时任务实现的,源码在com.netflix.discovery.DiscoveryClient中,并且任务是在构造方法中初始化的,还不能自己手动触发,主要代码如下:

//任务调度器,私有属性
private final ScheduledExecutorService scheduler;
//刷新注册中心节点的线程池,私有属性
private final ThreadPoolExecutor cacheRefreshExecutor

//1224行,私有方法中,如果允许拉取注册中心的节点,则初始化调度任务,从源码中可以看出能配置任务执行的间隔时间
if (clientConfig.shouldFetchRegistry()) {
    // registry cache refresh timer
    int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();
    int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();
    scheduler.schedule(
        new TimedSupervisorTask(
                "cacheRefresh",
                scheduler,
                cacheRefreshExecutor,
                registryFetchIntervalSeconds,
                TimeUnit.SECONDS,
                expBackOffBound,
                new CacheRefreshThread()
        ),
        registryFetchIntervalSeconds, TimeUnit.SECONDS);
}

配置在EurekaClientAutoConfiguration-->RefreshableEurekaClientConfiguration,使用DiscoveryClient的子类CloudEurekaClient实例化

@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
@org.springframework.cloud.context.config.annotation.RefreshScope
@Lazy
public EurekaClient eurekaClient(ApplicationInfoManager manager,
        EurekaClientConfig config, EurekaInstanceConfig instance) {
    manager.getInfo(); // force initialization
    return new CloudEurekaClient(manager, config, this.optionalArgs,
            this.context);
}

CloudEurekaClient中有一个刷新的方法,发布一个心跳事件,但这个方法是protected,没法通过实例调用,并且依赖于心跳事件。应用节点默认刷新事件是60秒一次,时间也不算太长,所以动态停用节点后再60秒内生效,应该是在能接受的范围吧,并且这个时间还能配置

目录
相关文章
|
4月前
|
算法 Java 微服务
【SpringCloud(1)】初识微服务架构:创建一个简单的微服务;java与Spring与微服务;初入RestTemplate
微服务架构是What?? 微服务架构是一种架构模式,它提出将单一应用程序划分为一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。 每个服务允许在其独立的进程中,服务于服务间采用轻量级的通信机制互相协作(通常是Http协议的RESTful API或RPC协议)。 每个服务都围绕着具体业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据上下文,选择合适的语言、工具对其进行构建
563 126
|
4月前
|
负载均衡 算法 Java
【SpringCloud(2)】微服务注册中心:Eureka、Zookeeper;CAP分析;服务注册与服务发现;单机/集群部署Eureka;连接注册中心
1. 什么是服务治理? SpringCloud封装了Netfix开发的Eureka模块来实现服务治理 在传统pc的远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册
358 0
|
5月前
|
数据可视化 Java BI
将 Spring 微服务与 BI 工具集成:最佳实践
本文探讨了 Spring 微服务与商业智能(BI)工具集成的潜力与实践。随着微服务架构和数据分析需求的增长,Spring Boot 和 Spring Cloud 提供了构建可扩展、弹性服务的框架,而 BI 工具则增强了数据可视化与实时分析能力。文章介绍了 Spring 微服务的核心概念、BI 工具在企业中的作用,并深入分析了两者集成带来的优势,如实时数据处理、个性化报告、数据聚合与安全保障。同时,文中还总结了集成过程中的最佳实践,包括事件驱动架构、集中配置管理、数据安全控制、模块化设计与持续优化策略,旨在帮助企业构建高效、智能的数据驱动系统。
318 1
将 Spring 微服务与 BI 工具集成:最佳实践
|
5月前
|
存储 安全 Java
管理 Spring 微服务中的分布式会话
在微服务架构中,管理分布式会话是确保用户体验一致性和系统可扩展性的关键挑战。本文探讨了在 Spring 框架下实现分布式会话管理的多种方法,包括集中式会话存储和客户端会话存储(如 Cookie),并分析了它们的优缺点。同时,文章还涵盖了与分布式会话相关的安全考虑,如数据加密、令牌验证、安全 Cookie 政策以及服务间身份验证。此外,文中强调了分布式会话在提升系统可扩展性、增强可用性、实现数据一致性及优化资源利用方面的显著优势。通过合理选择会话管理策略,结合 Spring 提供的强大工具,开发人员可以在保证系统鲁棒性的同时,提供无缝的用户体验。
118 0
|
5月前
|
消息中间件 Java 数据库
Spring 微服务中的数据一致性:最终一致性与强一致性
本文探讨了在Spring微服务中实现数据一致性的策略,重点分析了最终一致性和强一致性的定义、优缺点及适用场景。结合Spring Boot与Spring Cloud框架,介绍了如何根据业务需求选择合适的一致性模型,并提供了实现建议,帮助开发者在分布式系统中确保数据的可靠性与同步性。
381 0
|
4月前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
Spring Boot 3.x 微服务架构实战指南
|
4月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
5月前
|
监控 安全 Java
Spring Cloud 微服务治理技术详解与实践指南
本文档全面介绍 Spring Cloud 微服务治理框架的核心组件、架构设计和实践应用。作为 Spring 生态系统中构建分布式系统的标准工具箱,Spring Cloud 提供了一套完整的微服务解决方案,涵盖服务发现、配置管理、负载均衡、熔断器等关键功能。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
361 1
|
11月前
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
12月前
|
人工智能 SpringCloudAlibaba 自然语言处理
SpringCloud Alibaba AI整合DeepSeek落地AI项目实战
在现代软件开发领域,微服务架构因其灵活性、可扩展性和模块化特性而受到广泛欢迎。微服务架构通过将大型应用程序拆分为多个小型、独立的服务,每个服务运行在其独立的进程中,服务与服务间通过轻量级通信机制(通常是HTTP API)进行通信。这种架构模式有助于提升系统的可维护性、可扩展性和开发效率。
4401 2