javascript
Spring Boot2.0之 整合Redis集群
項(xiàng)目目錄結(jié)構(gòu):?
?pom:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>redis</groupId><artifactId>com.toov5.redis</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version></parent><dependencies><!-- SpringBoot web 核心組件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><maimClass>com.itmayiedu.controller.IndexController</maimClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>yml:
spring:redis:database: 0 # host: 192.168.91.3 # port: 6379 # password: 123jedis:pool:max-active: 8max-wait: -1max-idle: 8min-idle: 0timeout: 10000cluster:nodes:- 192.168.91.5:9001- 192.168.91.5:9002- 192.168.91.5:9003- 192.168.91.5:9004- 192.168.91.5:9005- 192.168.91.5:9006#####用代碼獲取配置文件 靈活獲取密碼方案service
package com.toov5.service;import java.util.Set; import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component;@Component public class RedisService {@Autowiredprivate StringRedisTemplate stringRedisTemplate;//這樣該方法支持多種數(shù)據(jù)類(lèi)型 public void set(String key , Object object, Long time){ // //開(kāi)啟事務(wù)權(quán)限 // stringRedisTemplate.setEnableTransactionSupport(true); // try { // //開(kāi)啟事務(wù) // stringRedisTemplate.multi(); // // String argString =(String)object; //強(qiáng)轉(zhuǎn)下 // stringRedisTemplate.opsForValue().set(key, argString); // // //成功就提交 // stringRedisTemplate.exec(); // } catch (Exception e) { // //失敗了就回滾 // stringRedisTemplate.discard(); // // }if (object instanceof String ) { //判斷下是String類(lèi)型不String argString =(String)object; //強(qiáng)轉(zhuǎn)下//存放String類(lèi)型的 stringRedisTemplate.opsForValue().set(key, argString);}//如果存放Set類(lèi)型if (object instanceof Set) {Set<String> valueSet =(Set<String>)object;for(String string:valueSet){stringRedisTemplate.opsForSet().add(key, string); //此處點(diǎn)擊下源碼看下 第二個(gè)參數(shù)可以放好多 }}//設(shè)置有效期if (time != null) {stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);}}//做個(gè)封裝public void setString(String key, Object object){String argString =(String)object; //強(qiáng)轉(zhuǎn)下//存放String類(lèi)型的 stringRedisTemplate.opsForValue().set(key, argString);}public void setSet(String key, Object object){Set<String> valueSet =(Set<String>)object;for(String string:valueSet){stringRedisTemplate.opsForSet().add(key, string); //此處點(diǎn)擊下源碼看下 第二個(gè)參數(shù)可以放好多 }}public String getString(String key){return stringRedisTemplate.opsForValue().get(key);}}controller
package com.toov5.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import com.toov5.service.RedisService;@RestController public class IndexController {@Autowiredprivate RedisService redisService;@RequestMapping("/setString")public String setString(String key, String value){redisService.set(key, value, 500l); //超時(shí)時(shí)間500s l表示long型return "成功";}@RequestMapping("get")public String get(String key){return redisService.getString(key);} }啟動(dòng)
package com.toov5.app;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication(scanBasePackages={"com.toov5.*"}) public class app {public static void main(String[] args) {SpringApplication.run(app.class, args); } }結(jié)果:
?
?
?
Redis集群的幾個(gè)注意事項(xiàng)
?
Redis集群使用CRC16對(duì)key進(jìn)行hash,集群固定使用16384對(duì)hash出來(lái)的值取模。因?yàn)槿∧=Y(jié)果一定在16384之內(nèi),所以集群中的sharding(分片)實(shí)際就是如何將16384個(gè)值在n個(gè)主節(jié)點(diǎn)間分配(從節(jié)點(diǎn)是主節(jié)點(diǎn)的近似副本,原因見(jiàn)3),如何分配取決于你的配置。
Redis生產(chǎn)級(jí)集群需要容災(zāi),為此,一般部署為n個(gè)主+n*m個(gè)從。n大小主要取決于單機(jī)性能,m大小主要取決于機(jī)器穩(wěn)定性。
Redis集群是弱一致性的,此處的一致,主要指主從之間的數(shù)據(jù)一致性。主要是因?yàn)閞edis在做數(shù)據(jù)更新時(shí),不要求主從數(shù)據(jù)同步復(fù)制一定要成功。
集群最小的主數(shù)量為3,主數(shù)量應(yīng)為奇數(shù),以便做選舉判決。
轉(zhuǎn)載于:https://www.cnblogs.com/toov5/p/9893862.html
總結(jié)
以上是生活随笔為你收集整理的Spring Boot2.0之 整合Redis集群的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: springBoot AOP切面编程
- 下一篇: redis的学习使用,第二章