一、springboot nosql 簡介
Spring Data提供其他項目,用來幫你使用各種各樣的NoSQL技術,包括MongoDB, Neo4J, Elasticsearch, Solr, Redis,Gemfire, Couchbase和Cassandra。Spring Boot為Redis, MongoDB, Elasticsearch, Solr和Gemfire提供自動配置。你可以充分利用其他項目,但你需要自己配置它們。
1.1、Redis
Redis是一個緩存,消息中間件及具有豐富特性的鍵值存儲系統。Spring Boot為Jedis客戶端庫和由Spring Data Redis提供的基于Jedis客戶端的抽象提供自動配置。 spring-boot-starter-redis 'Starter POM'為收集依賴提供一種便利的方式。
Redis添加maven依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > <!-- <version>1.3.5.RELEASE</version> --> </ dependency > <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons --> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-redis</ artifactId > <!-- <version>1.3.6.RELEASE</version> --> </ dependency > |
1.2連接Redis
你可以注入一個自動配置的RedisConnectionFactory,StringRedisTemplate或普通的跟其他Spring Bean相同的RedisTemplate實例。默認情況下,這個實例將嘗試使用localhost:6379連接Redis服務器。
1
2
3
4
5
6
7
8
9
|
@Component public class MyBean { private StringRedisTemplate template; @Autowired public MyBean(StringRedisTemplate template) { this .template = template; } // ... } |
如果你添加一個你自己的任何自動配置類型的@Bean,它將替換默認的(除了RedisTemplate的情況,它是根據bean的名稱'redisTemplate'而不是它的類型進行排除的)。如果在classpath路徑下存在commons-pool2,默認你會獲得一個連接池工廠。
1.3 建立多個redis連接
使用redis的默認配置可以連接到redis中的0庫中,如果指定庫連接需要配置indexdb,同時如果需要連接多個redis服務,也需要同時配置多個數據源
1.3.1、application.yml 文件 中增加:
1
2
3
4
5
6
7
8
9
|
@Component public class MyBean { private StringRedisTemplate template; @Autowired public MyBean(StringRedisTemplate template) { this.template = template; } // ... } |
1.3.2、創建redisconfiguration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
@Configuration public class Redis137_11Configuration { @Bean (name = "redis123Template" ) public StringRedisTemplate redisTemplate( @Value ( "${redis123.hostName}" ) String hostName, @Value ( "${redis123.port}" ) int port, @Value ( "${redis123.password}" ) String password, @Value ( "${redis123.maxIdle}" ) int maxIdle, @Value ( "${redis123.maxTotal}" ) int maxTotal, @Value ( "${redis123.index}" ) int index, @Value ( "${redis123.maxWaitMillis}" ) long maxWaitMillis, @Value ( "${redis123.testOnBorrow}" ) boolean testOnBorrow) { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory(connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow)); return temple; } public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle, int maxTotal, int index, long maxWaitMillis, boolean testOnBorrow) { JedisConnectionFactory jedis = new JedisConnectionFactory(); jedis.setHostName(hostName); jedis.setPort(port); if (!StringUtils.isEmpty(password)) { jedis.setPassword(password); } if (index != 0 ) { jedis.setDatabase(index); } 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) { JedisPoolConfig poolCofig = new JedisPoolConfig(); poolCofig.setMaxIdle(maxIdle); poolCofig.setMaxTotal(maxTotal); poolCofig.setMaxWaitMillis(maxWaitMillis); poolCofig.setTestOnBorrow(testOnBorrow); return poolCofig; } } |
1.3.3、聲明redis抽象基類,創建redis的操作方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public abstract class AbRedisConfiguration { protected StringRedisTemplate temple; public void setData(String key, String value) { getTemple().opsForValue().set(key, value); } public String getData(String key) { return getTemple().opsForValue().get(key); } public StringRedisTemplate getTemple() { return temple; } } |
1.3.4、根據數據源,創建不同的子類@Component
1
2
3
4
5
6
7
8
9
|
public class Redis123 extends AbRedisConfiguration { @Resource (name = "redis123Template" ) private StringRedisTemplate temple; public StringRedisTemplate getTemple() { return temple; } } |
ps:類和子類中同時聲明了getTemple方法和 StringRedisTemple屬性,子類通過重寫父類的getTeimple方法,把子類的自己StringRedisTemple 屬性 傳給 父類,父類通過子類傳遞過來的StringRedisTemple使用不通的數據鏈接來操作緩存。至此,父類完成所有的操作方法,而當需要創建一個數據庫連接時,只需要在創建一個子類,被聲明自己的StringRedisTemple,并傳給父類即可。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/wangqingqi20005/article/details/52613387