我正在开发一个春季启动应用程序,其中我必须将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;
}
}
}
现在,这就是我所做的,并且在本地运行良好。
我有问题:
在EC2实例中部署应用程序时需要什么更改。我们是否需要在代码中配置主机名和端口?
如果需要配置,是否可以在本地测试部署时会发生的情况?我们可以以某种方式模拟该环境吗?
我已经读过要在本地访问AWS弹性缓存(Redis),我们必须设置代理服务器,这不是一个好习惯,那么我们如何才能轻松地在本地构建应用程序并在云上部署呢?
为什么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:无法从池中获取资源
我试图解释我做了什么,我需要知道什么?如果有人知道任何博客,其中有详细提及的资源,请直接将我指向那里。
发布问题后,我亲自尝试了一些事情。
根据亚马逊
您的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缓存中的数据。与我在上述问题中提到的相同。无需任何更改。
对问题的答案:
在EC2实例中进行部署时,我们需要更改主机名。
在EC2上部署应用程序时,本地运行Redis服务器与运行Redis完全相同,无需更改,请使用我正在使用的Redis配置。
是的,不要创建代理服务器,这超出了缓存的概念。使用Redis服务器在本地运行并更改主机名
我仍然需要找到一种在使用valueOperations时使缓存无效的方法
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句