使用refreshScope.refresh导致Eureka服务下线

38 篇文章 0 订阅
8 篇文章 0 订阅

这里的版本为: eureka-client-1.6.2

问题:

在使用refreshScope.refresh时,发生报错:

Caused by: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@727c01f4 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@436a63f1[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 12]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
    at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:326)
    at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:533)
    at java.util.concurrent.ScheduledThreadPoolExecutor.submit(ScheduledThreadPoolExecutor.java:632)
    at com.netflix.discovery.InstanceInfoReplicator.onDemandUpdate(InstanceInfoReplicator.java:77)
    at com.netflix.discovery.DiscoveryClient.registerHealthCheck(DiscoveryClient.java:618)
    at com.netflix.discovery.DiscoveryClient$$FastClassBySpringCGLIB$$1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.cloud.context.config.StandardBeanLifecycleDecorator$2.invoke(StandardBeanLifecycleDecorator.java:85)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
    at org.springframework.cloud.netflix.eureka.CloudEurekaClient$$EnhancerBySpringCGLIB$$1.registerHealthCheck(<generated>)
    at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry.register(EurekaServiceRegistry.java:49)
    at org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration.start(EurekaAutoServiceRegistration.java:73)
    at org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration$EurekaClientConfigurationRefresher.onApplicationEvent(EurekaDiscoveryClientConfiguration.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:253)
    at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:174)
    at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:137)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)
    at org.springframework.cloud.context.scope.refresh.RefreshScope.refresh(RefreshScope.java:137)

 

这个时候再查看Eureka界面发现,这个服务不存在了(down)

原因:

 

解决方法:      

1. 修复eureka bug   

https://github.com/spring-cloud/spring-cloud-netflix/issues/2228

 这属于一个bug:

https://github.com/Netflix/eureka/blob/v1.8.7/eureka-client/src/main/java/com/netflix/discovery/InstanceInfoReplicator.java#L89

2. 自己实现GenericScope的refresh方法

Spring Cloud动态配置实现原理与源码分析 - 腾讯云开发者社区-腾讯云

3.变通的办法,通过监听RefreshScopeRefreshedEvent后抛出一个异常阻止RefreshScopeRefreshedEvent向下传递

这个方法的前提是指定的bean信息,不需要及时的上报到Eureka上

修复组件:

package com.wjj.application.config.refreshscopefix;

import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.scope.GenericScope;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 为了修复RefreshScope Eureka 上线下线问题
 * @see <a href=https://blog.csdn.net/huang007guo/article/details/118939128?spm=1001.2014.3001.5501>使用refreshScope.refresh导致Eureka服务下线</a>
 * @author hank
 */
@Component
@Slf4j
@Order(-1)
public class RefreshScopeFix {

    private Set<String> rejectTransmitBeans = Sets.newHashSet();

    public void addRejectTransmitBean(String ...beans){
        for (String bean : beans) {
            rejectTransmitBeans.add(GenericScope.SCOPED_TARGET_PREFIX + bean);
        }
    }

    @EventListener(RefreshScopeRefreshedEvent.class)
    @Order(-1)
    public void onApplicationEvent(RefreshScopeRefreshedEvent event) throws Exception {
        if (rejectTransmitBeans.contains(event.getName())) {
            throw new RefreshScopeRefreshedRejectTransmitException();
        }
    }
}

不允许向下传递异常: 

package com.wjj.application.config.refreshscopefix;

/**
 * 不允许向下传递异常
 * <p>为了修复RefreshScope Eureka 上线下线问题
 * @see <a href=https://blog.csdn.net/huang007guo/article/details/118939128?spm=1001.2014.3001.5501>使用refreshScope.refresh导致Eureka服务下线</a>
 * @author hank
 */
public class RefreshScopeRefreshedRejectTransmitException extends Exception{
    private static final long serialVersionUID = -5184635902059811995L;
}

添加拒绝事件传递的bean(需要刷新的配置bean):


    @Autowired
    private RefreshScopeFix refreshScopeFix;

    @PostConstruct
    public void init(){
        // 添加拒绝传递的bean
        refreshScopeFix.addRejectTransmitBean("grayGateWay");
    }

调用时对拒绝传递异常特殊处理:

    
    @Autowired
    private RefreshScope refreshScope;

    /**
     * 刷新的核心代码
     *
     * @param changeEvent
     * @param beanName
     */
    private void refreshProperties(ConfigChangeEvent changeEvent, String beanName) {
        log.info("Refreshing properties! namespace: {}, changedKeys: {}, beanName: {}", changeEvent.getNamespace(), changeEvent.changedKeys(), beanName);
        try {
            refreshScope.refresh(beanName);
        } catch (Exception e) {
            if (e instanceof UndeclaredThrowableException &&
                    ((UndeclaredThrowableException) e).getUndeclaredThrowable() != null && ((UndeclaredThrowableException) e).getUndeclaredThrowable() instanceof RefreshScopeRefreshedRejectTransmitException) {
                log.info("RefreshScopeRefreshed 不允许向下传递");
            } else {
                log.error("refreshScope.refresh error", e);
            }
        }
        log.info("properties refreshed!");
    }

4.通过切面对EurekaDiscoveryClientConfiguration.onApplicationEvent做禁用处理

类似于上面的3方法,具体待补充...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值