Skip to content

Instantly share code, notes, and snippets.

@fastnsilver
Last active August 19, 2019 03:18
Show Gist options
  • Select an option

  • Save fastnsilver/5719ef01155c5e547d3b to your computer and use it in GitHub Desktop.

Select an option

Save fastnsilver/5719ef01155c5e547d3b to your computer and use it in GitHub Desktop.
Spring Boot Jetty9 enhanced config
# Jetty configuration
# No single request should take longer than 15s to return a response
jetty:
threadPool:
minThreads: 25
maxThreads: 500
idleTimeout: 15000
detailedDump: false
lowResources:
period: 1000
idleTimeout: 200
monitorThreads: true
maxConnections: 0
maxMemory: 0
maxLowResourcesTime: 5000
import org.eclipse.jetty.server.LowResourceMonitor;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool;
@Configuration
@ConditionalOnClass(MetricRegistry.class)
@ConditionalOnBean(MetricRegistry.class)
@EnableConfigurationProperties(value={JettyThreadPoolSettings.class, JettyLowResourceMonitorSettings.class})
public class JettyConfig {
// @see http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html#production-ready-code-hale-metrics
@Autowired
private MetricRegistry metricRegistry;
@Autowired
private JettyThreadPoolSettings jettyThreadPoolSettings;
@Autowired
private JettyLowResourceMonitorSettings jettyLowResourceMonitorSettings;
// @see http://wiki.eclipse.org/Jetty/Howto/High_Load
// @see http://jdpgrailsdev.github.io/blog/2014/10/07/spring_boot_jetty_thread_pool.html
@Primary
@Bean
@ConditionalOnClass(value={Server.class, InstrumentedQueuedThreadPool.class})
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(
@Value("${server.port:8080}") final int port) {
final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(port);
factory.addServerCustomizers(customizer -> { jettyServer(jettyThreadPoolSettings, jettyLowResourceMonitorSettings); });
return factory;
}
private Server jettyServer(JettyThreadPoolSettings jettyThreadPoolSettings, JettyLowResourceMonitorSettings jettyLowResourceMonitorSettings) {
QueuedThreadPool threadPool = new InstrumentedQueuedThreadPool(metricRegistry);
threadPool.setMaxThreads(jettyThreadPoolSettings.getMaxThreads());
threadPool.setMinThreads(jettyThreadPoolSettings.getMinThreads());
threadPool.setIdleTimeout(jettyThreadPoolSettings.getIdleTimeout());
threadPool.setDetailedDump(jettyThreadPoolSettings.isDetailedDump());
Server server = new Server(threadPool);
LowResourceMonitor lowResourcesMonitor = new LowResourceMonitor(server);
lowResourcesMonitor.setPeriod(jettyLowResourceMonitorSettings.getPeriod());
lowResourcesMonitor.setLowResourcesIdleTimeout(jettyLowResourceMonitorSettings.getIdleTimeout());
lowResourcesMonitor.setMonitorThreads(jettyLowResourceMonitorSettings.isMonitorThreads());
lowResourcesMonitor.setMaxConnections(jettyLowResourceMonitorSettings.getMaxConnections());
lowResourcesMonitor.setMaxMemory(jettyLowResourceMonitorSettings.getMaxMemory());
lowResourcesMonitor.setMaxLowResourcesTime(jettyLowResourceMonitorSettings.getMaxLowResourcesTime());
server.addBean(lowResourcesMonitor);
return server;
}
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "jetty.lowResources")
// @see http://eclipse.org/jetty/documentation/current/limit-load.html
// @see http://eclipse.org/jetty/documentation/current/embedding-jetty.html#d0e19129
class JettyLowResourceMonitorSettings {
// all durations in milliseconds
private Integer period = 1000;
private Integer idleTimeout = 200;
private boolean monitorThreads = true;
private Integer maxConnections = 0;
private Integer maxMemory;
private Integer maxLowResourcesTime = 5000;
public Integer getPeriod() {
return period;
}
public void setPeriod(Integer period) {
this.period = period;
}
public Integer getIdleTimeout() {
return idleTimeout;
}
public void setIdleTimeout(Integer idleTimeout) {
this.idleTimeout = idleTimeout;
}
public boolean isMonitorThreads() {
return monitorThreads;
}
public void setMonitorThreads(boolean monitorThreads) {
this.monitorThreads = monitorThreads;
}
public Integer getMaxConnections() {
return maxConnections;
}
public void setMaxConnections(Integer maxConnections) {
this.maxConnections = maxConnections;
}
public Integer getMaxMemory() {
return maxMemory;
}
public void setMaxMemory(Integer maxMemory) {
this.maxMemory = maxMemory;
}
public Integer getMaxLowResourcesTime() {
return maxLowResourcesTime;
}
public void setMaxLowResourcesTime(Integer maxLowResourcesTime) {
this.maxLowResourcesTime = maxLowResourcesTime;
}
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "jetty.threadPool")
class JettyThreadPoolSettings {
private Integer minThreads = 8;
private Integer maxThreads = 200;
private Integer idleTimeout = 30000;
private boolean detailedDump;
public Integer getMinThreads() {
return minThreads;
}
public void setMinThreads(Integer minThreads) {
this.minThreads = minThreads;
}
public Integer getMaxThreads() {
return maxThreads;
}
public void setMaxThreads(Integer maxThreads) {
this.maxThreads = maxThreads;
}
public Integer getIdleTimeout() {
return idleTimeout;
}
public void setIdleTimeout(Integer idleTimeout) {
this.idleTimeout = idleTimeout;
}
public boolean isDetailedDump() {
return detailedDump;
}
public void setDetailedDump(boolean detailedDump) {
this.detailedDump = detailedDump;
}
}
<!-- @see http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html#production-ready-code-hale-metrics -->
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
</dependency>
<!-- @see com.expedia.ewe.lodging.commons.spring.config.JettyConfig -->
<!-- Expose threadpool and session info -->
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-jetty9</artifactId>
<version>${dropwizard-metrics.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
@acidlychee

Copy link
Copy Markdown

It seems not work..can't create a server..just can change the existed server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment