首页 类库下载 其它类库 spring-session redis集群配置步骤总结

spring-session redis集群配置步骤总结

Oct 15, 2016 pm 05:00 PM

总结配置步骤

简单四步,开始 spring-session redis 配置人生

1. pom.xml 添加 jar依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    ....

    <properties>
        <version.spring-session>1.2.2.RELEASE</version.spring-session>        <!--不兼容 1.7.4.RELEASE -->
        <version.spring-data-redis>1.7.1.RELEASE</version.spring-data-redis>

    </properties>

    <dependencies>

        ....

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>${version.spring-session}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>${version.spring-session}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>${version.spring-data-redis}</version>
        </dependency> 

        ....

    </dependencies>

</project>
登录后复制

2. web.xml 添加 springSessionRepositoryFilter 配置下载

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

        ...        <!-- spring-session config -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath*:spring-session-cluster.xml
            </param-value>
        </context-param>        <!-- 这个filter 要放在第一个 -->
        <filter>
            <filter-name>springSessionRepositoryFilter</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>springSessionRepositoryFilter</filter-name>
            <url-pattern>/*</url-pattern>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>ERROR</dispatcher>
        </filter-mapping>
        ...
    </web-app>
登录后复制

3. spring-session-cluster.xml下载

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd               ">

        <context:annotation-config />

        <util:properties id="redis" location="classpath:redis-cluster.properties"></util:properties>        <!-- RedisHttpSessionConfiguration -->
        <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />        <!--JedisConnectionFactory -->
        <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <constructor-arg index="0">                <!-- since spring-data-redis 1.7 -->
                <bean class="org.springframework.data.redis.connection.RedisClusterConfiguration">
                    <constructor-arg index="0">
                        <set>
                            <value>#{redis[&#39;redis.redisClusterConfiguration.clusters&#39;]}</value>
                        </set>
                    </constructor-arg>                    <!--                        用于 redis.clients.jedis.JedisCluster.JedisCluster(Set<HostAndPort>, int, int, GenericObjectPoolConfig) 第三个参数 maxRedirections                        默认值是5                        一般当此值设置过大时,容易报:Too many Cluster redirections                    -->
                    <property name="maxRedirects" value="#{redis[&#39;redis.redisClusterConfiguration.maxRedirects&#39;]}" />
                </bean>
            </constructor-arg>

            <constructor-arg index="1">
                <bean class="redis.clients.jedis.JedisPoolConfig">
                    <property name="maxIdle" value="#{redis[&#39;redis.jedisPoolConfig.MaxIdle&#39;]}" />
                    <property name="testOnBorrow" value="#{redis[&#39;redis.jedisPoolConfig.testOnBorrow&#39;]}" />
                    <property name="testOnReturn" value="#{redis[&#39;redis.jedisPoolConfig.testOnReturn&#39;]}" />                    <!-- 新版jedis 不支持这个参数了 -->
                    <!-- <property name="maxWait" value="#{redis[&#39;redis.jedisPoolConfig.MaxWait&#39;]}" /> -->
                    <!-- <property name="maxActive" value="#{redis[&#39;redis.jedisPoolConfig.MaxActive&#39;]}" /> -->
                </bean>
            </constructor-arg>
        </bean>

    </beans>
登录后复制

4. redis-cluster.properties下载

    #############for org.springframework.data.redis.connection.RedisClusterConfiguration###################
    #只需要配置 master
    #理论上只需要配置一个节点即可,配置多个是为了防止单个节点挂掉,
    redis.redisClusterConfiguration.clusters=10.88.21.31:10000,10.88.22.25:10000,10.88.21.31:10001    #用于 redis.clients.jedis.JedisCluster.JedisCluster(Set<HostAndPort>, int, int, GenericObjectPoolConfig) 第三个参数 maxRedirections
    #默认值是5
    #一般当此值设置过大时,容易报:Too many Cluster redirections
    redis.redisClusterConfiguration.maxRedirects=3    ###########for redis.clients.jedis.JedisPoolConfig##############################

    redis.jedisPoolConfig.MaxIdle=60000    redis.jedisPoolConfig.testOnBorrow=true    redis.jedisPoolConfig.testOnReturn=true    #新版jedis 不支持这个参数了
    #redis.jedisPoolConfig.MaxActive=50
    #redis.jedisPoolConfig.MaxWait=5000
登录后复制


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)