首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使Javamelody使用不同的端口(Spring Boot+two HTTP端口公开)

问如何使Javamelody使用不同的端口(Spring Boot+two HTTP端口公开)
EN

Stack Overflow用户
提问于 2016-12-14 14:56:56
回答 3查看 2.4K关注 0票数 3

我有Spring应用程序。它在端口8080上公开REST。它还公开了带有(http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html)的管理端口8081。我没有任何定制的Tomcat配置来实现这一点。我只有属性management.port=8081在我的application.properties文件中。

我将JavaMelody配置为https://github.com/javamelody/javamelody/wiki/UserGuideAdvanced#spring-boot-app中所描述的(我有自己的自定义JavaMelodyConfiguration类,使用注册net.bull.javamelody.MonitoringFilter的org.springframework.boot.web.servlet.FilterRegistrationBean )。

代码语言:javascript
复制
@Bean
    public FilterRegistrationBean javaMelody() {
        final FilterRegistrationBean javaMelody = new FilterRegistrationBean();
        javaMelody.setFilter(new MonitoringFilter());
        javaMelody.setAsyncSupported(true);
        javaMelody.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.ASYNC);
        javaMelody.addUrlPatterns("/*");
        return javaMelody;
    }

使用此配置,Javamelody将公开在端口8080 (业务端口)上。我想把它移到8081(管理端口)。怎么改变呢?

我使用Spring 1.4.2. javamelody,javamelody 1.62.0

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-02-13 14:27:29

如果目标是公开管理端口上的监视,则从java旋律开始,版本1.76现在要简单得多。

您需要SpringBoot2.x、执行器和yml或属性文件:

  • management.server.port:{您的自定义端口}
  • management.endpoints.web.exposure.include:{您通常想要的},监视
  • javamelody.management-endpoint-monitoring-enabled:真

请参阅这里的详细信息:https://github.com/javamelody/javamelody/wiki/SpringBootStarter#configuration-in-case-of-management-port

票数 3
EN

Stack Overflow用户

发布于 2016-12-14 17:02:50

编辑:这个答案仍然是正确的,但为了更简单的解决方案,请参阅已接受的答案。

EmbeddedTomcatConfiguration.java

代码语言:javascript
复制
package ...

import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.connector.Connector;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EmbeddedTomcatConfiguration {

    @Value("${server.additionalPorts}")
    private String additionalPorts;

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        Connector[] additionalConnectors = this.additionalConnector();
        if (additionalConnectors != null && additionalConnectors.length > 0) {
            tomcat.addAdditionalTomcatConnectors(additionalConnectors);
        }
        return tomcat;
    }

    private Connector[] additionalConnector() {
        if (StringUtils.isBlank(this.additionalPorts)) {
            return null;
        }
        String[] ports = this.additionalPorts.split(",");
        List<Connector> result = new ArrayList<>();
        for (String port : ports) {
            Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
            connector.setScheme("http");
            connector.setPort(Integer.valueOf(port));
            result.add(connector);
        }
        return result.toArray(new Connector[] {});
    }
}

application.yml

代码语言:javascript
复制
server:
  port: ${appPort:8800}
  additionalPorts: 8880,8881

Application.java

代码语言:javascript
复制
@SpringBootApplication
@ComponentScan(...)
@Import(EmbeddedTomcatConfiguration.class)
public Application {

    public static void main(String[] args) {
        SpringApplication.run(Application .class, args);
    }
}

而我关于限制从特定端口访问javamelody的建议是扩展javamelody过滤器,如果请求来自特定端口,则只需链接请求,否则就发送404。

从日志中:

代码语言:javascript
复制
INFO  TomcatEmbeddedServletContainer:185 - Tomcat started on port(s): 8800 (http) 8880 (http) 8881 (http)

这种方法BTW公开了这些端口上的其他端点。要解决这个问题,并将javamelody filter (/monitoring)限制在特定端口上,您需要编写一个筛选器来验证从允许端口请求路径(servlet和filter路径),同时要记住这些过滤器的排序非常重要。

基于这个答案和我在回答这个问题时已经可用的部分源代码,我在http://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html上发表了一篇关于这个主题的博客文章。

票数 2
EN

Stack Overflow用户

发布于 2017-01-02 14:32:13

您可以通过一个ReportServlet使用MvcEndpoint。就像这样:

代码语言:javascript
复制
    import net.bull.javamelody.MonitoringFilter;
    import net.bull.javamelody.ReportServlet;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.actuate.endpoint.Endpoint;
    import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.GetMapping;

    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    /**
     * We configure the Java Melody {@link MonitoringFilter} normally, but disables all access to the UI. Instead,
     * we create a {@link ReportServlet}, and expose it through a {@link MvcEndpoint} in {@link #javaMelodyReportEndpoint()}.
     */
    @Configuration
    public class JavaMelodyConfiguration {

        private final ServletConfig servletConfig;

        @Autowired
        public JavaMelodyConfiguration(ServletConfig servletConfig) {
            this.servletConfig = servletConfig;
        }

        @Bean
        MvcEndpoint javaMelodyReportEndpoint() {
            ReportServlet reportServlet = new ReportServlet();
            // We initialize the servlet with the servlet configuration from the server that runs on server.port, as
            // it currently only uses it to access the Collector instance, and some system information.
            reportServlet.init(servletConfig);

            return new MvcEndpoint() {
                @Override
                public String getPath() {
                    return "/monitoring";
                }

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

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

                @GetMapping
                public void report(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException {
                    reportServlet.service(httpRequest, httpResponse);
                }
            };
        }

        @Bean
        FilterRegistrationBean javaMelodyFilterRegistration() {
            FilterRegistrationBean javaMelody = new FilterRegistrationBean();
            javaMelody.setFilter(monitoringFilter());
            javaMelody.setName("javamelody");
            return javaMelody;
        }

        @Bean
        MonitoringFilter monitoringFilter() {
            return new MonitoringFilter() {
                @Override
                protected boolean isAllowed(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
                    // We allow no access to the report (/monitoring) from this filter, access is done through the
                    // MvcEndpoint above, using the management port.
                    return false;
                }
            };
        }
    }

(我也在这里发布了这个:https://github.com/javamelody/javamelody/issues/601)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41145640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档