[經驗] SpringBoot 遠程連接 Linux 上的 Redis


開發環境: 

---------- springboot 2.X

---------- Linux Ubuntu 18.0.04

關於怎么在 Ubuntu 上安裝 Linux , 網上的教程一大堆, 這里就不水了, 只要是能像下面那樣玩, 那就說明 Ubuntu 上的 redis 安裝成功了

 

 

 

(一)  Java 服務器部分的開發

1. 在 springboot 的配置文件里配置 redis 連接相關的配置:

 在 pom.xml 中添加 redis 依賴

        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

在 application.perporties 里添加配置屬性

################## redis 緩存配置 ##################
# Redis數據庫索引
spring.redis.database=0
# Redis服務器地址
spring.redis.host= 虛擬機的IP地址
# Redis服務器連接端口
spring.redis.port=6379
# Redis服務器連接密碼
spring.redis.password=  redis的連接密碼
# 連接池最大連接數
spring.redis.jedis.pool.max-active=8
# 連接池最大阻塞等待時間
spring.redis.jedis.pool.max-wait=-1
# 連接池中的最大空閑連接
spring.redis.jedis.pool.max-idle=8
# 連接池中的最小空閑連接
spring.redis.jedis.pool.min-idle=0
# 連接超時時間(毫秒)
spring.redis.timeout=100

2. 添加一個 redis 連接對象

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/*
 *@Description //TODO 遠程訪問redis服務器測試類$
 *@Author 吾王劍鋒所指 吾等心之所向
 *@Date 2019/9/7 10:39
 */
@Component
public class RedisClientTest {
    private StringRedisTemplate template;

    @Autowired
    public RedisClientTest(StringRedisTemplate template) {
        this.template = template;
    }

    public StringRedisTemplate getTemplate() {
        return template;
    }
}

3. 找到 springboot 里的測試類,,,,,,話說這個測試類我之前都不怎么使用, 都是自己寫測試用例...........................

import cn.gzserver.cache.RedisClientTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

/**
 *  服務器測試類
 * @author avicii
 * @date 2019-09-07
 * */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

    @Autowired
    private RedisClientTest redisCli;

    @Test
    public void contextLoads() {
        StringRedisTemplate redis = redisCli.getTemplate();
        redis.opsForValue().set("name", "Jack");
        String firstName = redis.opsForValue().get("name");
        System.out.println(firstName);
        redis.opsForValue().set("name", "Rose");
        String secondName = redis.opsForValue().get("name");
        System.out.println(secondName);
    }

}

然后啟動

如果不出意外的話, 你就會得到這樣的一個錯誤

org.springframework.data.redis.RedisConnectionFailureException: 
  Unable to connect to Redis;
  nested exception is io.lettuce.core.RedisConnectionException:
     Unable to connect to 192.168.***.***:6379

但是去檢查 Ubuntu 的接口開放情況, 又會發現

 

 6379   這個端口是正常開放的, 那問題的原因出在哪呢

emmmm.....等下再回來補上,,,,我先去試試我的想法    ~逃)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM