上次redis 測試redis集羣 在master節點崩掉的狀況下集羣的可用性. 涉及到一個問題,程序如何在主節點失效的狀況下支持切換主節點, 這裏是基於spring boot 集成redis 。java
step1: node
application.propertiesredis
# name of Redis server spring.redis.sentinel.master=devmaster # comma-separated list of host:port pairs spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26380
step2: spring
controller.javaapp
@RequestMapping("/editUser") @ResponseBody public String editUserCache( @RequestParam(value="userId",required = false) long userId, @RequestParam(value="firstName",required = false) String firstName ){ User user=new User(); user.setId((long)userId); user.setFirstName(firstName); ValueOperations<String, String> ops = template.opsForValue(); ops.set("user_"+user.getId(),user.getId()+","+user.getFirstName()); return "userId:"+user.getId().toString()+",userFirstName:"+user.getFirstName()+",userLastName:"+user.getLastName(); }
User.java測試
public class User { private Long id; private String firstName; private String lastName; public User(Long id,String firstName, String lastName) { this.id = id ; this.firstName = firstName; this.lastName = lastName; } public User() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
step3: ui
測試redis 集羣環境 this
向boot發一個hhtp請求 日誌
至關於在redis 加入一個User,對象.server
集羣的結果:
step4:
已知7001是master,讓它休眠
step5:
再次插入記錄
,再插入一條記錄
再次記錄都成功
日誌不少,焦點是7001 ,查看它的部分日誌
日誌顯示7001 開始時master後來失去鏈接,最後成爲7002的slave 。