前言

  此处已经省略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. WEB服务器,TOMCAT和servlet之间的关系

    WEB服务器,TOMCAT和servlet之间的关系 什么是WEB服务器Web服务器是指能够为发出请求的浏览器提供文档的程序.服务器是 一种被动程序,只有浏览器发出请求的时候才会响应.应用层使用 的是 ...

  2. jupyter notebook + frp 实现内容穿透

    服务器上找到frps.ini 配置如下 [common] bind_port = 7000 vhost_http_port = 8890 要穿透的笔记本的frpc.ini配置 [common] ser ...

  3. 论文速读(Jiaming Liu——【2019】Detecting Text in the Wild with Deep Character Embedding Network )

    Jiaming Liu--[2019]Detecting Text in the Wild with Deep Character Embedding Network 论文 Jiaming Liu-- ...

  4. Git使用和Vue项目

    1.创建git排除文件,.gitignore 2.READEME.md 和 LICENSE开源协议 git init  创建仓库 , git status 查看文件状态 红色文件表示未提交. git ...

  5. LINUX 编程定位工具gstack,pstack

    pstack: pstack命令可显示每个进程的栈跟踪. pstack 命令必须由相应进程的属主或 root 运行. 可以使用 pstack 来确定进程挂起的位置. 此命令允许使用的唯一选项是要检查的 ...

  6. Android的JSON数据解析

    一. 使用原生方式解析 准备工作:准备一个布局文件,用来显示元数据与转换之后的数据 <?xml version="1.0" encoding="utf-8" ...

  7. 安卓GridView奇偶行不同颜色

    背景:安卓制作表格,两列多行,奇数行和偶数行背景色不同 分析:GridView是经常用来制作表格的,但是和ListView不同,不能简单的用position % 2 == 0/1 来判断奇偶行,下面提 ...

  8. python 字典嵌套字典赋值异常

    针对dict中 嵌套dict 出现复制异常 lists={} test=['s1','s2','s3'] data = {'value': '',} for i in range(2): lists[ ...

  9. Tomcat笔记 #01# WEB应用管理工具简介

    索引 查看JVM以及SERVLET/接口的情况 动态管理WEB应用 Tomcat自带了一个基于网页的web应用管理工具,可以帮助我们监控&管理部署上去的WEB APP,特别方便!恰好之前碰到的 ...

  10. Docker volume权限导致的几个问题

    挂宿主目录的权限问题 由于容器和宿主机共用了一套内核,因此同一个uid对应的容器用户和宿主机用户(哪怕用户名不同)对于内核权限控制而言都是同一个用户.而默认情况下,如果未做特殊配置,容器里的进程默认是 ...