spring?boot整合redis中間件與熱部署實(shí)現(xiàn)代碼
熱部署
每次寫完程序后都需要重啟服務(wù)器,需要大量的時間,spring boot提供了一款工具devtools幫助實(shí)現(xiàn)熱部署。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 可選 -->
</dependency>
導(dǎo)入插件的以來后每次點(diǎn)擊 ---->構(gòu)建------>構(gòu)建項目就可以了,相比重啟要快的多。
Redis
spring boot整合redis最常用的有三個工具庫Jedis,Redisson,Lettuce。
共同點(diǎn):都提供了基于 Redis 操作的 Java API,只是封裝程度,具體實(shí)現(xiàn)稍有不同。
不同點(diǎn):
Jedis是 Redis 的 Java 實(shí)現(xiàn)的客戶端。支持基本的數(shù)據(jù)類型如:String、Hash、List、Set、Sorted Set。
特點(diǎn):使用阻塞的 I/O,方法調(diào)用同步,程序流需要等到 socket 處理完 I/O 才能執(zhí)行,不支持異步操作。Jedis 客戶端實(shí)例不是線程安全的,需要通過連接池來使用 Jedis。
Redisson
優(yōu)點(diǎn)點(diǎn):分布式鎖,分布式集合,可通過 Redis 支持延遲隊列。
Lettuce
用于線程安全同步,異步和響應(yīng)使用,支持集群,Sentinel,管道和編碼器。
基于 Netty 框架的事件驅(qū)動的通信層,其方法調(diào)用是異步的。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服務(wù)器地址 spring.data.redis.host=192.168.223.128 # Redis服務(wù)器連接端口 spring.data.redis.port=6379 # Redis服務(wù)器連接密碼(默認(rèn)為空) spring.data.redis.password=root
注意時spring.data.redis而不是spring.redis后者已經(jīng)舍棄了。
通過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 聯(lián)合jedis連接redis:
//裝配參數(shù)
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;
}
//創(chuàng)建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
裝配參數(shù)除了上面@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自帶模板,需要配置一些參數(shù):
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服務(wù)器地址 spring.data.redis.host=192.168.223.128 # Redis服務(wù)器連接端口 spring.data.redis.port=6379 # Redis服務(wù)器連接密碼(默認(rèn)為空) 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
如果報錯了,如標(biāo)題的錯誤說明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時出現(xiàn)這個錯誤原因是:

port屬性不能用int接收,改為Integer。
Caused by: java.lang.NumberFormatException: For input string: “port“
到此這篇關(guān)于spring boot整合redis中間件與熱部署實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)spring boot整合redis中間件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一次關(guān)于Redis內(nèi)存詭異增長的排查過程實(shí)戰(zhàn)記錄
這篇文章主要給大家分享了一次關(guān)于Redis內(nèi)存詭異增長的排查過程實(shí)戰(zhàn)記錄,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Redis系列之底層數(shù)據(jù)結(jié)構(gòu)SDS詳解
SDS(簡單動態(tài)字符串)是Redis使用的核心數(shù)據(jù)結(jié)構(gòu),用于替代C語言的字符串,以解決長度獲取慢、內(nèi)存溢出等問題,SDS通過預(yù)分配與惰性釋放策略優(yōu)化內(nèi)存使用,增強(qiáng)安全性,且能存儲文本與二進(jìn)制數(shù)據(jù),可查看源碼src/sds.h和src/sds.c了解更多2024-11-11
全網(wǎng)最完整的Redis新手入門指導(dǎo)教程
這篇文章主要給大家介紹了Redis新手入門的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Redis increment 函數(shù)處理并發(fā)序列號案例
這篇文章主要介紹了Redis increment 函數(shù)處理并發(fā)序列號案例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
Redis?生成分布式業(yè)務(wù)單號的實(shí)現(xiàn)
在業(yè)務(wù)系統(tǒng)中很多場景下需要生成不重復(fù)的ID,本文主要介紹了Redis生成分布式業(yè)務(wù)單號的實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-04-04

