SpringBoot整合集成redis

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.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.sicdt.sicsign.web.SicSignWebApplication</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>
    </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.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<String, String> stringRedis;

    @PostConstruct
    public void init(){
        stringRedis=redisTemplate.opsForValue();
    }

    @Test
    public void test() throws InterruptedException{
        stringRedis.set("name", "丁洁");
        System.out.println(stringRedis.get("name"));
    }
}

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

时间: 2024-08-02 14:45:22

SpringBoot整合集成redis的相关文章

springboot整合mybatis,redis,代码(二)

一 说明: springboot整合mybatis,redis,代码(一) 这个开发代码的复制粘贴,可以让一些初学者直接拿过去使用,且没有什么bug 二 对上篇的说明 可以查看上图中文件: 整个工程包括配置,对对应上文的配置 原文地址:https://www.cnblogs.com/xiufengchen/p/10327501.html

springboot整合mybatis,redis,代码(一)

一 搭建项目,代码工程结构 使用idea或者sts构建springboot项目 二  数据库sql语句 SQLyog Ultimate v12.08 (64 bit) MySQL - 5.7.14-log ********************************************************************* */ /*!40101 SET NAMES utf8 */; create table `person` ( `id` int (11), `name`

springboot整合mybatis,redis,代码(四)

一 说明 这是spring整合redis注解开发的系类: 二 正文 在注解开发时候,会有这几个注解需要注意: 具体含义: [email protected] 可以标记在方法上,也可以标记在类上.当标记在方法上时表示该方法是支持缓存的,当标记在类上时则表示该类所有的方法都是支持缓存的.应用到读取数据的方法上,将先从缓存中读取该方法的返回值,如果没有再从DB获取数据,然后把数据添加到缓存中 2 @CacheEvcit  应用到删除数据的方法上,调用方法时会从缓存中删除对应key的数据 3 @Cach

swagger+springBoot整合集成(三步看明白)

swagger是什么? swagger是一个后台人员专门测试的一个简单工具,特别适合做前后端分离的项目,之前我们一直都是用的postman,但是这个有点复杂,路径需要自己,而swagger则直接使用,接下来我们就直接上代码说一说swagger,文采不好往见谅! 1.首先需要两个jar包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifac

springboot整合mybatis,redis,代码(五)

redis注解开发过程中包含许多注解 [email protected] 可以标记在方法上,也可以标记在类上.当标记在方法上时表示该方法是支持缓存的,当标记在类上时则表示该类所有的方法都是支持缓存的.应用到读取数据的方法上,将先从缓存中读取该方法的返回值,如果没 有再从DB获取数据,然后把数据添加到缓存中 缓存是以键值对进行的,值就是方法的返回结果,至于键的话,Spring又支持两种策略,默认策略和自定义策略,需要注意的是当一个支持缓存的方法在对象内部被调用时是不会触发缓存功能的. [email

springboot整合mybatis,redis,代码(三)

一 说明 接着上篇讲述redis缓存配置的用法: 二 正文 首先要使用缓存就必须要开开启缓存,第二步是需要开redis-server 下载redis包之后,点击图中两个都可以开启redis 怎么看是否启动呢,见下图: 在application中加入连接地址,和相关策略: 原文地址:https://www.cnblogs.com/xiufengchen/p/10327515.html

SpringBoot整合Shiro 集成Redis缓存(六)

简介:由于考虑到项目后期分布式部署,所以缓存由ehcache改为redis,而redis既有单机版部署,也有分布式部署,所以二者需要兼容. 1. maven依赖 <dependency> <groupId>org.crazycake</groupId> <artifactId>shiro-redis</artifactId> <version>3.1.0</version> </dependency> 2. 设

SpringBoot集成Redis来实现缓存技术方案

概述 在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Redis简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件,Redis 的优势包括它的速度.支持丰富的数据类型.操作原子性,以及它的通用性. 案例整合 本案例是在之前一篇SpringBoot + Mybatis + RESTful的基础上来集

springboot集成redis详解

欢迎扫码加入Java高知群交流 springboot集成redis非常简单 1.引入maven依赖redis包 <!-- springboot整合 redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> 2.appli