zoukankan      html  css  js  c++  java
  • SpringBoot整合Redis集群

    一、环境搭建

      Redis集群环境搭建:https://www.cnblogs.com/zwcry/p/9174233.html

    二、Spring整合Redis集群

    1.pom.xml

    <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>
          <groupId>szw</groupId>
          <artifactId>springboot_redis_sentinel</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <name>springboot_redis_sentinel</name>
          <description>springboot整合redis哨兵</description>
      
          <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <start-class>com.sze.redis.SzwRedisApplication</start-class>
        </properties>
      
          <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.2.RELEASE</version>
            <relativePath></relativePath>
        </parent>
        
        <dependencies>
            <!-- 使用web启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- 使用aop模板启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <!-- 测试 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- redis缓存 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.49</version>
            </dependency>
        </dependencies>
        
        <repositories>
            <repository>
                <id>nexus-aliyun</id>
                <name>Nexus aliyun</name>
                <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>nexus-aliyun</id>
                <name>Nexus aliyun</name>
                <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
        
        <build>  
            <plugins> 
                <!-- 要将源码放上去,需要加入这个插件 -->  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-source-plugin</artifactId>  
                    <configuration>  
                        <attach>true</attach>  
                    </configuration>  
                    <executions>  
                        <execution>  
                            <phase>compile</phase>  
                            <goals>  
                                <goal>jar</goal>  
                            </goals>  
                        </execution>  
                    </executions>  
                </plugin>
                <!-- 打包 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>  
            </plugins>  
        </build>
    </project>

     

    2.application.properties

    ##单服务器
    spring.redis.cluster.nodes=192.168.159.129:7001,192.168.159.129:7002,192.168.159.129:7003,192.168.159.129:7004,192.168.159.129:7005,192.168.159.129:7006
    ## 连接池最大连接数(使用负值表示没有限制) 
    spring.redis.pool.max-active=300
    ## Redis数据库索引(默认为0) 
    spring.redis.database=0
    ## 连接池最大阻塞等待时间(使用负值表示没有限制) 
    spring.redis.pool.max-wait=-1
    ## 连接池中的最大空闲连接 
    spring.redis.pool.max-idle=100
    ## 连接池中的最小空闲连接 
    spring.redis.pool.min-idle=20
    ## 连接超时时间(毫秒) 
    spring.redis.timeout=60000

     

    3.服务启动入口

    package com.sze.redis;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SzwRedisApplication {
        public static void main(String[] args) {
            System.setProperty("spring.devtools.restart.enabled", "false");
            SpringApplication.run(SzwRedisApplication.class, args);
        }
    }

     

    4.单元测试

    package com.sze.redis;
    
    import javax.annotation.PostConstruct;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ClusterTest {
        
        @Autowired
        StringRedisTemplate redisTemplate;
        
        ValueOperations<String, String> stringRedis;
        
        @PostConstruct
        public void init(){
            stringRedis=redisTemplate.opsForValue();
        }
        
        
        @Test
        public void testString (){
            stringRedis.set("name", "丁洁");
            System.out.println(stringRedis.get("name"));
        }
    }

     

  • 相关阅读:
    sql2005创建存储过程
    sql 2005 调用存储过程
    sql2005创建存储过程(需要注意的几点)
    c# 日期处理
    Worldwind WMS Server安装
    Ubuntu11.10 设置默认Terminal为Terminator
    WordPress中文标题无法显示的解决方法
    Unknown Source的出现及解决
    Windows下 ant配置 以及 Unable to locate tools.jar
    安装WordPress中文包四步曲
  • 原文地址:https://www.cnblogs.com/zwcry/p/9176250.html
Copyright © 2011-2022 走看看