一,正常写一个新增接口,便与测试是否连接成功
二,配置类中填写redis和swagger的配置
package com.igeek.boot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @description T0D0
* auther lizhifeng
* 2024/7/11 18:22
*
* * 其实SpringBoot自动帮我们在容器中生成了一个RedisTemplate和一个StringRedisTemplate。
* * 但是,这个RedisTemplate的泛型是<Object,Object>,写代码不方便,需要写好多类型转换的代码;
* * 我们需要一个泛型为<String,Object>形式的RedisTemplate
* * 同时,设置key-value的序列化方式
*/
@Configuration(proxyBeanMethods = false)
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // 键
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); // 值
//1.设置序列化 针对String字符串类型、Hash类型 的键、值 分别进行序列化
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
//2.设置Redis连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
//3.干涉的初始化操作
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
package com.igeek.boot.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @description T0D0
* auther lizhifeng
* 2024/7/11 18:22
*
* * SpringBoot整合Swagger2
* * 1.引入springfox-swagger2 、 springfox-swagger-ui依赖
* * 2.新建Swagger2Config配置文件 @Bean Docket
* * 3.在application.properties文件上,spring.mvc.pathmatch.matching-strategy=ant_path_matcher
* * 4.在entity类、controller类添加相关注解
* * 5.访问 http://localhost:8080/swagger-ui.html 访问Swagger2的页面
* * 6.整合Knife4j ,引入场景启动器 , 开启@EnableKnife4j
* * 7.访问 http://localhost:8080/doc.html 访问Knife4j的页面
*/
// 开启API接口文档
@EnableSwagger2
@EnableKnife4j
@Configuration(proxyBeanMethods = false)
public class Swagger2Config {
@Value("${spring.swagger2.enabled}")
private boolean enabled;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(enabled)
.select()
.apis(RequestHandlerSelectors.basePackage("com.igeek.boot"))
.paths(PathSelectors.any())
.build();
}
//Swagger文档信息
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot整合swagger2构建API接口文档 "+"\t"+ DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now()))
.description("有问题给管理员邮件:3168850469@qq.com")
.version("1.0")
.termsOfServiceUrl("https://www.igeekhome.com/")
.build();
}
}
三,编辑application.properties文件
#------------- Redis 单机 ----------
spring.redis.host=8.130.125.162
spring.redis.port=6333
spring.redis.password=123456
spring.redis.database=2
spring.redis.timeout=18000
#不建议切换客户端
#spring.redis.client-type=jedis
#-------- Redis Lettuce 连接池 ----------
spring.redis.lettuce.pool.enabled=true
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.min-idle=3
spring.redis.lettuce.pool.max-wait=-1
#------------- Redis 集群 ----------
#------------- Swagger2 ----------
spring.swagger2.enabled=true
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
#------------- 日志 ----------
logging.level.root=info
logging.level.com.igeek.boot.controller=debug
logging.file.name=./boot/boot_4_data_redis.log
logging.charset.file=UTF-8
四,运行并用swagger测试接口
五,打开FinalShell,并输入相关指令
[root@iZ0jle53v75ggexh3pl5q6Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d98b95c334d8 redis:6 "docker-entrypoint.s…" 33 hours ago Up 33 hours 0.0.0.0:6333->6379/tcp, :::6333->6379/tcp redis6
[root@iZ0jle53v75ggexh3pl5q6Z ~]# docker exec -it d98b95c334d8 /bin/bash
root@d98b95c334d8:/data# redis-cli -p 6379 --raw
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> keys *
oid::39
127.0.0.1:6379[2]> get oid::39
{"@class":"com.igeek.boot.entity.Orders","oid":39,"serial":"流水号:07d38928-4ca6-4475-9895-4129b5092ddd"}
六,打开redis,查看相关数据库