遇到這樣乙個需求:運營人員在發布內容的時候可以選擇性的發布到測試庫、開發庫和線上庫。
專案使用的是spring boot整合redis,實現如下:
org.springframework.datagroupid>
spring-data-redisartifactid>
dependency>
redis.clientsgroupid>
jedisartifactid>
dependency>
spring:
redis:
database: 0
pool:
max-active: 8
max-idle: 9
max-wait: -1
min-idle: 0
redis-dev:
host: 填redis的ip位址
prot: 填redis的埠號
password: 填redis的密碼
testonborrow: fals
redis-test:
host:
prot:
password:
testonborrow: false
redis-online:
host:
prot:
password:
testonborrow: false
針對每個資料來源寫乙個配置類:
這裡就只列舉其中乙個,不同的資料來源設定不同的@bean和@value註解即可
@configuration
public class redisdevconfiguration ") string hostname,
@value("$") int port, @value("$") string password,
@value("$") boolean testonborrow,
@value("$") int maxidle, @value("$") int maxtotal,
@value("$") int index, @value("$") long maxwaitmillis)
public redisconnectionfactory connectionfactory(string hostname, int port, string password, int maxidle,
int maxtotal, int index, long maxwaitmillis, boolean testonborrow)
if (index != 0)
jedis.setpoolconfig(poolcofig(maxidle, maxtotal, maxwaitmillis, testonborrow));
// 初始化連線pool
jedis.afterpropertiesset();
redisconnectionfactory factory = jedis;
return factory;
}public jedispoolconfig poolcofig(int maxidle, int maxtotal, long maxwaitmillis, boolean testonborrow)
}
寫乙個redis例項抽象類
public abstract class abredisconfiguration
public string getdata(string key)
public stringredistemplate gettemple()
}
繼成抽象類實現操作類,這裡只列舉乙個
@component
public class redisdev extends abredisconfiguration
}
在service中注入redis操作類
@autowired
private redisdev redisdev;
@autowired
private redistest redistest;
@autowired
private redistest redisonline;
呼叫不同的操作類即可使用不同的redis資料來源。 Spring Boot Redis 資料快取
之前都是在spring mvc中使用redis,這裡記錄在spring boot中使用redis作為資料快取的過程。參考spring boot整合spring data jpa,搭建專案。本次是在docker中部署redis,從中直接pull官方的redis映象,啟動redis。使用redisdes...
spring boot redis發布訂閱
1.pom 依賴 org.springframework.boot spring boot starter data redis redis.clients jedis 2.配置 redis host 10.5.6.13 port 6379 password sensetime timeout 10...
Spring Boot Redis集群案例
1 配置redis配置檔案 port 6379 daemonize yes bind是繫結ip,0.0.0.0是代表任何ip bind 0.0.0.0 保護模式 protected mode no 新增節點 cluster enabled yes cluster配置檔名,該檔案屬於自動生成,僅用於快...