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之分治法专题-169. 求众数(Majority Element)

    Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...

  2. IT项目管理入门知识

  3. 混合图欧拉回路POJ1637Sightseeing tour

    http://www.cnblogs.com/looker_acm/archive/2010/08/15/1799919.html /* ** 混合图欧拉回路 ** 只记录各定点的出度与入度之差,有向 ...

  4. 牛客OI测试赛 F 子序列 组合数学 欧拉降幂公式模板

    链接:https://www.nowcoder.com/acm/contest/181/F来源:牛客网 题目描述 给出一个长度为n的序列,你需要计算出所有长度为k的子序列中,除最大最小数之外所有数的乘 ...

  5. java中多线程执行时,为何调用的是start()方法而不是run()方法

    Thead类中start()方法和run()方法的区别 1,start()用来启动一个线程,当调用start()方法时,系统才会开启一个线程,通过Thead类中start()方法来启动的线程处于就绪状 ...

  6. linux下创建git代码

    1.创建一个新的repository: 先在github上创建并写好相关名字,描述. $cd ~/hello-world        //到hello-world目录 $git init       ...

  7. 字符串和Date互相转化

    话不多说,上程序,如图: public class TestDate { public static void main(String[] args) throws ParseException { ...

  8. 解决SSM框架中,存储到mysql数据库中乱码问题的究极方案

    因为编码格式不匹配的问题,设置了好多遍,都不行,就试着让所有的编码格式保持一致.然后读取.插入数据库再也不乱码了. 数据库编码格式必须和myeclipse编码格式一致 其次依次让以下各文件的编码格式保 ...

  9. Winform中设置ZedGraph曲线图的字体样式是避免出现边框

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  10. jmeter 查看结果树数据分析 优化

    1.点击查看结果树,配置 2.筛选功能项