前言

  此处已经省略redis的安装,请自行百度查找redis的服务端安装过程。

1.pom文件配置:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.aaron</groupId>
<artifactId>framework</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<name>framework Maven Webapp</name>
<url>http://maven.apache.org</url> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath />
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- redis客户端lettuce -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.0.4.RELEASE</version>
</dependency> <!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.2.RELEASE</version>
</dependency> <!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency> </dependencies> <build>
<finalName>framework</finalName> <plugins>
<!-- Spring Boot包含一个Maven插件 ,可以将项目打包为可执行的jar。 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 没有该配置,devtools 不生效 -->
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>

2.新增redis配置类:

package com.aaron.framework.common.configuration;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set; import javax.annotation.Resource; import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.transaction.annotation.EnableTransactionManagement; import com.fasterxml.jackson.databind.ObjectMapper; @Configuration
@EnableTransactionManagement//事务支持
public class ReidsConfig { @Resource
private LettuceConnectionFactory lettuceConnectionFactory; @Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator(){
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuffer sb = new StringBuffer();
sb.append(target.getClass().getName());
sb.append(method.getName());
for(Object obj:params){
sb.append(obj.toString());
}
return sb.toString();
}
};
}
//缓存管理器
@Bean
public CacheManager cacheManager() {
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
.RedisCacheManagerBuilder
.fromConnectionFactory(lettuceConnectionFactory);
Set<String> cacheNames = new HashSet<String>() {{
add("codeNameCache");
}};
builder.initialCacheNames(cacheNames);
return builder.build();
} /**
* RedisTemplate配置
*
* @param jedisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory ) {
//设置序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);//key序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);//value序列化
redisTemplate.setHashKeySerializer(stringSerializer);//Hash key序列化
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);//Hash value序列化
redisTemplate.afterPropertiesSet();
return redisTemplate;
} /**
* redis连接工厂
* @return
*/
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379));
} // /**
// * redis响应式连接工厂
// * @return
// */
// @Bean
// public ReactiveRedisConnectionFactory connectionFactory() {
// return new LettuceConnectionFactory("localhost", 6379);
// } // /**
// * Lettuce
// * 高可用redis连接工厂(容灾)
// */
// @Bean
// public RedisConnectionFactory lettuceConnectionFactory() {
// RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
// .master("mymaster")
// .sentinel("127.0.0.1", 26379)
// .sentinel("127.0.0.1", 26380);
// return new LettuceConnectionFactory(sentinelConfig);
// }
}

3.编写服务类和接口:

package com.aaron.framework.common.service;

public interface IRedisService {

    /**
* 设置redis值
* @param key
* @param value
*/
void setValue(String key,Object value); /**
* 设置redis值
* @param key
* @param value
*/
void setValue(String key,Object value,long timeout); /**
* 从redis获取值
* @param key
* @return
*/
Object getValue(String key); }
package com.aaron.framework.common.service.impl;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service; import com.aaron.framework.common.service.IRedisService; @Service("redisService")
public class RedisService implements IRedisService{ @Resource
private RedisTemplate<String, Object> redisTemplate; public void setValue(String key, Object value) {
ValueOperations<String, Object> vo = redisTemplate.opsForValue();
vo.set(key, value);
} @Override
public void setValue(String key, Object value, long timeout) {
ValueOperations<String, Object> vo = redisTemplate.opsForValue();
vo.set(key, value, timeout, TimeUnit.MILLISECONDS);
} public Object getValue(String key) {
ValueOperations<String, Object> vo = redisTemplate.opsForValue();
return vo.get(key);
} }

spring boot2.0(二 ) lettcute访问redis的更多相关文章

  1. Spring Boot2.0使用Spring Security

     一.Spring Secutity简介     Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性 ...

  2. spring boot 2.0(一)权威发布spring boot2.0

    Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...

  3. 【spring cloud】spring cloud2.X spring boot2.0.4调用feign配置Hystrix Dashboard 和 集成Turbine 【解决:Hystrix仪表盘Unable to connect to Command Metric Stream】【解决:Hystrix仪表盘Loading...】

    环境: <java.version>1.8</java.version><spring-boot.version>2.0.4.RELEASE</spring- ...

  4. Spring Boot2.0自定义配置文件使用

    声明: spring boot 1.5 以后,ConfigurationProperties取消locations属性,因此采用PropertySource注解配合使用 根据Spring Boot2. ...

  5. 【spring colud】spring cloud微服务项目搭建【spring boot2.0】

    spring cloud微服务项目搭建 =================================== 示例版本: 1.spring boot 2.0版本 2.开发工具 IntellJ IDE ...

  6. Spring Boot2.0之 监控管理

    Spring boot监控中心: 针对微服务的服务状态,服务器的内存变化(内存.线程.日志管理等)检测服务配置连接地址是否有用(有些懒加载的情况下,用的时候发现卧槽不能用)模拟访问,懒加载.统计有多少 ...

  7. 基于spring boot2.0+spring security +oauth2.0+ jwt微服务架构

    github地址:https://github.com/hankuikuide/microservice-spring-security-oauth2 项目介绍 该项目是一个演示项目,主要演示了,基于 ...

  8. Spring Boot2.0 设置拦截器

    所有功能完成 配置登录认证 配置拦截器 在spring boot2.0 之后 通过继承这个WebMvcConfigurer类 就可以完成拦截 新建包com.example.interceptor; 创 ...

  9. Spring Boot2.0 静态资源被拦截问题

    在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...

随机推荐

  1. idea 2018.1 for mac破解激活方法---到2099

    https://blog.csdn.net/weixin_41470864/article/details/81607169

  2. 再谈HTTP2性能提升之背后原理—HTTP2历史解剖

    即使千辛万苦,还是把网站升级到http2了,遇坑如<phpcms v9站http升级到https加http2遇到到坑>. 因为理论相比于 HTTP 1.x ,在同时兼容 HTTP/1.1 ...

  3. Python之socket_udp

    UDP服务端&客户端编程 ''' udp编程 创建socket对象,socket.SOCK_DGRAM 绑定ip和port,bind()方法 传输数据 1.接收数据,socket.recvfr ...

  4. 在Ubuntu16.04中python环境下实现tab键补全

    1.编写tab.py的代码: 1 #!/usr/bin/env python 2 # python startup file 3 import sys 4 import readline 5 impo ...

  5. JQuery 中$("input:eq(0)") eq 的意思

    :eq(index)匹配一个给定索引值的元素 ----------------------------------------------------- Matches a single elemen ...

  6. 2018-2019-2 20165215《网络对抗技术》Exp4 恶意代码分析

    目录 实践目标 实践内容 基础问题回答 实验步骤 使用schtasks指令监控系统 使用sysmon工具监控系统 使用VirusTotal分析恶意软件 使用PEiD进行外壳检测 使用PE explor ...

  7. Oracle生成GUID

    ,),),),),) GUID FROM ( SELECT SYS_GUID() GUID FROM DUAL )

  8. springboot使用@data注解,减少不必要代码

    一.idea安装lombok插件 二.重启idea 三.添加maven依赖 <dependency> <groupId>org.projectlombok</groupI ...

  9. 作业(更新ing)

    暴政警告 2019.4.19 1.YBT完成 7 道题目(1)广搜 3 道 (2)最短路 4 道 2.写至少 2 篇博客 (据说集中精力,一个上午/下午就能写完) 1.   一个 又一个 球细胞数目 ...

  10. leetcode 最后一个单词的长度 python

    class Solution: def lengthOfLastWord(self, s): """ :type s: str :rtype: int "&qu ...