spring boot中引入redis(redis來自docker)
不邁出實踐永遠都不會知道結果,就像我如果不當面告訴你我喜歡你,我就不知道你不喜歡我一樣,哈哈
到dockerhub上面下載docker,似乎要翻牆登入才能下載,請大家自備梯子。下載連結 https://hub.docker.com/editions/community/docker-ce-desktop-windows?tab=description
在日常的開發中,為了方便化,怎麼快速怎麼來,我們需要安裝視覺化工具,方便快速的進行映象的下載,docker的管理。
我的電腦是win10家庭版,不能直接安裝docker for windows,只能下載docker toolbox 然後進行安裝。
下載完軟體之後,安裝就行了。出現了兩個問題,第一是github的問題,由於docker去請求版本是請求的 api.github.com,會出現說映象版本out of date。所以最好的辦法就是去下載一個iso,https://github.com/boot2docker/boot2docker/releases, 放在C:\Users\junlin.docker\machine\cache,開啟的時候斷網就好了。直接斷網如果覺得不優雅的話可以考慮用別的一大堆辦法。
解除安裝自動安裝的visual box,自己手動下載一個最新的,我的辦法是點開那個自動安裝的,會有安裝更新的提示,然後直接下載那一個就行了,在安裝新的之前,必要的措施是解除安裝掉那一箇舊的。
出現大鯨魚
下面說一下我的個人設計,專案無狀態化,分散式儲存,分散式快取,分散式訊息中介軟體。
在docker hub 裡面搜尋我需要的docker image,我分別需要
zookeeper-redis
zookeeper-hbase
和rabbitmq的容器
用來分別完成上面的工作。開啟視覺化工具進行搜尋。開啟 Kitematic (Alpha) ,然後我是家庭版的系統所以選擇visualbox開啟,輸入dockerhub的賬號密碼就可以登入然後進行想要的image的搜尋了。
用 Kitematic (Alpha) 開啟的dockerhub的介面。
開啟redis的映象
這裡我重新安裝了docker for windows,以下內容適合於安裝了docker for windows,如果是安裝的docker toolbox的朋友,下面內容僅供參考。
redis主要是用於快取各種臨時資料,在分散式環境下,幫助服務無狀態化
設定本機的10000埠與容器中的6379埠對應起來,這樣就可以使用本機的10000號埠訪問容器中的6379埠提供的redis服務了。完整的開啟方式:
docker run --name redis-test -p 6379:6379 -d --restart=always redis:latest redis-server --appendonly yes --requirepass "your passwd"
在spring boot引入redis
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
然後在spring boot的配置檔案中加上對redis的配置
#redis相關
spring.redis.host=127.0.0.1
spring.redis.port=10000
spring.redis.password=kexuejia123
spring.redis.database=0
建立一個redis資料庫訪問物件(DAO)
@Repository
public class RedisDao {
@Autowired
private StringRedisTemplate template;
public void setKey(String key,String value){
ValueOperations<String, String> ops = template.opsForValue();
ops.set(key,value);
}
public String getValue(String key){
ValueOperations<String, String> ops = this.template.opsForValue();
return ops.get(key);
}
}
修改原來的controller:
@RequestMapping(value = Path.LOGIN, method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> userLogin(@RequestParam("phone") final String phone,
@RequestParam("password") final String password,
@RequestParam("ip") final String ip){
Map<String, Object> ans = new HashMap<>();
// TODO: 2018/12/18 驗證該phone是否已經登入
String token = redisService.getTokenFromRedis(phone);
if (token != ""){
ans.put("code", 300);
ans.put("info", "使用者已經登入,請半小時後重新嘗試");
return ans;
}
UserInfoDto dto = userInfoService.getUser(phone);
if (!dto.isRegister()){
dto.setPassword(password);
UserInfoDto dto1 = userInfoService.checkPassword(dto);
if (dto1.isVoladate()){
ans.put("code", 200);
ans.put("info", "成功");
String key = jwtService.getJwtsString(ip);
ans.put("key", key);
// TODO: 2018/12/18 將key放入redis
redisService.insertTokenTORedis(key,phone);
return ans;
}
}
ans.put("code", 400);
ans.put("info", "使用者名稱或密碼錯誤");
return ans;
}
然後編寫RedisService
public interface RedisService {
boolean insertTokenTORedis(String key, String phone);
String getTokenFromRedis(String key);
}
//實現
@Service
public class RedisServiceImpl implements RedisService {
private RedisDao redisDao;
public RedisServiceImpl(RedisDao dao){
this.redisDao = dao;
}
@Override
public boolean insertTokenTORedis(String key, String phone) {
try {
redisDao.setKey(phone, key);
return true;
}catch (Exception e){
return false;
}
}
@Override
public String getTokenFromRedis(String key) {
String check = redisDao.getValue(key);
if (check == null){
return "";
}
return check;
}
}
用postman請求,檢視效果
在docker中的redis
再次發起請求,返回結果
{
"code": 300,
"info": "使用者已經登入,請半小時後重新嘗試"
}