通过Spring Boot Java在本地访问时,AWS Elastic Cache(Redis)无法连接(jedis连接错误)

Sudhanshu Gupta:

我正在开发一个春季启动应用程序,其中我必须将OTP存储在弹性缓存(Redis)中。

弹性缓存是存储OTP的正确选择吗?

使用Redis存储OTP

为了在本地连接Redis,我使用了“ sudo apt-get install Redis-server ”。它已安装并成功运行。

我创建了一个Redisconfig,在其中我向应用程序配置文件询问端口和主机名。在这里,我以为我将使用此主机名和端口连接到aws弹性缓存,但是现在我在本地运行。

public class RedisConfig {

@Value("${redis.hostname}")
private String redisHostName;

@Value("${redis.port}")
private int redisPort;

@Bean
protected JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();
}

@Bean
public RedisTemplate<String,Integer> redisTemplate() {
    final RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    return redisTemplate;
}

现在我使用RedisTemplate和valueOperation放置,读取Redis缓存中的数据

public class MyService {

    private RedisTemplate<String, Integer> redisTemplate;

    private ValueOperations<String, Integer> valueOperations;

    public OtpService(RedisTemplate<String, Integer> redisTemplate) {
        super();
        this.redisTemplate = redisTemplate;
        valueOperations = redisTemplate.opsForValue();
    }

    public int generateOTP(String key) throws Exception {
        try {
            Random random = new Random();
            int otp = 1000 + random.nextInt(9000);
            valueOperations.set(key, otp, 120, TimeUnit.SECONDS);
            return otp;
        } catch (Exception e) {
            throw new Exception("Exception while setting otp" + e.getMessage()) ;
        }
    }

    public int getOtp(String key) {
        try {
            return  valueOperations.get(key);
        } catch (Exception e) {
            return 0;
        }
    }
}

现在,这就是我所做的,并且在本地运行良好。

我有问题:

  1. 在EC2实例中部署应用程序时需要什么更改。我们是否需要在代码中配置主机名和端口?

  2. 如果需要配置,是否可以在本地测试部署时会发生的情况?我们可以以某种方式模拟该环境吗?

  3. 我已经读过要在本地访问AWS弹性缓存(Redis),我们必须设置代理服务器,这不是一个好习惯,那么我们如何才能轻松地在本地构建应用程序并在云上部署呢?

  4. 为什么ValueOperations在设置,放置方法后没有“​​删除”方法?在过期之前使用缓存后,如何使缓存无效?

在本地访问AWS缓存:

当我尝试通过在JedisConnectionFactory实例的创建中放置帖子和主机名来访问AWS弹性缓存(Redis)时

    @Bean
    protected JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(redisHostName, redisPort);
        JedisConnectionFactory factory = new JedisConnectionFactory(configuration);
        return factory;
    }

设置键值时出现错误:

无法获得Jedis连接;嵌套的异常是redis.clients.jedis.exceptions.JedisConnectionException:无法从池中获取资源

我试图解释我做了什么,我需要知道什么?如果有人知道任何博客,其中有详细提及的资源,请直接将我指向那里。

Sudhanshu Gupta:

发布问题后,我亲自尝试了一些事情。

根据亚马逊

您的Amazon ElastiCache实例旨在通过Amazon EC2实例进行访问。

To connect to Redis locally on Linux, 

Run "sudo apt-get install Redis-server". It will install redis server.
Run "redis-cli". It will run Redis on localhost:6379 successfully run.

在Java中连接服务器(Spring Boot)

Redisconfig的

对于application.properties中的本地:redis.hostname = localhost,redis.port = 6379

对于云或在部署到ec2时: redis.hostname =“ amazon弹性缓存端点”,redis.port = 6379

public class RedisConfig {

@Value("${redis.hostname}")
private String redisHostName;

@Value("${redis.port}")
private int redisPort;

@Bean
protected JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(redisHostName, redisPort);
    JedisConnectionFactory factory = new JedisConnectionFactory(configuration);
    return factory;
}

@Bean
public RedisTemplate<String,Integer> redisTemplate() {
    final RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    return redisTemplate;
}

这样,无论您是在本地运行还是在云上运行,只需更改URL,一切都将完美运行。

此后,使用RedisTemplate和valueOperation放置,读取Redis缓存中的数据。与我在上述问题中提到的相同。无需任何更改。

对问题的答案:

  1. 在EC2实例中进行部署时,我们需要更改主机名。

  2. 在EC2上部署应用程序时,本地运行Redis服务器与运行Redis完全相同,无需更改,请使用我正在使用的Redis配置。

  3. 是的,不要创建代理服务器,这超出了缓存的概念。使用Redis服务器在本地运行并更改主机名

  4. 我仍然需要找到一种在使用valueOperations时使缓存无效的方法

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过Spring Boot连接到AWS Aurora Serverless

连接到 RDS 通过 VPC 对等时 Spring Boot UnknownHostException

无法从Spring Boot连接到Dockerized Redis

通过Java / Spring Boot连接到Docker Elasticsearch实例

Spring Boot在连接Oracle数据库时出现错误:无法打开JDBC连接以执行DDL

尝试访问Amazon Elastic Bean Stalk上的Spring Boot应用程序时出现错误404

AWS Elastic Beanstalk上的Spring Boot并记录到文件

无法将Spring Boot战争部署到Elastic Beanstalk

Spring Boot App无法在Elastic Beanstalk上启动

无法通过Spring Boot应用程序访问角度页面

Spring AWS:在Elastic Beanstalk上部署War时访问被拒绝错误

通过SSL的Spring Boot远程Mysql连接

无法通过Spring Boot将Docker Desktop Kubernetes(Windows)服务连接到本地Postgres数据库

在AWS CloudWatch中从AWS Elastic Beanstalk查看Java Spring Boot日志

无法通过Spring Boot和基于Java的配置注入UserDetailsManager

通过谷歌代理将本地spring boot应用程序连接到谷歌云SQL

AWS EC2 无法通过 SSH 连接到 Elastic Beanstalk 创建的 EC2

无法通过AWS Elastic Load Balancer进行与MySQL容器的SSL加密连接

设置SSL连接时出现Spring Boot错误

将Angular与JWT的Spring Boot连接时发生CORS错误

当@Sendto 失败时,rabbitMQ 与 spring boot amqp 连接无限错误

通过Spring Boot与mysql连接时发生时区问题,相当于MitteleuropäischeZeit

通过mysql和spring boot关闭ssl连接时出现静默异常

通过Spring Boot连接到Tibco主题时身份验证失败

从Elastic Beanstalk获取正确的日志(Spring Boot)

JAR中的Spring Boot + Elastic Beanstalk .ebextensions

使用可以通过Spring Boot加密本地

从Spring Boot连接到AWS SQS时出错

使用AWS Elastic Beanstalk在AWS上部署Spring Boot应用程序