2016-12-14 11 views
0

私はSpringブートWebアプリケーションを持っています。 REST APIをポート8080で公開します。また、管理ポート8081をSpringブート管理エンドポイント(http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html)で公開します。 これを実現するためのカスタムTomcat設定はありません。私はちょうど私のapplication.propertiesファイルにmanagement.port=8081のプロパティを持っています。 https://github.com/javamelody/javamelody/wiki/UserGuideAdvanced#spring-boot-app で説明したように、私はJavaMelody設定した Javamelodyで別のポートを使用する方法(Springブート+公開された2つのHTTPポート)

は(私が登録し org.springframework.boot.web.servlet.FilterRegistrationBean net.bull.javamelody.MonitoringFilterで、私のカスタム JavaMelodyConfigurationクラスを持っています)。

@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(管理ポート)に移動したいと思います。それを変更するには?

私は春ブーツ1.4.2.RELEASE、javamelodyを使用1.62.0

+0

あなたが8080のプロジェクト、あなたは、これは私の問題を解決しないであろう8081 – StackFlowed

+0

にそれを変更する場所を見つけるかどうかを確認、検索、このような何か。私はまだ私のアプリケーションがポート8080でビジネスREST APIを公開し、8081で管理APIを、そして8081でjavamelodyを公開したい。 –

答えて

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); 
    } 
} 
INFO TomcatEmbeddedServletContainer:185 - Tomcat started on port(s): 8800 (http) 8880 (http) 8881 (http) 
:および特定のポートからのアクセスjavamelodyを制限するための私の提案は、javamelodyフィルタを拡張し、それ以外のログから404

を送り返す特定のポートから来る場合だけ、要求をチェーンになります

このアプローチは、これらのポート上の他のエンドポイントを公開します。 これを解決し、javamelodyフィルタ(/監視)を特定のポートに制限するには、これらのフィルタの順序が重要であることを覚えておいて、許容ポートから要求されているパス(サーブレットとフィルタパス)を検証するフィルタを作成する必要があります。この答えと私はこの質問に答えたとき、私はあなたがMvcEndpointを通じてReportServletを使用することができますhttp://tech.asimio.net/2016/12/15/Configuring-Tomcat-to-Listen-on-Multiple-ports-using-Spring-Boot.html

+0

ありがとう!このような構成では、記述したようにjavamelodyを登録すると(私はそれを含む質問を編集しました)、net.bull.javamelody.MonitoringFilterは両方のポートに登録されますか? –

+0

Yw、管理ポートと管理コンテキストパスを使用してJavamelody UIにアクセスしたいと仮定して、Javamelodyフィルタを/ admin/monitoringにマッピングします。 – ootero

+0

私はserver.servlet-pathを/、management.context-path/adminに設定する – ootero

0

でこのトピックについてのブログ記事を発表していた、すでに利用可能だった部分のソースコードに基づいて

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)を

関連する問題