SpringBoot整合集成redis

2021-07-02 05:03

阅读:376

标签:blog   ica   mode   build   name   sentinel   sign   dex   system   

Redis安装:https://www.cnblogs.com/zwcry/p/9505949.html

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.0modelVersion>
      groupId>szwgroupId>
      artifactId>springboot_redis_sentinelartifactId>
      version>0.0.1-SNAPSHOTversion>
      name>springboot_redis_sentinelname>
      description>springboot整合redis哨兵description>
  
      properties>
        project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        java.version>1.8java.version>
        start-class>com.sicdt.sicsign.web.SicSignWebApplicationstart-class>
    properties>
  
      parent>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-parentartifactId>
        version>1.4.2.RELEASEversion>
        relativePath>relativePath>
    parent>
    
    dependencies>
        
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-aopartifactId>
        dependency>
        
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
        dependency>
        
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-redisartifactId>
        dependency>
    dependencies>
    
    repositories>
        repository>
            id>nexus-aliyunid>
            name>Nexus aliyunname>
            url>http://maven.aliyun.com/nexus/content/groups/publicurl>
            releases>
                enabled>trueenabled>
            releases>
            snapshots>
                enabled>falseenabled>
            snapshots>
        repository>
    repositories>
    pluginRepositories>
        pluginRepository>
            id>nexus-aliyunid>
            name>Nexus aliyunname>
            url>http://maven.aliyun.com/nexus/content/groups/publicurl>
            releases>
                enabled>trueenabled>
            releases>
            snapshots>
                enabled>falseenabled>
            snapshots>
        pluginRepository>
    pluginRepositories>
    
    build>  
        plugins> 
              
            plugin>  
                groupId>org.apache.maven.pluginsgroupId>  
                artifactId>maven-source-pluginartifactId>  
                configuration>  
                    attach>trueattach>  
                configuration>  
                executions>  
                    execution>  
                        phase>compilephase>  
                        goals>  
                            goal>jargoal>  
                        goals>  
                    execution>  
                executions>  
            plugin>
            
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
                configuration>
                    fork>truefork>
                configuration>
            plugin>  
        plugins>  
    build>
project>

2.application.properties

##单服务器
spring.redis.host=192.168.159.129
##单端口
spring.redis.port=6379
## 连接池最大连接数(使用负值表示没有限制) 
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 RedisTest {
    @Autowired
    StringRedisTemplate redisTemplate;
    
    private ValueOperations stringRedis;
    
    @PostConstruct
    public void init(){
        stringRedis=redisTemplate.opsForValue();
    }
    
    @Test
    public void test() throws InterruptedException{
        stringRedis.set("name", "丁洁");
        System.out.println(stringRedis.get("name"));
    }
}

 

SpringBoot整合集成redis

标签:blog   ica   mode   build   name   sentinel   sign   dex   system   

原文地址:https://www.cnblogs.com/zwcry/p/9633184.html


评论


亲,登录后才可以留言!