当前位置:   article > 正文

成功解决java.lang.IllegalStateException:Another strategy was already registered._java.lang.illegalstateexception: another endpoint

java.lang.illegalstateexception: another endpoint is already registered with

成功解决java.lang.IllegalStateException:Another strategy was already registered.

**

项目场景:

项目场景:微服务项目使用了熔断器Hystrix配置,在添加spring-boot-starter-actuator依赖之后报错。

问题描述:

异常关键内容描述:
Caused by: java.lang.IllegalStateException: Another strategy was already registered.

控制台打印异常如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hystrixConfig': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Another strategy was already registered.
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:415) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
	......
Caused by: java.lang.IllegalStateException: Another strategy was already registered.
	at com.netflix.hystrix.strategy.HystrixPlugins.registerConcurrencyStrategy(HystrixPlugins.java:190) ~[hystrix-core-1.5.18.jar:1.5.18]
	at com.myproject.common.hystrix.hystrixConfig.init(hystrixConfig.java:46) ~[upp-common-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_171]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_171]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_171]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

原因分析:

Caused by: java.lang.IllegalStateException: Another strategy was already registered.

提示:已经注册策略,不能重复注册。

分析项目:
原来项目在使用feign和Hystrix结合进行服务之间调用时,为保证将上游服务的的请求header传递到下游服务,自定义了hystrix执行的隔离策略。
项目中自定义了策略,继承了HystrixConcurrencyStrategy,重写wrapCallable方法。

关键代码:

/**
 *
 * @Description:    Hystrix实现ThreadLocal上下文的传递
 * @Author: Yanzh
 * @Date: Created in 2021/06/03 10:28
 * @Copyright: Copyright (c) 2021
 * @Version: V1.0
 * @Modified: Yanzh
 */
public class RequestContextHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
    private final Collection<HystrixCallableWrapper> wrappers;

    public RequestContextHystrixConcurrencyStrategy(Collection<HystrixCallableWrapper> wrappers) {
        this.wrappers = wrappers;
    }
    @Override
    public <T> Callable<T> wrapCallable(Callable<T> callable) {
        return new CallableWrapperChain<T>(callable, wrappers.iterator()).wrapCallable();
    }

    private static class CallableWrapperChain<T> {
        private final Callable<T> callable;
        private final Iterator<HystrixCallableWrapper> wrappers;

        CallableWrapperChain(Callable<T> callable, Iterator<HystrixCallableWrapper> wrappers) {
            this.callable = callable;
            this.wrappers = wrappers;
        }

        Callable<T> wrapCallable() {
            Callable<T> delegate = callable;
            while (wrappers.hasNext()) {
                delegate = wrappers.next().wrap(delegate);
            }
            return delegate;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
/**
 *
 * @Description:    Hystrix实现配置
 * @Author: Yanzh
 * @Date: Created in 2021/06/03 10:28
 * @Copyright: Copyright (c) 2021
 * @Version: V1.0
 * @Modified: Yanzh
 */
@Configuration
public class hystrixConfig {
@PostConstruct
    public void init() {
         HystrixPlugins.getInstance().registerConcurrencyStrategy(new RequestContextHystrixConcurrencyStrategy(wrappers));
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

解决方案:

重置策略;先删除已经注册的策略,重新注册新的策略,如下:
/**
 *
 * @Description:    与 Hystrix 相关的公共配置
 * @Author: Yanzh
 * @Date: Created in 2021/06/03 10:28
 * @Copyright: Copyright (c) 2021
 * @Version: V1.0
 * @Modified: Yanzh
 */
@Configuration
public class HystrixConfiguration {

    @Autowired(required = false)
    private List<HystrixCallableWrapper> wrappers;

    @Bean
    public HystrixCallableWrapper requestAttributeAwareCallableWrapper() {
        return new RequestAttributeAwareCallableWrapper();
    }

    @PostConstruct
    public void init() {
        if (!Collections.isEmpty(wrappers)) {
            try {
                HystrixConcurrencyStrategy strategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
                if (strategy instanceof RequestContextHystrixConcurrencyStrategy) {
                    return;
                }
                HystrixConcurrencyStrategy hystrixConcurrencyStrategy = new RequestContextHystrixConcurrencyStrategy(wrappers);
                // 获取原来的相关数据配置
                HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook();
                HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
                HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
                HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy();
                logger.debug("Registering Muses Hystrix Concurrency Strategy.");

                // 重置再重新填充
                // 直接设置会触发异常 Caused by: java.lang.IllegalStateException: Another strategy was already registered.
                HystrixPlugins.reset();
                HystrixPlugins.getInstance().registerConcurrencyStrategy(hystrixConcurrencyStrategy);
                HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
                HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
                HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
                HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);

            } catch (Exception e) {
                logger.error("Failed to register Muses Hystrix Concurrency Strategy", e);
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

参考文章

https://blog.csdn.net/weihao_/article/details/83240099
https://shanhy.blog.csdn.net/article/details/108668952

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/251157?site
推荐阅读
相关标签
  

闽ICP备14008679号