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. vue实现输入框的模糊查询(节流函数的应用场景)

    上一篇讲到了javascript的节流函数和防抖函数,那么我们在实际场合中该如何运用呢? 首先,我们来理解一下:节流函数首先是节流,就是节约流量.内存的损耗,旨在提升性能,在高频率频发的事件中才会用到 ...

  2. ASP.NET Core中使用Csp标头对抗Xss攻击

    内容安全策略(CSP)是一个增加的安全层,可帮助检测和缓解某些类型的攻击,包括跨站点脚本(XSS)和数据注入攻击.这些攻击用于从数据窃取到站点破坏或恶意软件分发的所有内容(深入CSP) 简而言之,CS ...

  3. nginx-springboot-vue前后端分离跨域配置

    nginx-springboot-vue前后端分离跨域配置 引言 接着上篇--简单的springboot-vue前后端分离登录Session拦截的demo,其中跨域是通过springboot后端全局设 ...

  4. Contour 学习笔记(二):使用级联功能实现蓝绿部署和金丝雀发布

    上篇文章介绍了 Contour 分布式架构的工作原理,顺便简单介绍了下 IngressRoute 的使用方式.本文将探讨 IngressRoute 更高级的用法,其中级联功能是重点. 1. Ingre ...

  5. [Environment setup]python+selenium+Eclipse+pydev环境搭建

    转载自: http://www.cnblogs.com/Bonker/p/3584707.html http://www.cnblogs.com/fnng/archive/2013/05/29/310 ...

  6. lightoj 1125 - Divisible Group Sums (dp)

    Given a list of N numbers you will be allowed to choose any M of them. So you can choose in NCM ways ...

  7. poj3666 Making the Grade(基础dp + 离散化)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  8. 牛客网 湖南大学2018年第十四届程序设计竞赛重现赛 A game

    链接:https://www.nowcoder.com/acm/contest/125/A来源:牛客网 Tony and Macle are good friends. One day they jo ...

  9. Kubernetes集群部署核心步骤

    目录 前言 一.所有节点安装docker 二.所有节点安装kubeadm 三.安装master节点 四.部署网络插件 五.安装node节点 六.运行一个demo 前言 这里使用环境:Ubuntu 18 ...

  10. Linux之acl库的安装与使用(限制Linux某用户的访问权限)

    acl库 作用:限制Linux某用户的访问权限 acl库的安装 首先github中下载acl代码: git clone https://github.com/acl-dev/acl 进入acl, 执行 ...