spring boot整合redis中间件与热部署实现代码
作者:_小许_
热部署
每次写完程序后都需要重启服务器,需要大量的时间,spring boot提供了一款工具devtools
帮助实现热部署。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 可选 --> </dependency>
导入插件的以来后每次点击 ---->构建------>构建项目就可以了,相比重启要快的多。
Redis
spring boot整合redis最常用的有三个工具库Jedis,Redisson,Lettuce
。
共同点:都提供了基于 Redis 操作的 Java API,只是封装程度,具体实现稍有不同。
不同点:
Jedis是 Redis 的 Java 实现的客户端。支持基本的数据类型如:String、Hash、List、Set、Sorted Set。
特点:使用阻塞的 I/O,方法调用同步,程序流需要等到 socket 处理完 I/O 才能执行,不支持异步操作。Jedis 客户端实例不是线程安全的,需要通过连接池来使用 Jedis。
Redisson
优点点:分布式锁,分布式集合,可通过 Redis 支持延迟队列。
Lettuce
用于线程安全同步,异步和响应使用,支持集群,Sentinel,管道和编码器。
基于 Netty 框架的事件驱动的通信层,其方法调用是异步的。Lettuce 的 API 是线程安全的,所以可以操作单个 Lettuce 连接来完成各种操作。
Jedis
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.1.0</version> </dependency>
配置文件
# Redis服务器地址 spring.data.redis.host=192.168.223.128 # Redis服务器连接端口 spring.data.redis.port=6379 # Redis服务器连接密码(默认为空) spring.data.redis.password=root
注意时spring.data.redis而不是spring.redis后者已经舍弃了。
通过jedis连接redis:
import redis.clients.jedis.Jedis; public class RedisConect { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.223.128",6379); //配置连接密码 jedis.auth("root"); String csvfile = jedis.get("csvfile"); System.out.println(csvfile); jedis.close(); } }
spring boot 联合jedis连接redis:
//装配参数 import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.data.redis") @Data public class RedisConfig { private String host; private int port; private String password; } //创建jedis import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import redis.clients.jedis.Jedis; @Service public class JedisService { @Autowired RedisConfig redisConfig; public Jedis defaultJedis(){ Jedis jedis = new Jedis(redisConfig.getHost(),redisConfig.getPort()); jedis.auth(redisConfig.getPassword()); return jedis; } } //测试 @Test void One(){ jedisService.defaultJedis().set("one","word"); String one = jedisService.defaultJedis().get("one"); System.out.println(one); }
RedisTemplate
装配参数除了上面@ConfigurationProperties
的方法还有PropertySource
方法:
@Configuration @PropertySource("classpath:redis.properties") public class RedisConfig { @Value("${redis.hostName}") private String hostName; @Value("${redis.password}") private String password; @Value("${redis.port}") }
RedisTemplate是spring自带模板,需要配置一些参数:
package com.example.JedsFactory; import com.example.RedisConfig.RedisConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Component; @Configuration @PropertySource("classpath:redis.properties") public class JedisFactory { @Value("${spring.data.redis.host}") private String host; @Value("${spring.data.redis.password}") private String password; @Value("${spring.data.redis.port}") private Integer port; @Bean public JedisConnectionFactory JedisConnectionFactory(){ RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration (); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setPort(port); redisStandaloneConfiguration.setPassword(password); JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder(); JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build()); return factory; } @Bean public RedisTemplate makeRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }
//redis.properties # Redis服务器地址 spring.data.redis.host=192.168.223.128 # Redis服务器连接端口 spring.data.redis.port=6379 # Redis服务器连接密码(默认为空) spring.data.redis.password=root
测试:
@Test void two(){ redisTemplate.opsForValue().set("two","hello"); String two =(String) redisTemplate.opsForValue().get("two"); System.out.println(two); }
Caused by: java.lang.NoClassDefFoundError: redis/clients/util/Pool
如果报错了,如标题的错误说明jedis版本高了,有冲突,降低jedis版本即可。
jedis从3.0.1版本降低到2.9.1版本。
Caused by: java.lang.NumberFormatException: For input string: “port”
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "port" at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:79) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1339) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.24.jar:5.3.24] ... 88 common frames omitted Caused by: java.lang.NumberFormatException: For input string: "port" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_181] at java.lang.Integer.parseInt(Integer.java:580) ~[na:1.8.0_181] at java.lang.Integer.valueOf(Integer.java:766) ~[na:1.8.0_181] at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:211) ~[spring-core-5.3.24.jar:5.3.24] at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:115) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:429) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:402) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:155) ~[spring-beans-5.3.24.jar:5.3.24]
连接redis时出现这个错误原因是:
port属性不能用int接收,改为Integer。
Caused by: java.lang.NumberFormatException: For input string: “port“
到此这篇关于spring boot整合redis中间件与热部署实现的文章就介绍到这了,更多相关spring boot整合redis中间件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!