Spring Boot 2.0 連線Redis (gradle)
阿新 • • 發佈:2018-12-19
目錄
前言
編譯執行下先:
gradlew build
_start.bat
測試在:13. 測試
人生苦短,執行不起來就趕緊去找下一家。
踩了一天的坑,剛爬出來。
START
1. 新建專案
2. 配置 build.gradle
buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' bootJar { baseName = 'redis' // 生成jar名稱 version = '0.1.0' } repositories { mavenCentral() } sourceCompatibility = 1.8 //java 1.8 targetCompatibility = 1.8 dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-data-redis") }
3. 儲存後,右下角 import changes 一下
等待依賴包下載
4. 這裡的專案目錄結構
5. 編寫Application.java
@ComponentScan 裡新增一下自己需要掃描的包
package app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan({"controller","util"}) public class Application { public static void main(String[] args){ SpringApplication.run(Application.class, args); } }
6. 編寫RedisConfig.java
目的是獲得RedisTemplate。
class註釋Annotation標註為@Configuration。
建立返回RedisTemplate的方法,標註為@Bean。
package util; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @Configuration public class RedisConfig { @Bean RedisTemplate template(RedisConnectionFactory redisConnectionFactory){ RedisTemplate template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }
7. 編寫RedisUtil.java
儲存RedisTemplate,包裝一些基本的操作。
片段:
@Component
public class RedisUtil {
@Bean
RedisUtil util(RedisTemplate template){
return new RedisUtil(template);
}
private RedisTemplate template;
public RedisUtil(RedisTemplate template){
this.template = template;
}
}
那麼 只要宣告@Autowired RedisUtil redisUtil,系統就自己找到帶Bean註釋的返回RedisUtil的方法,它的唯一引數template的獲得方法在6.RedisConfig中。會呼叫RedisConfig中的template方法,引數RedisConnectionFactory factory由系統自己傳入,然後再一路往回,創建出RedisUtil,其中template是單例的,不同的Redisutil的template在記憶體中是指向同一個東西。
RedisUtil建構函式把接收到的template給儲存。
讀寫redis資料庫這裡只有簡單的兩種。
package util;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class RedisUtil {
@Bean
RedisUtil util(RedisTemplate template){
return new RedisUtil(template);
}
private RedisTemplate template;
public RedisUtil(RedisTemplate template){
this.template = template;
}
//value
public boolean setValue(Object key, Object value){
try{
template.opsForValue().set(key, value);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public Object getValue(Object key){
return template.opsForValue().get(key);
}
}
8. 編寫C.java
測試類。接收到http請求時寫或者讀sql資料庫。
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import util.RedisUtil;
@RestController
public class C {
@Autowired
RedisUtil redisUtil;
@RequestMapping("/set")
public void set(@RequestParam(value="key", defaultValue = "k") String key,
@RequestParam(value = "value", defaultValue = "v") String value){
redisUtil.setValue(key, value);
}
@RequestMapping("/get")
public Object get(@RequestParam(value="key", defaultValue = "k") String key){
return redisUtil.getValue(key);
}
}
9. 配置檔案application.properties
這裡Redis埠用的是25001
spring.redis.database=0
# Redis伺服器地址
spring.redis.host=127.0.0.1
# Redis伺服器連線埠
spring.redis.port=25001
# Redis伺服器連線密碼(預設為空)
spring.redis.password=
# 連線池最大連線數(使用負值表示沒有限制)
spring.redis.pool.max-active=8
# 連線池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.pool.max-wait=-1
# 連線池中的最大空閒連線
spring.redis.pool.max-idle=8
# 連線池中的最小空閒連線
spring.redis.pool.min-idle=0
10. 啟動Redis服務
測試一下啟動了沒:
恩,啟動了。
11. 編譯
gradlew build
編譯成功:
生成了jar。
12. 執行生成的jar
java -jar build/libs/redis-0.1.0.jar --server.port=15001
這裡web服務埠用的是15001。
13. 測試
先清空redis資料庫。
flushall
瀏覽器開啟:
http://localhost:15001/set?key=abc&value=def
瀏覽器內容空白,沒報錯。
應該呼叫了set方法。
瞅一眼redis資料庫。
keys *
寫進去了。
測試一下get。
瀏覽器開啟:
http://localhost:15001/get?key=abc
顯示:
啊,行了。
另外
RedisUtil根據需要,去包裝。