系列文章目录
SpringBoot3-第一篇(快速入门)
SpringBoot3-第二篇(Web开发)
SpringBoot3-第三篇(数据访问)
SpringBoot3-第四篇(基础特性)
SpringBoot3-第五篇(核心原理)
SpringBoot3-第六篇(整合NoSQL)
文章目录
- 系列文章目录
- 1. 环境准备
- 1.1 Docker安装
- 1.2 prometheus.yml
- 1.3 docker-compose.yml
- 1.4 启动环境
- 1.5 验证
- 2. Redis整合
- 2.1 场景整合
- 2.2 自动配置原理
- 2.3 定制化
- 2.3.1 序列化机制
- 2.3.2 redis客户端
- 2.3.3 配置参考
1. 环境准备
1.1 Docker安装
sudo yum install -y yum-utilssudo yum-config-manager \--add-repo \https://download.docker.com/linux/centos/docker-ce.reposudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginsudo systemctl enable docker --now#测试工作
docker ps
# 批量安装所有软件
docker compose
创建 /prod
文件夹,准备以下文件
1.2 prometheus.yml
global:scrape_interval: 15sevaluation_interval: 15sscrape_configs:- job_name: 'prometheus'static_configs:- targets: ['localhost:9090']- job_name: 'redis'static_configs:- targets: ['redis:6379']- job_name: 'kafka'static_configs:- targets: ['kafka:9092']
1.3 docker-compose.yml
version: '3.9'services:redis:image: redis:latestcontainer_name: redisrestart: alwaysports:- "6379:6379"networks:- backendzookeeper:image: bitnami/zookeeper:latestcontainer_name: zookeeperrestart: alwaysenvironment:ZOOKEEPER_CLIENT_PORT: 2181ZOOKEEPER_TICK_TIME: 2000networks:- backendkafka:image: bitnami/kafka:3.4.0container_name: kafkarestart: alwaysdepends_on:- zookeeperports:- "9092:9092"environment:ALLOW_PLAINTEXT_LISTENER: yesKAFKA_CFG_ZOOKEEPER_CONNECT: zookeeper:2181KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1networks:- backendkafka-ui:image: provectuslabs/kafka-ui:latestcontainer_name: kafka-uirestart: alwaysdepends_on:- kafkaports:- "8080:8080"environment:KAFKA_CLUSTERS_0_NAME: devKAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092networks:- backendprometheus:image: prom/prometheus:latestcontainer_name: prometheusrestart: alwaysvolumes:- ./prometheus.yml:/etc/prometheus/prometheus.ymlports:- "9090:9090"networks:- backendgrafana:image: grafana/grafana:latestcontainer_name: grafanarestart: alwaysdepends_on:- prometheusports:- "3000:3000"networks:- backendnetworks:backend:name: backend
1.4 启动环境
docker compose -f docker-compose.yml up -d
1.5 验证
- Redis:你的ip:6379
- 填写表单,下载官方可视化工具:
- https://redis.com/redis-enterprise/redis-insight/#insight-form
- Kafka:你的ip:9092
- idea安装大数据插件
- Prometheus:你的ip:9090
- 直接浏览器访问
- Grafana:你的ip:3000
- 直接浏览器访问
2. Redis整合
2.1 场景整合
依赖导入
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置
spring.data.redis.host=192.168.200.100
spring.data.redis.password=123456
测试
@Autowired
StringRedisTemplate redisTemplate;@Test
void redisTest(){redisTemplate.opsForValue().set("a","1234");Assertions.assertEquals("1234",redisTemplate.opsForValue().get("a"));
}
2.2 自动配置原理
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
中导入了RedisAutoConfiguration
、RedisReactiveAutoConfiguration
和RedisRepositoriesAutoConfiguration
。所有属性绑定在RedisProperties
中RedisReactiveAutoConfiguration
属于响应式编程,不用管。RedisRepositoriesAutoConfiguration
属于 JPA 操作,也不用管RedisAutoConfiguration
配置了以下组件LettuceConnectionConfiguration
: 给容器中注入了连接工厂LettuceConnectionFactory
,和操作 redis 的客户端DefaultClientResources
。RedisTemplate<Object, Object>
: 可给 redis 中存储任意对象,会使用 jdk 默认序列化方式。StringRedisTemplate
: 给 redis 中存储字符串,如果要存对象,需要开发人员自己进行序列化。key-value都是字符串进行操作··
2.3 定制化
2.3.1 序列化机制
@Configuration
public class AppRedisConfiguration {/*** 允许Object类型的key-value,都可以被转为json进行存储。* @param redisConnectionFactory 自动配置好了连接工厂* @return*/@Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);//把对象转为json字符串的序列化工具template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());return template;}
}
2.3.2 redis客户端
RedisTemplate、StringRedisTemplate: 操作redis的工具类
- 要从redis的连接工厂获取链接才能操作redis
- Redis客户端
- Lettuce: 默认
- Jedis:可以使用以下切换
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><exclusion><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></exclusion></exclusions></dependency><!-- 切换 jedis 作为操作redis的底层客户端--><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>
2.3.3 配置参考
spring.data.redis.host=8.130.74.183
spring.data.redis.port=6379
#spring.data.redis.client-type=lettuce#设置lettuce的底层参数
#spring.data.redis.lettuce.pool.enabled=true
#spring.data.redis.lettuce.pool.max-active=8spring.data.redis.client-type=jedis
spring.data.redis.jedis.pool.enabled=true
spring.data.redis.jedis.pool.max-active=8