4

默认情况下,Togglz 管理控制台在应用程序端口上运行(由server.port属性配置)。我想把它暴露在management.port. 我的问题:有可能吗?

4

1 回答 1

4

如果您使用 Togglz >= 2.4.0,则此功能开箱即用。

对于旧版本的解决方案如下:

我设法management.port通过用MvcEndpoint. 使用 Spring Cloud 模块为您完成所有工作的最简单方法(例如在 HystrixStreamEndpoint 中):

public class HystrixStreamEndpoint extends ServletWrappingEndpoint {

    public HystrixStreamEndpoint() {
        super(HystrixMetricsStreamServlet.class, "hystrixStream", "/hystrix.stream",
                true, true);
    }

}

不幸的是,由于它从请求 URI 中提取前缀的方式,TogglzConsoleServlet还有一个与路径有关的 hack,所以整个解决方案看起来有点难看:

@Component
class TogglzConsoleEndpoint implements MvcEndpoint {

    private static final String ADMIN_CONSOLE_URL = "/togglz-console";
    private final TogglzConsoleServlet togglzConsoleServlet;

    @Autowired
    TogglzConsoleEndpoint(final ServletContext servletContext) throws ServletException {
        this.togglzConsoleServlet = new TogglzConsoleServlet();
        togglzConsoleServlet.init(new DelegatingServletConfig(servletContext));
    }

    @Override
    public String getPath() {
        return ADMIN_CONSOLE_URL;
    }

    @Override
    public boolean isSensitive() {
        return true;
    }

    @Override
    public Class<? extends Endpoint> getEndpointType() {
        return null;
    }

    @RequestMapping("**")
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request) {
            @Override
            public String getServletPath() {
                return ADMIN_CONSOLE_URL;
            }
        };
        togglzConsoleServlet.service(requestWrapper, response);
        return null;
    }

    private class DelegatingServletConfig implements ServletConfig {

        private final ServletContext servletContext;

        DelegatingServletConfig(final ServletContext servletContext) {
            this.servletContext = servletContext;
        }

        @Override
        public String getServletName() {
            return TogglzConsoleEndpoint.this.togglzConsoleServlet.getServletName();
        }

        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }

        @Override
        public String getInitParameter(final String name) {
            return servletContext.getInitParameter(name);
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return servletContext.getInitParameterNames();
        }
    }
}
于 2016-11-29T14:56:40.303 回答