我有Spring Boot Web应用程序。它在端口8080上公开REST API。它还使用Spring Boot Management端点(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
课程,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 Boot 1.4.2.RELEASE,javamelody 1.62.0
答案 0 :(得分:3)
如果目标是从Java melody版本1.76开始在管理端口上公开监视,则现在变得更加简单。
您需要Spring Boot 2.x,执行器以及yml或属性文件中的
在此处查看更多详细信息: https://github.com/javamelody/javamelody/wiki/SpringBootStarter#configuration-in-case-of-management-port
答案 1 :(得分:2)
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过滤器(/ monitoring)限制到特定端口,您需要编写一个过滤器来验证从允许端口请求的路径(servlet和过滤器路径),同时记住这些过滤器的排序很重要。 / p>
基于我在回答此问题时已经提供的此答案和部分源代码,我在http://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html
发布了一篇关于此主题的博客文章答案 2 :(得分:0)
您可以通过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)