/** * /!\ Work only if application run on embedded Tomcat server * * @return * @throws Exception */ public static String getBaseUrlForEmbeddedTomcat() throws Exception { // get embedded tomcat EmbeddedWebApplicationContext appContext = (EmbeddedWebApplicationContext) new ApplicationContextProvider().getApplicationContext(); Tomcat tomcat = ((TomcatEmbeddedServletContainer) appContext.getEmbeddedServletContainer()).getTomcat(); Connector connector = tomcat.getConnector(); // compose address String scheme = connector.getScheme(); String hostName = tomcat.getHost().getName(); int port = connector.getPort(); String contextPath = appContext.getServletContext().getContextPath(); return scheme + "://" + hostName + ":" + port + contextPath; }
/** * Return the actual port file that should be written for the given application * context. The default implementation builds a file from the source file and the * application context namespace. * @param applicationContext the source application context * @return the file that should be written */ protected File getPortFile(EmbeddedWebApplicationContext applicationContext) { String contextName = applicationContext.getNamespace(); if (StringUtils.isEmpty(contextName)) { return this.file; } String name = this.file.getName(); String extension = StringUtils.getFilenameExtension(this.file.getName()); name = name.substring(0, name.length() - extension.length() - 1); if (isUpperCase(name)) { name = name + "-" + contextName.toUpperCase(); } else { name = name + "-" + contextName.toLowerCase(); } if (StringUtils.hasLength(extension)) { name = name + "." + extension; } return new File(this.file.getParentFile(), name); }
@Test public void registerWithSimpleWebApp() throws Exception { this.context = new SpringApplicationBuilder() .sources(EmbeddedServletContainerAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class, JmxAutoConfiguration.class, SpringApplicationAdminJmxAutoConfiguration.class) .run("--" + ENABLE_ADMIN_PROP, "--server.port=0"); assertThat(this.context).isInstanceOf(EmbeddedWebApplicationContext.class); assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(), "EmbeddedWebApplication")).isEqualTo(Boolean.TRUE); int expected = ((EmbeddedWebApplicationContext) this.context) .getEmbeddedServletContainer().getPort(); String actual = getProperty(createDefaultObjectName(), "local.server.port"); assertThat(actual).isEqualTo(String.valueOf(expected)); }
@Override public void afterSingletonsInstantiated() { ManagementServerPort managementPort = ManagementServerPort.DIFFERENT; if (this.applicationContext instanceof WebApplicationContext) { managementPort = ManagementServerPort .get(this.applicationContext.getEnvironment(), this.beanFactory); } if (managementPort == ManagementServerPort.DIFFERENT) { if (this.applicationContext instanceof EmbeddedWebApplicationContext && ((EmbeddedWebApplicationContext) this.applicationContext) .getEmbeddedServletContainer() != null) { createChildManagementContext(); } else { logger.warn("Could not start embedded management container on " + "different port (management endpoints are still available " + "through JMX)"); } } if (managementPort == ManagementServerPort.SAME && this.applicationContext .getEnvironment() instanceof ConfigurableEnvironment) { addLocalManagementPortPropertyAlias( (ConfigurableEnvironment) this.applicationContext.getEnvironment()); } }
private Integer getServicePort(EmbeddedWebApplicationContext webContext) { Integer port = null; if (webContext != null) { final EmbeddedServletContainer container = webContext.getEmbeddedServletContainer(); if (container instanceof TomcatEmbeddedServletContainer) { // Work around an issue with the tomcat container, which uses the local port // as the port (-1) instead of the registered port port = ((TomcatEmbeddedServletContainer) container).getTomcat().getConnector().getPort(); } else { port = container.getPort(); } } return port; }
@Test public void registerWithSimpleWebApp() throws Exception { this.context = new SpringApplicationBuilder() .sources(EmbeddedServletContainerAutoConfiguration.class, ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class, JmxAutoConfiguration.class, SpringApplicationAdminJmxAutoConfiguration.class) .run("--" + ENABLE_ADMIN_PROP, "--server.port=0"); assertTrue(this.context instanceof EmbeddedWebApplicationContext); assertEquals(true, this.mBeanServer.getAttribute(createDefaultObjectName(), "EmbeddedWebApplication")); int expected = ((EmbeddedWebApplicationContext) this.context) .getEmbeddedServletContainer().getPort(); String actual = getProperty(createDefaultObjectName(), "local.server.port"); assertEquals(String.valueOf(expected), actual); }
/** * spring boot 服务主入口 * * @param args */ public static void main(String[] args) { ApplicationContext context = SpringApplication.run(Bootstrap.class, args); if (context instanceof EmbeddedWebApplicationContext) { int port = ((EmbeddedWebApplicationContext) context).getEmbeddedServletContainer().getPort(); String contextPath = context.getApplicationName(); String url = String.format(Locale.US, "http://localhost:%d%s", port, contextPath); if (log.isInfoEnabled()) { //提示项目用到的相关配置文件 log.info(" =========== ${user.dir}={} =========== ", System.getProperty("user.dir")); log.info(" =========== ${java.io.tmpdir}={} =========== ", System.getProperty("java.io.tmpdir")); String dashes = "------------------------------------------------------------------------"; log.info("Access URLs:\n{}\n\tLocal: \t\t{}\n{}", dashes, url, dashes); } } }
@Bean public TomcatMetrics metrics(ApplicationContext applicationContext) { Manager manager = null; if (applicationContext instanceof EmbeddedWebApplicationContext) { manager = getManagerFromContext((EmbeddedWebApplicationContext) applicationContext); } return new TomcatMetrics(manager, Collections.emptyList()); }
private Manager getManagerFromContext(EmbeddedWebApplicationContext applicationContext) { EmbeddedServletContainer embeddedServletContainer = applicationContext.getEmbeddedServletContainer(); if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) { return getManagerFromContainer((TomcatEmbeddedServletContainer) embeddedServletContainer); } return null; }
public static void main(String[] args) { ApplicationContext context = SpringApplication.run(Application.class, args); if (log.isInfoEnabled() && context instanceof EmbeddedWebApplicationContext) { int port = ((EmbeddedWebApplicationContext) context).getEmbeddedServletContainer().getPort(); String contextPath = context.getApplicationName(); final String dashes = Strings.repeat("-", 72); log.info("Access URLs:\n{}\n\tLocal:\t\thttp://localhost:{}{}\n{}", dashes, port, contextPath, dashes); } }
@Override public void afterSingletonsInstantiated() { ManagementServerPort managementPort = ManagementServerPort.DIFFERENT; if (this.applicationContext instanceof WebApplicationContext) { managementPort = ManagementServerPort .get(this.applicationContext.getEnvironment(), this.beanFactory); } if (managementPort == ManagementServerPort.DIFFERENT) { if (this.applicationContext instanceof EmbeddedWebApplicationContext && ((EmbeddedWebApplicationContext) this.applicationContext) .getEmbeddedServletContainer() != null) { createChildManagementContext(); } else { logger.warn("Could not start embedded management container on " + "different port (management endpoints are still available " + "through JMX)"); } } if (managementPort == ManagementServerPort.SAME) { if (new RelaxedPropertyResolver(this.applicationContext.getEnvironment(), "management.ssl.").getProperty("enabled") != null) { throw new IllegalStateException( "Management-specific SSL cannot be configured as the management " + "server is not listening on a separate port"); } if (this.applicationContext .getEnvironment() instanceof ConfigurableEnvironment) { addLocalManagementPortPropertyAlias( (ConfigurableEnvironment) this.applicationContext .getEnvironment()); } } }
@Override public Collection<Metric<?>> metrics() { if (this.applicationContext instanceof EmbeddedWebApplicationContext) { Manager manager = getManager( (EmbeddedWebApplicationContext) this.applicationContext); if (manager != null) { return metrics(manager); } } return Collections.emptySet(); }
private Manager getManager(EmbeddedWebApplicationContext applicationContext) { EmbeddedServletContainer embeddedServletContainer = applicationContext .getEmbeddedServletContainer(); if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) { return getManager((TomcatEmbeddedServletContainer) embeddedServletContainer); } return null; }
private EmbeddedServletContainerInitializedEvent mockEvent(String name, int port) { EmbeddedWebApplicationContext applicationContext = mock( EmbeddedWebApplicationContext.class); EmbeddedServletContainer source = mock(EmbeddedServletContainer.class); given(applicationContext.getNamespace()).willReturn(name); given(source.getPort()).willReturn(port); EmbeddedServletContainerInitializedEvent event = new EmbeddedServletContainerInitializedEvent( applicationContext, source); return event; }
protected String getPropertyName(EmbeddedWebApplicationContext context) { String name = context.getNamespace(); if (StringUtils.isEmpty(name)) { name = "server"; } return "local." + name + ".port"; }
@Override public void finished(ConfigurableApplicationContext context, Throwable exception) { applicationContext = (EmbeddedWebApplicationContext) context; cdl.countDown(); }
private boolean isRunningInEmbeddedContainer() { return this.applicationContext instanceof EmbeddedWebApplicationContext && ((EmbeddedWebApplicationContext) this.applicationContext).getServletContext() == null; }
@Override public boolean isEmbeddedWebApplication() { return (SpringApplicationAdminMXBeanRegistrar.this.applicationContext != null && SpringApplicationAdminMXBeanRegistrar.this.applicationContext instanceof EmbeddedWebApplicationContext); }
private boolean isRunningInEmbeddedContainer() { return this.applicationContext instanceof EmbeddedWebApplicationContext && ((EmbeddedWebApplicationContext) this.applicationContext) .getServletContext() == null; }