欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

熱部署

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

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

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

Redis

spring boot整合redis最常用的有三個(gè)工具庫(kù)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í)例不是線程安全的,需要通過(guò)連接池來(lái)使用 Jedis。

Redisson
優(yōu)點(diǎn)點(diǎn):分布式鎖,分布式集合,可通過(guò) Redis 支持延遲隊(duì)列。

Lettuce
用于線程安全同步,異步和響應(yīng)使用,支持集群,Sentinel,管道和編碼器。
基于 Netty 框架的事件驅(qū)動(dòng)的通信層,其方法調(diào)用是異步的。Lettuce 的 API 是線程安全的,所以可以操作單個(gè) Lettuce 連接來(lái)完成各種操作。

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

注意時(shí)spring.data.redis而不是spring.redis后者已經(jīng)舍棄了。

通過(guò)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;
    }
}
//測(cè)試
    @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

測(cè)試:

    @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

如果報(bào)錯(cuò)了,如標(biāo)題的錯(cuò)誤說(shuō)明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時(shí)出現(xiàn)這個(gè)錯(cuò)誤原因是:

Alt

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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在CenOS系統(tǒng)下安裝和配置Redis數(shù)據(jù)庫(kù)的教程

    在CenOS系統(tǒng)下安裝和配置Redis數(shù)據(jù)庫(kù)的教程

    這篇文章主要介紹了在CenOS系統(tǒng)下安裝和配置Redis數(shù)據(jù)庫(kù)的教程,Redis是一個(gè)可基于內(nèi)存的高性能NoSQL數(shù)據(jù)庫(kù),需要的朋友可以參考下
    2015-11-11
  • Redis中的連接命令與鍵命令操作詳解

    Redis中的連接命令與鍵命令操作詳解

    Redis連接命令主要是用于客戶端與服務(wù)器建立連接的,Redis是一種流行的內(nèi)存數(shù)據(jù)庫(kù),支持多種數(shù)據(jù)結(jié)構(gòu),其中鍵命令是核心操作之一,在Redis中,鍵(Key)是用來(lái)存儲(chǔ)數(shù)據(jù)的主要元素,每個(gè)鍵都有一個(gè)唯一的名稱,本文給大家介紹了Redis中的連接命令與鍵命令操作
    2024-09-09
  • Redis數(shù)據(jù)結(jié)構(gòu)之鏈表詳解

    Redis數(shù)據(jù)結(jié)構(gòu)之鏈表詳解

    大家好,本篇文章主要講的是Redis數(shù)據(jù)結(jié)構(gòu)之鏈表詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Redis?腳本和連接命令示例詳解

    Redis?腳本和連接命令示例詳解

    Redis腳本是一種可以實(shí)現(xiàn)復(fù)雜任務(wù)的腳本語(yǔ)言,可以用來(lái)快速履行復(fù)雜任務(wù),靈活處理數(shù)據(jù)管理和管理復(fù)雜的利用場(chǎng)景,這篇文章主要介紹了Redis?腳本和連接命令,需要的朋友可以參考下
    2023-09-09
  • Redis緩存空間優(yōu)化實(shí)踐詳解

    Redis緩存空間優(yōu)化實(shí)踐詳解

    緩存Redis,是我們最常用的服務(wù),其適用場(chǎng)景廣泛,被大量應(yīng)用到各業(yè)務(wù)場(chǎng)景中。也正因如此,緩存成為了重要的硬件成本來(lái)源,我們有必要從空間上做一些優(yōu)化,降低成本的同時(shí)也會(huì)提高性能,本文通過(guò)代碼示例介紹了redis如何優(yōu)化緩存空間,需要的朋友可以參考一下
    2023-04-04
  • 通過(guò)實(shí)例解析布隆過(guò)濾器工作原理及實(shí)例

    通過(guò)實(shí)例解析布隆過(guò)濾器工作原理及實(shí)例

    這篇文章主要介紹了通過(guò)實(shí)例解析布隆過(guò)濾器工作原理及實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 詳解redis在服務(wù)器linux下啟動(dòng)的相關(guān)命令(安裝和配置)

    詳解redis在服務(wù)器linux下啟動(dòng)的相關(guān)命令(安裝和配置)

    這篇文章主要介紹了redis在服務(wù)器linux下的啟動(dòng)的相關(guān)命令(安裝和配置),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • Redis連接池監(jiān)控(連接池是否已滿)與優(yōu)化方法

    Redis連接池監(jiān)控(連接池是否已滿)與優(yōu)化方法

    本文詳細(xì)講解了如何在Linux系統(tǒng)中監(jiān)控Redis連接池的使用情況,以及如何通過(guò)連接池參數(shù)配置、系統(tǒng)資源使用情況、Redis命令監(jiān)控、外部監(jiān)控工具等多種方法進(jìn)行檢測(cè)和優(yōu)化,以確保系統(tǒng)在高并發(fā)場(chǎng)景下的性能和穩(wěn)定性,討論了連接池的概念、工作原理、參數(shù)配置,以及優(yōu)化策略等內(nèi)容
    2024-09-09
  • Redis 事務(wù)與過(guò)期時(shí)間詳細(xì)介紹

    Redis 事務(wù)與過(guò)期時(shí)間詳細(xì)介紹

    這篇文章主要介紹了Redis 事務(wù)與過(guò)期時(shí)間詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 簡(jiǎn)單粗暴的Redis數(shù)據(jù)備份和恢復(fù)方法

    簡(jiǎn)單粗暴的Redis數(shù)據(jù)備份和恢復(fù)方法

    這里我們來(lái)講解一個(gè)簡(jiǎn)單粗暴的Redis數(shù)據(jù)備份和恢復(fù)方法,有一個(gè)在不同主機(jī)上遷移Redis數(shù)據(jù)的示例,還有一個(gè)備份腳本實(shí)現(xiàn)的關(guān)鍵點(diǎn)提示,一起來(lái)看一下:
    2016-06-06

最新評(píng)論