最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring?boot整合redis中間件與熱部署實現(xiàn)代碼

 更新時間:2023年01月30日 10:49:19   作者:_小許_  
spring?boot整合redis最常用的有三個工具庫Jedis,Redisson,Lettuce,本文重點介紹spring?boot整合redis中間件與熱部署實現(xiàn),需要的朋友可以參考下

熱部署

每次寫完程序后都需要重啟服務(wù)器,需要大量的時間,spring boot提供了一款工具devtools幫助實現(xiàn)熱部署。

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <optional>true</optional> <!-- 可選 -->
 </dependency>

導(dǎo)入插件的以來后每次點擊 ---->構(gòu)建------>構(gòu)建項目就可以了,相比重啟要快的多。

Redis

spring boot整合redis最常用的有三個工具庫Jedis,Redisson,Lettuce。

共同點:都提供了基于 Redis 操作的 Java API,只是封裝程度,具體實現(xiàn)稍有不同。

不同點:

Jedis是 Redis 的 Java 實現(xiàn)的客戶端。支持基本的數(shù)據(jù)類型如:String、Hash、List、Set、Sorted Set。
特點:使用阻塞的 I/O,方法調(diào)用同步,程序流需要等到 socket 處理完 I/O 才能執(zhí)行,不支持異步操作。Jedis 客戶端實例不是線程安全的,需要通過連接池來使用 Jedis。

Redisson
優(yōu)點點:分布式鎖,分布式集合,可通過 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ù)器連接密碼(默認為空)
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ù)器連接密碼(默認為空)
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時出現(xiàn)這個錯誤原因是:

Alt

port屬性不能用int接收,改為Integer。

Caused by: java.lang.NumberFormatException: For input string: “port“

到此這篇關(guān)于spring boot整合redis中間件與熱部署實現(xiàn)的文章就介紹到這了,更多相關(guān)spring boot整合redis中間件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Redis中一些最常見的面試問題總結(jié)

    Redis中一些最常見的面試問題總結(jié)

    Redis在互聯(lián)網(wǎng)技術(shù)存儲方面使用如此廣泛,幾乎所有的后端技術(shù)面試官都要在Redis的使用和原理方面對小伙伴們進行各種刁難。下面這篇文章主要給大家總結(jié)介紹了關(guān)于Redis中一些最常見的面試問題,需要的朋友可以參考下
    2018-09-09
  • Rocky9部署redis的實現(xiàn)示例

    Rocky9部署redis的實現(xiàn)示例

    本文主要介紹了Rocky9部署redis的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 一次關(guān)于Redis內(nèi)存詭異增長的排查過程實戰(zhàn)記錄

    一次關(guān)于Redis內(nèi)存詭異增長的排查過程實戰(zhàn)記錄

    這篇文章主要給大家分享了一次關(guān)于Redis內(nèi)存詭異增長的排查過程實戰(zhàn)記錄,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Redis系列之底層數(shù)據(jù)結(jié)構(gòu)SDS詳解

    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)存使用,增強安全性,且能存儲文本與二進制數(shù)據(jù),可查看源碼src/sds.h和src/sds.c了解更多
    2024-11-11
  • Redis集群部署的過程詳解

    Redis集群部署的過程詳解

    Redis集群是Redis官方推出的分布式解決方案,它允許將數(shù)據(jù)分布在多個Redis節(jié)點中,從而提高Redis的性能和可擴展性,本文給大家介紹了Redis集群部署的過程步驟,需要的朋友可以參考下
    2024-06-06
  • jedis配置含義詳解

    jedis配置含義詳解

    這篇文章主要介紹了jedis配置含義詳解的相關(guān)資料,需要的朋友可以參考下
    2020-04-04
  • 全網(wǎng)最完整的Redis新手入門指導(dǎo)教程

    全網(wǎng)最完整的Redis新手入門指導(dǎo)教程

    這篇文章主要給大家介紹了Redis新手入門的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 在K8s上部署Redis集群的方法步驟

    在K8s上部署Redis集群的方法步驟

    這篇文章主要介紹了在K8s上部署Redis集群的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Redis increment 函數(shù)處理并發(fā)序列號案例

    Redis increment 函數(shù)處理并發(fā)序列號案例

    這篇文章主要介紹了Redis increment 函數(shù)處理并發(fā)序列號案例,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • Redis?生成分布式業(yè)務(wù)單號的實現(xiàn)

    Redis?生成分布式業(yè)務(wù)單號的實現(xiàn)

    在業(yè)務(wù)系統(tǒng)中很多場景下需要生成不重復(fù)的ID,本文主要介紹了Redis生成分布式業(yè)務(wù)單號的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-04-04

最新評論

金华市| 万州区| 永德县| 苍梧县| 彩票| 巫溪县| 城市| 沐川县| 阿克陶县| 舞钢市| 富源县| 乌苏市| 石城县| 东莞市| 通河县| 嵊州市| 平山县| 新龙县| 云阳县| 翁源县| 东方市| 华池县| 池州市| 达拉特旗| 曲沃县| 汝阳县| 凤台县| 巴林右旗| 五河县| 保亭| 永兴县| 安图县| 平凉市| 尚志市| 玉树县| 辽源市| 崇左市| 嫩江县| 安乡县| 龙胜| 武川县|