我有春天启动的Web应用程序。 它暴露的端口REST API 8080也暴露出管理端口8081春季引导管理端点( http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html ) 。 我没有任何自定义Tomcat配置,以实现这一目标。 我只是财产management.port=8081
在我的application.properties
文件。
我已经JavaMelody配置中描述https://github.com/javamelody/javamelody/wiki/UserGuideAdvanced#spring-boot-app (我有我的自定义JavaMelodyConfiguration
类,有org.springframework.boot.web.servlet.FilterRegistrationBean
该寄存器net.bull.javamelody.MonitoringFilter
)。
@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.RELEASE,javamelody 1.62.0
EmbeddedTomcatConfiguration.java
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
server:
port: ${appPort:8800}
additionalPorts: 8880,8881
Application.java
@SpringBootApplication
@ComponentScan(...)
@Import(EmbeddedTomcatConfiguration.class)
public Application {
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
我从一个特定的端口限制访问javamelody的建议是延长javamelody过滤器,如果它来自一个特定端口,否则发回404只链中的请求。
从日志:
INFO TomcatEmbeddedServletContainer:185 - Tomcat started on port(s): 8800 (http) 8880 (http) 8881 (http)
这种方法BTW暴露于这些端口其他端点。 为了解决这个问题,并限制javamelody过滤器(/监控)到特定端口,你需要编写一个过滤器,验证路径(Servlet和过滤器路径),牢记这些过滤器的顺序很重要允许的端口被请求。
在此基础上的答案,并且我已经可以当我回答这个问题的部分源代码,我在曾发表一篇博客文章中有关此主题的http://tech.asimio.net/2016/12/15/Configuring-Tomcat-to -听-对-多端口,使用弹簧,Boot.html
您可以通过MvcEndpoint使用ReportServlet。 事情是这样的:
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 )
文章来源: How to make Javamelody use different port (Spring Boot+two HTTP ports exposed)