通过 spring 启动 java 在本地访问时,Aws Elastic Cache (Redis) 连接失败(jedis 连接错误)
Aws Elastic Cache (Redis) failed to connect (jedis connection error) when acessed locally through spring boot java
我正在开发 spring 启动应用程序,我必须在弹性缓存 (Redis) 中存储 OTP。
Is elastic cache right choice to store OTP?
使用Redis存储OTP
为了在本地连接到 Redis,我使用了“sudo apt-get install Redis-server”。安装成功 运行.
我创建了一个 Redisconfig,我在其中向应用程序配置文件询问端口和主机名。在这里,我想我会使用这个主机名和端口连接到 aws 弹性缓存,但现在我在本地 运行ning。
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 elastic cache (Redis) 我们必须设置代理服务器,这不是一个好的做法,那么我们如何轻松地在本地构建应用程序并部署到云?
为什么 ValueOperations 在设置、放置方法后没有 "delete" 方法?一旦缓存在到期时间之前完成使用,我该如何使缓存失效?
正在本地访问 AWS 缓存:
当我尝试通过在 JedisConnectionFactory 实例
的创建中放入 post 和主机名来访问 aws 弹性缓存 (Redis) 时
@Bean
protected JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(redisHostName, redisPort);
JedisConnectionFactory factory = new JedisConnectionFactory(configuration);
return factory;
}
设置键值时出错:
Cannot get Jedis connection; nested exception is
redis.clients.jedis.exceptions.JedisConnectionException: Could not get
a resource from the pool
我试图解释我做了什么以及我需要知道什么?
如果有人知道任何详细提及内容的博客和资源,请告诉我那里。
发布问题后,我自己尝试了一些东西。
根据亚马逊,
Your Amazon ElastiCache instances are designed to be accessed through
an Amazon EC2 instance.
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 启动)
Redis 配置
For local in application.properties: redis.hostname = localhost, redis.port = 6379
For cloud or when deployed to ec2: redis.hostname = "amazon Elastic cache endpoint", 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 put,读取Redis缓存中的数据。就像我在上面的问题中提到的一样。无需任何更改。
问题答案:
我们需要在EC2实例中部署时更改主机名。
运行 Redis 服务器本地与 运行ning Redis 完全相同,当应用程序部署在 EC2 上时,无需更改,使用我正在使用的 Redis 配置。
是的,不要创建代理服务器,这违背了缓存的初衷。 运行 在本地使用 Redis 服务器并更改主机名
在使用valueOperations
时,我仍然需要找到一种使缓存无效的方法
我正在开发 spring 启动应用程序,我必须在弹性缓存 (Redis) 中存储 OTP。
Is elastic cache right choice to store OTP?
使用Redis存储OTP
为了在本地连接到 Redis,我使用了“sudo apt-get install Redis-server”。安装成功 运行.
我创建了一个 Redisconfig,我在其中向应用程序配置文件询问端口和主机名。在这里,我想我会使用这个主机名和端口连接到 aws 弹性缓存,但现在我在本地 运行ning。
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 elastic cache (Redis) 我们必须设置代理服务器,这不是一个好的做法,那么我们如何轻松地在本地构建应用程序并部署到云?
为什么 ValueOperations 在设置、放置方法后没有 "delete" 方法?一旦缓存在到期时间之前完成使用,我该如何使缓存失效?
正在本地访问 AWS 缓存:
当我尝试通过在 JedisConnectionFactory 实例
的创建中放入 post 和主机名来访问 aws 弹性缓存 (Redis) 时 @Bean
protected JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(redisHostName, redisPort);
JedisConnectionFactory factory = new JedisConnectionFactory(configuration);
return factory;
}
设置键值时出错:
Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
我试图解释我做了什么以及我需要知道什么? 如果有人知道任何详细提及内容的博客和资源,请告诉我那里。
发布问题后,我自己尝试了一些东西。
根据亚马逊,
Your Amazon ElastiCache instances are designed to be accessed through an Amazon EC2 instance.
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 启动)
Redis 配置
For local in application.properties: redis.hostname = localhost, redis.port = 6379
For cloud or when deployed to ec2: redis.hostname = "amazon Elastic cache endpoint", 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 put,读取Redis缓存中的数据。就像我在上面的问题中提到的一样。无需任何更改。
问题答案:
我们需要在EC2实例中部署时更改主机名。
运行 Redis 服务器本地与 运行ning Redis 完全相同,当应用程序部署在 EC2 上时,无需更改,使用我正在使用的 Redis 配置。
是的,不要创建代理服务器,这违背了缓存的初衷。 运行 在本地使用 Redis 服务器并更改主机名
在使用valueOperations
时,我仍然需要找到一种使缓存无效的方法