我正在尝试将嵌入式 redis 用于我的dev
spring 配置文件(我也有一个cloud
连接到真实 redis 的配置文件)。
但是,似乎我的嵌入式 redis 配置试图连接到真正的 redis。
这是我在运行dev
配置文件时遇到的错误:
Caused by: redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.
at redis.clients.jedis.Protocol.processError(Protocol.java:113)
at redis.clients.jedis.Protocol.process(Protocol.java:131)
at redis.clients.jedis.Protocol.read(Protocol.java:200)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:285)
at redis.clients.jedis.Connection.getBinaryMultiBulkReply(Connection.java:221)
at redis.clients.jedis.Connection.getMultiBulkReply(Connection.java:214)
at redis.clients.jedis.Jedis.configGet(Jedis.java:2758)
at org.springframework.data.redis.connection.jedis.JedisConnection.getConfig(JedisConnection.java:554)
... 19 more
这JedisDataException NOAUTH
是因为我已经在真实的 Redis 服务器上激活了身份验证,它会尝试连接到真实的服务器......
这是我的 Spring 配置:
@Configuration
public class RedisConfiguration {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
“开发”和“测试”配置:
@Configuration
@EnableRedisHttpSession
@Profile({ Profiles.DEV, Profiles.TEST })
public class RedisDevTestSessionConfiguration {
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisServerBean redisServer() {
return new RedisServerBean();
}
class RedisServerBean implements InitializingBean, DisposableBean {
private RedisServer redisServer;
public void afterPropertiesSet() throws Exception {
redisServer = new RedisServer(Protocol.DEFAULT_PORT);
redisServer.start();
}
public void destroy() throws Exception {
if (redisServer != null) {
redisServer.stop();
}
}
}
}
云配置:
@Configuration
@EnableRedisHttpSession
@Profile(Profiles.CLOUD)
public class RedisCloudSessionConfiguration extends AbstractCloudConfig {
@Bean
public RedisConnectionFactory redisConnectionFactory() {
PoolConfig poolConfig = new PoolConfig(10, 200);// TODO: configure!
return connectionFactory().redisConnectionFactory("REDISCLOUD", new PooledServiceConnectorConfig(poolConfig));
}
这是我的依赖:
compile("redis.embedded:embedded-redis:0.4")
简介:
public final class Profiles {
public static final String CLOUD = "cloud";
public static final String TEST = "test";
public static final String DEV = "dev";
public static final String CAPACITY_TEST = "capacity-test";
}
编辑:
这是我得到的异常,下面给出了相应的配置:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisServer' defined in class path resource [com/bignibou/configuration/session/EmbeddedRedisConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Can't start redis server. Check logs for details.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.bignibou.Application.main(Application.java:22)
Caused by: java.lang.RuntimeException: Can't start redis server. Check logs for details.
at redis.embedded.AbstractRedisInstance.awaitRedisServerReady(AbstractRedisInstance.java:66)
at redis.embedded.AbstractRedisInstance.start(AbstractRedisInstance.java:37)
at redis.embedded.RedisServer.start(RedisServer.java:11)
at com.bignibou.configuration.session.EmbeddedRedisConfiguration$RedisServerBean.afterPropertiesSet(EmbeddedRedisConfiguration.java:26)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 15 more
这是我编辑的redis配置:
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import redis.clients.jedis.Protocol;
import redis.embedded.RedisServer;
@Configuration
@Profile({ "dev" })
public class EmbeddedRedisConfiguration {
@Bean
public RedisServerBean redisServer() {
return new RedisServerBean();
}
class RedisServerBean implements InitializingBean, DisposableBean {
private RedisServer redisServer;
public void afterPropertiesSet() throws Exception {
redisServer = new RedisServer(Protocol.DEFAULT_PORT);
redisServer.start();
}
public void destroy() throws Exception {
if (redisServer != null) {
redisServer.stop();
}
}
}
}