Zuul 与 Spring boot 3 的兼容性

问题描述 投票:0回答:0

我从 Spring Boot 2.7 迁移到 3.1.1,在使用 Zuul 进行重定向之前,它找不到 RouteLocator 和 ZuulController 的 bean:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.cglib.proxy.*;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.web.ZuulController;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/** Zuul configuration. */
@Configuration
public class ZuulConfiguration {

    /**
     * Constructs a new bean post-processor for Zuul.
     *
     * @param routeLocator
     *            the route locator.
     * @param zuulController
     *            the Zuul controller.
     * @param errorController
     *            the error controller.
     * @return the new bean post-processor.
     */
    @Bean
    @Primary
    public ZuulPostProcessor zuulPostProcessor(@Autowired RouteLocator routeLocator,
                                               @Autowired ZuulController zuulController,
                                               @Autowired ErrorController errorController) {
        return new ZuulPostProcessor(routeLocator, zuulController, errorController);
    }

    private static final class ZuulPostProcessor implements BeanPostProcessor {

        private final RouteLocator routeLocator;

        private final ZuulController zuulController;

        private final boolean hasErrorController;

        ZuulPostProcessor(RouteLocator routeLocator, ZuulController zuulController, ErrorController errorController) {
            this.routeLocator = routeLocator;
            this.zuulController = zuulController;
            this.hasErrorController = (errorController != null);
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (hasErrorController && (bean instanceof ZuulHandlerMapping)) {
                Enhancer enhancer = new Enhancer();
                enhancer.setSuperclass(ZuulHandlerMapping.class);
                enhancer.setCallbackFilter(LookupHandlerCallbackFilter.INSTANCE); // only for lookupHandler
                enhancer.setCallbacks(new Callback[] { LookupHandlerMethodInterceptor.INSTANCE, NoOp.INSTANCE });
                Constructor<?> ctor = ZuulHandlerMapping.class.getConstructors()[0];
                return enhancer.create(ctor.getParameterTypes(), new Object[] { routeLocator, zuulController });
            }
            return bean;
        }

    }

    private enum LookupHandlerCallbackFilter implements CallbackFilter {

        INSTANCE;

        @Override
        public int accept(Method method) {
            if ("lookupHandler".equals(method.getName())) {
                return 0;
            }
            return 1;
        }

    }

    private enum LookupHandlerMethodInterceptor implements MethodInterceptor {

        INSTANCE;

        @Override
        public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
            if ("/error".equals(args[0])) {

                /* by entering this branch we avoid the ZuulHandlerMapping.lookupHandler method to trigger the NoSuchMethodError */
                return null;
            }
            return methodProxy.invokeSuper(target, args);
        }

    }
}

Zuul not autowired dependencies

我无法迁移到 api-gateway,因为我们使用 spring-web-dependency(我读到需要 webflux)

如果您知道如何在弹性中将/报告页面重定向到 kibana (任务的主要思想)的任何其他方法,请告诉我,但最好保留旧的 Zuul 配置并添加一些依赖项

我发现它不起作用,因为在 Spring Boot 3.1.1 - jakarta 中使用 javax servlet

请帮助我:)

java spring-boot spring-cloud netflix-zuul
© www.soinside.com 2019 - 2024. All rights reserved.