1、redis服务搭建

centos7 搭建redis服务

2、接入相关

  pom文件依赖引入

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
</dependencies>

其中使用jedis来开发,jedis集成了redis的一些命令操作,封装了redis的java客户端。提供了连接池管理等。

  application配置文件

# Redis
spring.redis:
host: localhost
port: 6379
password: root
timeout: 1000
jedis.pool:
#jedis最大分配对象
maxTotal: 1024
#jedis最大保存idel状态对象数
maxIdle: 200
#jedis池没有对象返回时,最大等待时间
maxWaitMillis: 1000
testOnBorrow: true
testOnReturn: true
blockWhenExhausted: false

  jedis配置类

@Configuration
@Data
public class JedisConfig { private Logger logger = LoggerFactory.getLogger(JedisConfig.class); @Bean(name = "jedis.pool")
@Autowired
public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
@Value("${spring.redis.host}") String host,
@Value("${spring.redis.port}") int port,
@Value("${spring.redis.timeout}") int timeout,
@Value("${spring.redis.password}") String password) {
logger.info("缓存服务器的地址:" + host + ":" + port);
return new JedisPool(config, host, port, Protocol.DEFAULT_TIMEOUT, password);
} @Bean(name = "jedis.pool.config")
public JedisPoolConfig jedisPoolConfig(@Value("${spring.redis.jedis.pool.maxTotal}") int maxTotal,
@Value("${spring.redis.jedis.pool.maxIdle}") int maxIdle,
@Value("${spring.redis.jedis.pool.maxWaitMillis}") int maxWaitMillis,
@Value("${spring.redis.jedis.pool.testOnBorrow}") boolean testOnBorrow,
@Value("${spring.redis.jedis.pool.testOnReturn}") boolean testOnReturn) { JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnBorrow);
return config;
} }

此处使用注解@Value来读取配置参数,也可以使用springboot的ConfigurationProperties注解来将配置文件转换成java对象使用,见springboot解析配置文件

  RedisClient类

@Component
public class RedisClient { private static final Logger logger = LoggerFactory.getLogger(RedisClient.class); @Autowired
private JedisPool jedisPool; public Jedis getJedis() {
return jedisPool.getResource();
} /**
* 写入缓存
*
* @param key
* @param value
* @return Boolean
*/
public String set(final String key, String value) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.set(key, String.valueOf(value));
} catch (Exception e) {
logger.error("[RedisClient] set e,", e);
return "";
} finally {
close(jedis);
}
} /**
* 读取缓存
*
* @param key
* @return
*/
public Optional<String> get(final String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return Optional.ofNullable(jedis.get(key));
} catch (Exception e) {
logger.error("[RedisClient] get exception,", e);
return Optional.empty();
} finally {
close(jedis);
}
} }

  测试:

    @Autowired
private RedisClient redisClient; @Test
public void redis() {
System.out.println(redisConfig); redisClient.set("hello", "hello, redis"); System.out.println(redisClient.get("hello")); }

参照原码:Github

springboot 集成Redis单机的更多相关文章

  1. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  2. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  3. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  4. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

  5. springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3. springboot+shiro+redis(集群re ...

  6. springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败

    提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...

  7. springboot+shiro+redis(单机redis版)整合教程

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+shiro+redis(单机red ...

  8. SpringBoot | 集成Redis

    Windows下安装: https://github.com/MicrosoftArchive/redis/releases zip下就解包到自定义目录下,msi就跟着步骤安装 进入安装目录下运行命令 ...

  9. springboot 集成Redis一主二从三哨兵

    1.Centos7 Redis一主二从三哨兵配置 Redis一主二从三哨兵环境搭建 2.接入过程 与集成redis单机不同的是jedis相关的配置做了修改,JedisPool换成了JedisSenti ...

随机推荐

  1. Leetcode之回溯法专题-46. 全排列(Permutations)

    Leetcode之回溯法专题-46. 全排列(Permutations) 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3, ...

  2. 转载-lambda sort

    原文:https://blog.csdn.net/qq_27127145/article/details/83930498 版权声明:本文为博主原创文章,转载请附上博文链接! import com.g ...

  3. 成功项目管理与PMP认证2017

    http://study.163.com/course/courseLearn.htm?courseId=1064005#/learn/video?lessonId=1003778171&co ...

  4. P2762 太空飞行计划问题 最大权闭合子图

    link:https://www.luogu.org/problemnew/show/P2762 题意 承担实验赚钱,但是要花去对应仪器的费用,仪器可能共用.求最大的收益和对应的选择方案. 思路 这道 ...

  5. lightoj 1028 - Trailing Zeroes (I)(素数筛)

    We know what a base of a number is and what the properties are. For example, we use decimal number s ...

  6. ui、li模拟下拉框

    转载:原文地址:https://www.jianshu.com/p/e303e0298e9e 效果图: HTML: <div class="rank_top"> < ...

  7. java1.8新特性(一)接口的默认方法

    一 简介 我们通常所说的接口的作用是用于定义一套标准.约束.规范等,接口中的方法只声明方法的签名,不提供相应的方法体,方法体由对应的实现类去实现. 在JDK1.8中打破了这样的认识,接口中的方法可以有 ...

  8. Springboot2.x 自动创建表并且执行初始化数据

    1.使用springboot jdbc初始化数据库 项目结构 schema.sql drop table if exists user; create table user (id bigint(20 ...

  9. 2019本科se第一次作业-博客初体验-chris

    (1)第一章  计算机专业术语总结: 软件=程序+软件工程.程序=数据结构+算法.软件.程序.用户.需求.应用程序.软件服务.源程序.软件架构(Software Architecture).软件设计与 ...

  10. Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByExample问题解决

    最近在做一个关于ssm框架整合的项目,但是今天正合完后出现了问题: Invalid bound statement (not found): com.taotao.mapper.TbItemMappe ...