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

springBoot (springCloud2025)集成redisCluster 集群的操作方法

 更新時間:2025年11月13日 10:37:51   作者:Java 碼農(nóng)  
文章介紹了如何使用Spring Boot集成Redis Cluster集群,并詳細說明了pom.xml、application.yaml、集群配置類、其他配置類、連接池配置類、Redis工具類以及測試接口的配置和使用方法,感興趣的朋友跟隨小編一起看看吧

在Spring Boot項目中集成Redis Cluster,你可以使用Spring Data Redis庫,它提供了對Redis操作的抽象和封裝。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.5.6</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.emall</groupId>
	<artifactId>account-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>account-service</name>
	<description>account service</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
		<spring-cloud.version>2025.0.0</spring-cloud.version> <!-- 添加 Spring Cloud 版本 -->
	</properties>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web-services</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!---   redis 以及數(shù)據(jù)綁定 開始-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>
		<!-- Redis客戶端:Lettuce(默認,支持集群) -->
		<dependency>
			<groupId>io.lettuce</groupId>
			<artifactId>lettuce-core</artifactId>
		</dependency>
		<!-- Spring Boot Configuration Processor -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<!---   redis 以及數(shù)據(jù)綁定 結(jié)束-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

application.yaml

spring:
  application:
    name: account-service
  redis:
    password: wa123456
    cluster:
      password:
        ${spring.redis.password}
      nodes:
        - 192.168.88.104:6379
        - 192.168.88.104:6380
        - 192.168.88.105:6379
        - 192.168.88.105:6380
        - 192.168.88.106:6379
        - 192.168.88.106:6380
      max-redirects: 3
    lettuce:
      pool:
        max-active: 20
        max-idle: 10
        min-idle: 5
        max-wait: 2000ms
      cluster:
        refresh:
          adaptive: true
          period: 200
    timeout: 5000ms
    connect-timeout: 3000ms
eureka:
  instance:
    hostname: localhost
    instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka
server:
  port: 8760
logging:
  level:
    com.emall.account_service: DEBUG

cluster配置類

package com.emall.account_service.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.ArrayList;
import java.util.List;
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class RedisClusterProperties {
    private List<String> nodes = new ArrayList<>();
    private Integer maxRedirects = 3;
    private String password;
}

其他配置類

package com.emall.account_service.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.time.Duration;
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedisCommonProperties {
    private Duration timeout = Duration.ofMillis(2000);
    private String password;
    private Integer database = 0;
    private Lettuce lettuce = new Lettuce();
    @Data
    public static class Lettuce {
        private Pool pool = new Pool();
        private Cluster cluster = new Cluster();
        @Data
        public static class Pool {
            private int maxActive = 8;
            private int maxIdle = 8;
            private int minIdle = 0;
            private Duration maxWait = Duration.ofMillis(-1);
        }
        @Data
        public static class Cluster {
            private Refresh refresh = new Refresh();
            @Data
            public static class Refresh {
                private Boolean adaptive = true;
                private Duration period = Duration.ofMillis(2000);
            }
        }
    }
}

連接池配置類

package com.emall.account_service.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.time.Duration;
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
@Data
public class LettucePoolProperties {
    private int maxActive = 8;
    private int maxIdle = 8;
    private int minIdle = 0;
    private Duration maxWait = Duration.ofMillis(-1);
    private Duration timeBetweenEvictionRuns = Duration.ofMillis(30000);
}

redis 工具類

package com.emall.account_service.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * 設(shè)置緩存
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }
    /**
     * 設(shè)置緩存并設(shè)置過期時間
     */
    public void set(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }
    /**
     * 獲取緩存
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
    /**
     * 刪除緩存
     */
    public Boolean delete(String key) {
        return redisTemplate.delete(key);
    }
    /**
     * 設(shè)置過期時間
     */
    public Boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }
    /**
     * 判斷key是否存在
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
    /**
     * 哈希操作 - 設(shè)置字段值
     */
    public void hSet(String key, String field, Object value) {
        redisTemplate.opsForHash().put(key, field, value);
    }
    /**
     * 哈希操作 - 獲取字段值
     */
    public Object hGet(String key, String field) {
        return redisTemplate.opsForHash().get(key, field);
    }
    /**
     * 列表操作 - 左推
     */
    public Long lPush(String key, Object value) {
        return redisTemplate.opsForList().leftPush(key, value);
    }
    /**
     * 列表操作 - 右彈出
     */
    public Object rPop(String key) {
        return redisTemplate.opsForList().rightPop(key);
    }
}

測試接口

package com.emall.account_service.controller;
import com.emall.account_service.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AccountFeignController {
    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/account/address")
    public String getAddress(){
        return "account-service:8760";
    }
    @GetMapping("/account/add")
    public String addRedis(){
        String key = "user:account:";
        String value= "劉e非";
        redisUtil.set(key,value);
        System.out.println("保存數(shù)據(jù)成果");
        System.out.println("獲取數(shù)據(jù)成果"+redisUtil.get(key));
        return "success";
   }
}

到此這篇關(guān)于springBoot (springCloud2025)集成redisCluster 集群的文章就介紹到這了,更多相關(guān)springBoot 集成redisCluster 集群內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java異常處理學(xué)習(xí)心得

    Java異常處理學(xué)習(xí)心得

    本篇文章給大家詳細講述了學(xué)習(xí)Java異常處理學(xué)習(xí)的心得以及原理介紹,對此有興趣的朋友參考下吧。
    2018-01-01
  • SpringBoot實現(xiàn)接口統(tǒng)一前綴

    SpringBoot實現(xiàn)接口統(tǒng)一前綴

    本文主要介紹了SpringBoot實現(xiàn)接口統(tǒng)一前綴,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java中向文件寫入數(shù)據(jù)的幾種常見方式分享

    Java中向文件寫入數(shù)據(jù)的幾種常見方式分享

    在日常開發(fā)中,肯定離不開要和文件打交道,今天就簡單羅列一下平時比較常用的創(chuàng)建文件并向文件中寫入數(shù)據(jù)的幾種方式,文中有詳細的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下
    2023-10-10
  • Java并發(fā)編程必備之Synchronized關(guān)鍵字深入解析

    Java并發(fā)編程必備之Synchronized關(guān)鍵字深入解析

    本文我們深入探索了Java中的Synchronized關(guān)鍵字,包括其互斥性和可重入性的特性,文章詳細介紹了Synchronized的三種使用方式:修飾代碼塊、修飾普通方法和修飾靜態(tài)方法,感興趣的朋友一起看看吧
    2025-04-04
  • Java HashMap實現(xiàn)原理分析(一)

    Java HashMap實現(xiàn)原理分析(一)

    這篇文章主要介紹了Java HashMap實現(xiàn)原理的分析,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-08-08
  • Spring自帶的校驗框架Validation的使用實例

    Spring自帶的校驗框架Validation的使用實例

    今天小編就為大家分享一篇關(guān)于Spring自帶的校驗框架Validation的使用實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Spring整合Junit的使用詳解

    Spring整合Junit的使用詳解

    這篇文章主要介紹了Spring整合Junit的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • springboot整合vue2-uploader實現(xiàn)文件分片上傳、秒傳、斷點續(xù)傳功能

    springboot整合vue2-uploader實現(xiàn)文件分片上傳、秒傳、斷點續(xù)傳功能

    對于大文件的處理,無論是用戶端還是服務(wù)端,如果一次性進行讀取發(fā)送、接收都是不可取,很容易導(dǎo)致內(nèi)存問題,下面這篇文章主要給大家介紹了關(guān)于springboot整合vue2-uploader實現(xiàn)文件分片上傳、秒傳、斷點續(xù)傳功能的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • 關(guān)于過濾器Filter的介紹和使用詳解

    關(guān)于過濾器Filter的介紹和使用詳解

    這篇文章主要介紹了關(guān)于過濾器Filter的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • shardingjdbc之配置druid數(shù)據(jù)庫連接池過程

    shardingjdbc之配置druid數(shù)據(jù)庫連接池過程

    這篇文章主要介紹了shardingjdbc之配置druid數(shù)據(jù)庫連接池過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03

最新評論

嘉义市| 隆林| 巴彦淖尔市| 乌什县| 湖口县| 鹤山市| 淳安县| 五指山市| 永昌县| 毕节市| 集贤县| 隆德县| 方城县| 松阳县| 新龙县| 鹰潭市| 阿图什市| 彰化市| 灵武市| 自治县| 永州市| 库伦旗| 新邵县| 新河县| 岳阳市| 沽源县| 蒲城县| 忻城县| 阿巴嘎旗| 吉木萨尔县| 山丹县| 蓬安县| 如东县| 万州区| 梨树县| 嵩明县| 景泰县| 乡宁县| 凌海市| 平陆县| 溧阳市|