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

SpringBoot整合Redis哨兵模式的實現(xiàn)示例

 更新時間:2024年02月06日 10:05:29   作者:-代號9527  
Redis哨兵模式是Redis高可用方案的一種實現(xiàn)方式,通過哨兵來自動實現(xiàn)故障轉(zhuǎn)移,從而保證高可用,本文主要介紹了SpringBoot整合Redis哨兵模式的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下

1、Redis哨兵復習

在這里插入圖片描述

Redis哨兵主要有三點作用:

  • 監(jiān)控:不斷檢查master和slave是否正常運行
  • 通知:當被監(jiān)控的主從服務(wù)器發(fā)生問題時,向其他哨兵和客戶端發(fā)送通知
  • 自動故障轉(zhuǎn)移:斷開master和slave的連接,選取一個slave做為master,將其他slave連接到新的maser,并導致客戶端新的maser地址

關(guān)于Redis哨兵模式的搭建和詳解,看這篇Redis哨兵模式

2、整合

  • 在pom.xml文件中添加以下依賴:
<dependencies>
    <!-- Redis依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>
  • 在application.properties文件中添加以下Redis配置:
# Redis連接配置
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=host1:port1,host2:port2,host3:port3

# spring.redis.sentinel.master的值為你的Redis主節(jié)點名稱
# spring.redis.sentinel.nodes的值為你的Redis哨兵節(jié)點列表。
  • 如果項目的配置文件用的yaml文件,則Redis配置的格式用下面這個
# 單哨兵
spring:
  redis:
    sentinel:
      master: mymaster
      nodes: 192.168.43.243:26379
    lettuce:
      pool:
        max-idle: 10
        max-active: 20
        min-idle: 0
        max-wait: 10000ms
# 多哨兵,則nodes用數(shù)組格式,也就是橫線
spring:
  redis:
    sentinel:
      master: your-redis-master  # 指定Redis的主節(jié)點名稱
      nodes:  # 指定一個或多個哨兵節(jié)點的地址和端口號
        - host: your-sentinel1-host
          port: your-sentinel1-port
        - host: your-sentinel2-host
          port: your-sentinel2-port
        - host: your-sentinel3-host
          port: your-sentinel3-port
    password: your-redis-password
    # 在哨兵模式中,Redis哨兵節(jié)點的配置通常與主節(jié)點保持一致,因為哨兵節(jié)點不存儲數(shù)據(jù),它們只負責監(jiān)控節(jié)點的可用性。
    # 所以你不需要在哨兵節(jié)點的配置中指定用戶名和密碼,只需要在主節(jié)點的配置中指定密碼即可

關(guān)于連接池的參數(shù):

  • max-active:連接池中最大活躍連接數(shù)(默認值為8)。當連接數(shù)達到最大活躍連接數(shù)時,新的請求將會等待,直到有可用的連接為止。
  • max-idle:連接池中最大空閑連接數(shù)(默認值為8)。當空閑連接數(shù)超過最大空閑連接數(shù)時,多余的空閑連接將被釋放。
  • min-idle:連接池中最小空閑連接數(shù)(默認值為0)。連接池中保持的最小空閑連接數(shù),當請求連接時,如果空閑連接數(shù)不足,則會創(chuàng)建新的連接。
  • max-wait:連接池中獲取連接的最大等待時間(默認-1,表示無限等待)。當連接池中的連接都被占用且達到最大活躍連接數(shù)時,新的請求會等待一段時間,如果超過最大等待時間,則會拋出異常。
  • test-on-create:在創(chuàng)建新的連接時,是否進行連接的測試驗證(默認值為false)。如果設(shè)置為true,則在創(chuàng)建連接的時候會執(zhí)行一次測試命令,確保連接可用。
  • test-on-borrow:在從連接池中借用連接時,是否進行連接的測試驗證(默認值為false)。如果設(shè)置為true,則在從連接池中獲取連接的時候會執(zhí)行一次測試命令,確保連接可用。
  • test-on-return:在歸還連接到連接池時,是否進行連接的測試驗證(默認值為false)。如果設(shè)置為true,則在歸還連接到連接池的時候會執(zhí)行一次測試命令,確保連接可用。
  • test-while-idle:在連接空閑一段時間后,是否進行連接的測試驗證(默認值為true)。如果設(shè)置為true,則連接空閑一段時間后會執(zhí)行一次測試命令,確保連接可用。

最后,如果報錯

"NOAUTH HELLO must be called with the client already HELLO" 

表明Redis連接需要進行認證,但是在發(fā)起 HELLO 命令之前沒有進行認證??梢詥为毤酉律诒拿艽a:

spring.redis.sentinel.password=code9527

3、簡單舉例

  • 使用RedisTemplate或者使用注解的方式來訪問Redis。以下是使用RedisTemplate的示例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    public void saveUser(String username, String password) {
        redisTemplate.opsForHash().put("users", username, password);
    }
    
    public String findUser(String username) {
        return (String) redisTemplate.opsForHash().get("users", username);
    }
    
}

//RedisTemplate是通過自動裝配注入的,可以直接使用來操作Redis
  • 創(chuàng)建一個Controller類來處理請求并調(diào)用UserService中的方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    
    @Autowired
    private UserService userService;

	@PostMapping("/user/addRedis")
	public void addUserToRedis(String username,String password){
		userService.saveUser(username,password);
	}
    
    @GetMapping("/user/{username}")
    public String getUser(@PathVariable String username) {
        return userService.findUser(username);
    }
    

}

測試一下:

在這里插入圖片描述

添加成功:

在這里插入圖片描述

查詢一下:

在這里插入圖片描述

最后記錄下一個我遇到的問題,哨兵拿到master的host時,host是個域名,我本地IDEA啟動時不能解析成IP,報錯:

在這里插入圖片描述

可以先通過修改操作系統(tǒng)的Hosts文件來實現(xiàn)模擬域名和IP的關(guān)系:

C:\Windows\System32\drivers\etc

在文件末尾添加一行,格式為 "<IP地址> <域名>"
例如: "127.0.0.1 mydomain.com".

 到此這篇關(guān)于SpringBoot整合Redis哨兵模式的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot Redis哨兵 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論