阿里云ECS部署spring-boot访问redis出现redis.clients.jedis.HostAndPort - cant resolve localhost address
摘要: 阿里云ECS部署spring-boot+redis的坑
问题LOG
2017-08-17 09:06:52.526 [main] ERROR redis.clients.jedis.HostAndPort - cant resolve localhost address
door_1 | java.net.UnknownHostException: iZuf6buf42e89crw6zf: iZuf6buf42e89crw6zf: Name or service not known
door_1 | at java.net.InetAddress.getLocalHost(InetAddress.java:1505)
door_1 | at redis.clients.jedis.HostAndPort.getLocalHostQuietly(HostAndPort.java:105)
door_1 | at redis.clients.jedis.HostAndPort.<clinit>(HostAndPort.java:12)
door_1 | at redis.clients.jedis.JedisFactory.<init>(JedisFactory.java:37)
door_1 | at redis.clients.jedis.JedisPool.<init>(JedisPool.java:185)
door_1 | at redis.clients.jedis.JedisPool.<init>(JedisPool.java:169)
door_1 | at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createRedisPool(JedisConnectionFactory.java:275)
door_1 | at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createPool(JedisConnectionFactory.java:250)
door_1 | at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.afterPropertiesSet(JedisConnectionFactory.java:237)
door_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
door_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
door_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
door_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
door_1 | at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
door_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
door_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
door_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
door_1 | at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.obtainBeanInstanceFromFactory(ConfigurationClassEnhancer.java:389)
door_1 | at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
根本原因
这个问题的原因是spring-redis在初始化时会实例化LocalHost,但是ECS的host name并没有在/etc/hosts中设置。
解决方案
在/etc/hosts中设置ECS的host name。类似
127.0.0.1 iZuf6buf42e89crw6zf
以上的
iZuf6buf42e89crw6zf,用命令hostname 取到你阿里云自己服务器的名字