gpt4 book ai didi

tomcat - 如何在 spring boot 1.3.x 中为嵌入式 tomcat 添加 tomcat session 复制

转载 作者:行者123 更新时间:2023-11-28 23:59:29 26 4
gpt4 key购买 nike

我正在使用 spring boot 版本 1.3.1,我想在我的嵌入式 tomcat 应用程序中添加 tomcat session 复制。我有很多谷歌,但无处可寻。我已经使用 jhipster 生成了我的 spring boot 应用程序,我生成的 WebConfigurer 文件代码是:

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.servlet.InstrumentedFilter;
import com.codahale.metrics.servlets.MetricsServlet;
import in.megacabs.web.filter.CachingHttpHeadersFilter;
import in.megacabs.web.filter.StaticResourcesProductionFilter;
import in.megacabs.web.rest.filter.megacabs.ServerModifiedFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.MimeMappings;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.*;
import javax.inject.Inject;
import javax.servlet.*;

/**
* Configuration of web application with Servlet 3.0 APIs.
*/
@Configuration
@AutoConfigureAfter(CacheConfiguration.class)
public class WebConfigurer extends WebMvcConfigurerAdapter implements ServletContextInitializer, EmbeddedServletContainerCustomizer {

private final Logger log = LoggerFactory.getLogger(WebConfigurer.class);

@Inject
private Environment env;

@Inject
private JHipsterProperties props;

@Autowired(required = false)
private MetricRegistry metricRegistry;

@Inject
private JasperReportConfiguration reportConfiguration;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler(reportConfiguration.getUploadUrl()+"*//**").addResourceLocations("file://"+ reportConfiguration.getUploadPath()+"/");
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
log.info("Web application configuration, using profiles: {}", Arrays.toString(env.getActiveProfiles()));
EnumSet<DispatcherType> disps = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC);
if (!env.acceptsProfiles(Constants.SPRING_PROFILE_FAST)) {
initMetrics(servletContext, disps);
}
if (env.acceptsProfiles(Constants.SPRING_PROFILE_PRODUCTION)) {
initCachingHttpHeadersFilter(servletContext, disps);
initStaticResourcesProductionFilter(servletContext, disps);
}
initServerModifiedFilter(servletContext, disps);

log.info("Web application fully configured");
}

/**
* Set up Mime types.
*/
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
// IE issue, see https://github.com/jhipster/generator-jhipster/pull/711
mappings.add("html", "text/html;charset=utf-8");
// CloudFoundry issue, see https://github.com/cloudfoundry/gorouter/issues/64
mappings.add("json", "text/html;charset=utf-8");
container.setMimeMappings(mappings);
}

/**
* Initializes the static resources production Filter.
*/
private void initStaticResourcesProductionFilter(ServletContext servletContext,
EnumSet<DispatcherType> disps) {

log.debug("Registering static resources production Filter");
FilterRegistration.Dynamic staticResourcesProductionFilter =
servletContext.addFilter("staticResourcesProductionFilter",
new StaticResourcesProductionFilter());

staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/");
staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/index.html");
staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/assets/*");
staticResourcesProductionFilter.addMappingForUrlPatterns(disps, true, "/scripts/*");
staticResourcesProductionFilter.setAsyncSupported(true);
}

/**
* Initializes the caching HTTP Headers Filter.
*/
private void initCachingHttpHeadersFilter(ServletContext servletContext,
EnumSet<DispatcherType> disps) {
log.debug("Registering Caching HTTP Headers Filter");
FilterRegistration.Dynamic cachingHttpHeadersFilter =
servletContext.addFilter("cachingHttpHeadersFilter",
new CachingHttpHeadersFilter(env));

cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/dist/assets/*");
cachingHttpHeadersFilter.addMappingForUrlPatterns(disps, true, "/dist/scripts/*");
cachingHttpHeadersFilter.setAsyncSupported(true);
}

/**
* Initializes Metrics.
*/
private void initMetrics(ServletContext servletContext, EnumSet<DispatcherType> disps) {
log.debug("Initializing Metrics registries");
servletContext.setAttribute(InstrumentedFilter.REGISTRY_ATTRIBUTE,
metricRegistry);
servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY,
metricRegistry);

log.debug("Registering Metrics Filter");
FilterRegistration.Dynamic metricsFilter = servletContext.addFilter("webappMetricsFilter",
new InstrumentedFilter());

metricsFilter.addMappingForUrlPatterns(disps, true, "/*");
metricsFilter.setAsyncSupported(true);

log.debug("Registering Metrics Servlet");
ServletRegistration.Dynamic metricsAdminServlet =
servletContext.addServlet("metricsServlet", new MetricsServlet());

metricsAdminServlet.addMapping("/metrics/metrics/*");
metricsAdminServlet.setAsyncSupported(true);
metricsAdminServlet.setLoadOnStartup(2);
}

/**
* Initializes the serverModified Filter.
*/
private void initServerModifiedFilter(ServletContext servletContext,
EnumSet<DispatcherType> disps) {
log.debug("Registering static resources production Filter");
FilterRegistration.Dynamic serverModifiedFilter =
servletContext.addFilter("serverModifiedFilter",
new ServerModifiedFilter());

serverModifiedFilter.addMappingForUrlPatterns(disps, true, "/api/*");
serverModifiedFilter.setAsyncSupported(true);
}

@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = props.getCors();
if (config.getAllowedOrigins() != null && !config.getAllowedOrigins().isEmpty()) {
source.registerCorsConfiguration("/api/**", config);
source.registerCorsConfiguration("/v2/api-docs", config);
source.registerCorsConfiguration("/oauth/**", config);
}
return new CorsFilter(source);
}

}

我添加了上面的代码只是为了让你知道我在我的网络配置文件中做了什么。我找到了这个链接How to setup a Spring Boot application with embedded tomcat session clustering?但这对我没有帮助,因为有些类不在我的嵌入式 tomcat 类路径中。

如果有人之前设置过 tomcat session 复制,请帮助我。

最佳答案

我们使用 Hazelcast 在我们的企业应用程序中进行 session 复制/集群。

您需要将相关依赖添加到您的build.gradle 文件中:

dependencies {
//Main dependencies
compile
'com.hazelcast:hazelcast:3.3.5',
'com.hazelcast:hazelcast-wm:3.3.5',
'com.hazelcast:hazelcast-spring:3.3.5'
}

然后我们创建一个 Hazelcast 配置类:

import com.hazelcast.config.Config
import com.hazelcast.config.JoinConfig
import com.hazelcast.config.MulticastConfig
import com.hazelcast.config.NetworkConfig
import com.hazelcast.core.Hazelcast
import com.hazelcast.core.HazelcastInstance
import com.hazelcast.web.SessionListener
import com.hazelcast.web.WebFilter
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.embedded.FilterRegistrationBean
import org.springframework.boot.context.embedded.ServletListenerRegistrationBean
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.DependsOn

import javax.servlet.DispatcherType

@Configuration
@ConfigurationProperties(prefix="hzl")
@ConditionalOnProperty(value= "hzl.enabled")
class HazelcastConfig {
String address
Integer port
Integer multicastPort
String instanceName
Integer multicastTimeoutSeconds

@Bean
public hazelcastInstance(){
Config config = new Config()
config.instanceName = instanceName

config.setProperty("hazelcast.jmx", "true")

NetworkConfig network = config.networkConfig
network.port = port ?: network.port

JoinConfig join = network.join
join.tcpIpConfig.enabled = false
join.awsConfig.enabled = false

MulticastConfig multicastConfig = join.multicastConfig
multicastConfig.enabled = true
multicastConfig.multicastGroup = address

multicastConfig.multicastPort = multicastPort ?: multicastConfig.multicastPort
multicastConfig.multicastTimeoutSeconds = multicastTimeoutSeconds ?: multicastConfig.multicastTimeoutSeconds

HazelcastInstance instance = Hazelcast.newHazelcastInstance(config)
instance
}

@Bean
@DependsOn(["hazelcastInstance"])
public FilterRegistrationBean hazelcastFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean()
WebFilter filter = new WebFilter()

registration.filter = filter
registration.order = 0
registration.name = "hazelcastFilter"
registration.addUrlPatterns("/*")
registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE)

registration.addInitParameter("instance-name", instanceName)
// registration.addInitParameter("session-ttl-seconds", "3600")
registration.addInitParameter("sticky-session", "false")
registration.addInitParameter("deferred-write", "false")
registration.addInitParameter("sticky-session", "true")

registration
}

@Bean
public ServletListenerRegistrationBean hazelcastSessionListener() {
return new ServletListenerRegistrationBean(new SessionListener());
}
}

这取决于 application.properties 中以 hzl. 为前缀的特定属性:

#Hazelcast Session Replication
hzl.enabled=true
hzl.address=224.2.2.110
hzl.multicastTimeoutSeconds=60

那应该就可以了。现在,Hazelcast 会将所有 session 复制/复制到集群中广播并监听 IP 地址 224.2.2.110 的每个服务器。

有关 Hazelcast 的更多信息,请参阅 Hazelcast Download and Documentation for Java 上的文档

关于tomcat - 如何在 spring boot 1.3.x 中为嵌入式 tomcat 添加 tomcat session 复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39659465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com