java无法存入redis_java - 无法从Docker容器中的Spring连接到Redis - 堆栈内存溢出

我正在尝试容器化并尝试在写入redis的docker容器内运行可执行jar。

我已经尝试过所有关于StackOverflow的建议,包括:

我在OSX High Sierra 10.13.6上并使用最新的Docker桌面(社区)版本2.0.0.0-mac81 Docker引擎18.09.0 Docker撰写:1.23.2

我正在使用由Homebrew安装的Redis:Redis服务器v = 5.0.2 sha = 00000000:0 malloc = libc位= 64 build = 25bb354a43384ae5

我修改了redis.conf文件以禁用绑定并关闭保护模式

在/usr/local/etc/redis.conf内部注释掉所有绑定命令,例如

#bind 127.0.0.1 ::1

#bind 0.0.0.0 ::1

将保护模式设置为关闭

protected-mode no

我正在以这种方式运行redis服务器以启用这些更改:

redis-server /usr/local/etc/redis.conf

我正在以这种方式通过redis-cli收听redis:

redis-cli monitor

这是我的Dockerfile:

FROM alpine:3.2

RUN apk --update add openjdk7-jre

RUN apk add bash

ENV \

LC_ALL=en_US.UTF-8 \

GRADLE_USER_HOME=/tmp

RUN mkdir -p /var/foo/helloredis

ENV PATH "$PATH:."

ENV CLASSPATH "$CLASSPATH:."

EXPOSE 8080

COPY target/foo-v1.jar /var/foo/helloredis

WORKDIR /var/foo/helloredis

#ENTRYPOINT ["/bin/bash"]

ENTRYPOINT ["java", "-jar", "foo-v1.jar"]

这是我用来尝试调试的一个简单的spring应用程序,然后我可以将其编译并运行在bash中,该bash可以连接到Redis,而不会出现问题。

mvn clean install

java -jar target/foo-v1.jar

curl -4 localhost:8080/set

快乐的redis-cli输出:

1544470460.458554 [0 127.0.0.1:55989] "PSUBSCRIBE" "__keyevent@*:expired" "spring:session:event:created:*" "__keyevent@*:del"

1544470469.040193 [0 127.0.0.1:55991] "HMSET" "spring:session:sessions:934a7e2b-5c28-4c04-8361-57811d153b3e" "lastAccessedTime" "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01g\x99\x9d\x19\x9d" "sessionAttr:testKey" "\xac\xed\x00\x05t\x00\ttestValue" "maxInactiveInterval" "\xac\xed\x00\x05sr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\a\b" "creationTime" "\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01g\x99\x9d\x19\x9d"

1544470469.042203 [0 127.0.0.1:55991] "SADD" "spring:session:expirations:1544472300000" "\xac\xed\x00\x05t\x00,expires:934a7e2b-5c28-4c04-8361-57811d153b3e"

1544470469.042970 [0 127.0.0.1:55991] "PEXPIRE" "spring:session:expirations:1544472300000" "2100000"

1544470469.045250 [0 127.0.0.1:55991] "APPEND" "spring:session:sessions:expires:934a7e2b-5c28-4c04-8361-57811d153b3e" ""

1544470469.045519 [0 127.0.0.1:55991] "PEXPIRE" "spring:session:sessions:expires:934a7e2b-5c28-4c04-8361-57811d153b3e" "1800000"

1544470469.045861 [0 127.0.0.1:55991] "PEXPIRE" "spring:session:sessions:934a7e2b-5c28-4c04-8361-57811d153b3e" "2100000"

1544470469.048981 [0 127.0.0.1:55991] "PUBLISH" "spring:session:event:created:934a7e2b-5c28-4c04-8361-57811d153b3e" "\xac\xed\x00\x05sr\x00\x11java.util.HashMap\x05\a\xda\xc1\xc3\x16`\xd1\x03\x00\x02F\x00\nloadFactorI\x00\tthresholdxp?@\x00\x00\x00\x00\x00\x04w\b\x00\x00\x00\x04\x00\x00\x00\x00x"

1544470489.886247 [0 127.0.0.1:55991] "PING"

1544470500.005288 [0 127.0.0.1:55991] "SMEMBERS" "spring:session:expirations:1544470500000"

1544470500.006675 [0 127.0.0.1:55991] "DEL" "spring:session:expirations:1544470500000"

1544470519.885170 [0 127.0.0.1:55991] "PING"

的pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.foo

foo

v1

io.spring.platform

platform-bom

Athens-SR2

pom

import

org.springframework.boot

spring-boot-dependencies

1.5.9.RELEASE

pom

import

org.springframework.session

spring-session

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-redis

org.springframework.boot

spring-boot-maven-plugin

1.5.9.RELEASE

repackage

的src / main / JAVA / COM /富/ APP.java

package com.foo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class APP {

public static void main(String[] args) throws Exception {

SpringApplication.run(APP.class, args);

}

}

的src / main / JAVA / COM /富/ helloredis / Example.java

package com.foo.helloredis;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.*;

@RestController

public class Example {

@RequestMapping("/set")

String set(HttpServletRequest req) {

req.getSession().setAttribute("testKey", "testValue");

return "foo session:testKey=testValue";

}

@RequestMapping("/query")

String query(HttpServletRequest req) {

Object value = req.getSession().getAttribute("testKey");

return "foo Session:\"testKey\"=" + value;

}

}

的src / main / JAVA / COM /富/ helloredis / HttpSessionConfig.java

package comn.foo.helloredis;

import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableRedisHttpSession

public class HttpSessionConfig {

}

但是,当我在docker中运行此应用程序时,我无法连接到Redis,这是错误消息:

docker build -t foo .

docker run --rm -it foo

. ____ _ __ _ _

/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v1.4.3.RELEASE)

2018-12-10 19:39:16.039 INFO 1 --- [ main] com.foo.APP : Starting APP on 547b52504434 with PID 1 (/var/foo/helloredis/foo-v1.jar started by root in /var/foo/helloredis)

2018-12-10 19:39:16.046 INFO 1 --- [ main] com.foo.APP : No active profile set, falling back to default profiles: default

2018-12-10 19:39:16.146 INFO 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7c1ba98b: startup date [Mon Dec 10 19:39:16 GMT 2018]; root of context hierarchy

2018-12-10 19:39:18.062 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!

2018-12-10 19:39:19.351 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)

2018-12-10 19:39:19.372 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat

2018-12-10 19:39:19.373 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.6

2018-12-10 19:39:19.512 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext

2018-12-10 19:39:19.512 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3370 ms

2018-12-10 19:39:19.767 WARN 1 --- [ost-startStop-1] o.s.b.a.s.RedisSessionConfiguration : Spring Session store type is mandatory: set 'spring.session.store-type=redis' in your configuration

2018-12-10 19:39:20.015 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]

2018-12-10 19:39:20.020 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]

2018-12-10 19:39:20.021 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'springSessionRepositoryFilter' to: [/*]

2018-12-10 19:39:20.021 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

2018-12-10 19:39:20.021 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]

2018-12-10 19:39:20.022 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]

2018-12-10 19:39:20.749 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7c1ba98b: startup date [Mon Dec 10 19:39:16 GMT 2018]; root of context hierarchy

2018-12-10 19:39:20.879 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/set]}" onto java.lang.String com.foo.helloredis.Example.set(javax.servlet.http.HttpServletRequest)

2018-12-10 19:39:20.881 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/query]}" onto java.lang.String com.foo.helloredis.Example.query(javax.servlet.http.HttpServletRequest)

2018-12-10 19:39:20.884 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)

2018-12-10 19:39:20.884 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)

2018-12-10 19:39:20.944 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

2018-12-10 19:39:20.944 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

2018-12-10 19:39:21.014 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

2018-12-10 19:39:21.364 WARN 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/boot/autoconfigure/session/RedisSessionConfiguration$SpringBootRedisHttpSessionConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

2018-12-10 19:39:21.365 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

2018-12-10 19:39:21.370 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat

2018-12-10 19:39:21.396 INFO 1 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.

2018-12-10 19:39:21.408 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/boot/autoconfigure/session/RedisSessionConfiguration$SpringBootRedisHttpSessionConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1589) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]

at com.foo.APP.main(APP.java:9) [classes!/:na]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79]

at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79]

at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [foo-v1.jar:na]

at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [foo-v1.jar:na]

at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [foo-v1.jar:na]

at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [foo-v1.jar:na]

Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:198) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:345) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]

at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration$EnableRedisKeyspaceNotificationsInitializer.afterPropertiesSet(RedisHttpSessionConfiguration.java:223) ~[spring-session-1.2.2.RELEASE.jar!/:na]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1648) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

... 24 common frames omitted

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

at redis.clients.util.Pool.getResource(Pool.java:53) ~[jedis-2.8.2.jar!/:na]

at redis.clients.jedis.JedisPool.getResource(JedisPool.java:99) ~[jedis-2.8.2.jar!/:na]

at redis.clients.jedis.JedisPool.getResource(JedisPool.java:12) ~[jedis-2.8.2.jar!/:na]

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:191) ~[spring-data-redis-1.7.6.RELEASE.jar!/:na]

... 28 common frames omitted

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused

at redis.clients.jedis.Connection.connect(Connection.java:164) ~[jedis-2.8.2.jar!/:na]

at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80) ~[jedis-2.8.2.jar!/:na]

at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1677) ~[jedis-2.8.2.jar!/:na]

at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:87) ~[jedis-2.8.2.jar!/:na]

at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868) ~[commons-pool2-2.4.2.jar!/:2.4.2]

at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435) ~[commons-pool2-2.4.2.jar!/:2.4.2]

at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) ~[commons-pool2-2.4.2.jar!/:2.4.2]

at redis.clients.util.Pool.getResource(Pool.java:49) ~[jedis-2.8.2.jar!/:na]

... 31 common frames omitted

Caused by: java.net.ConnectException: Connection refused

at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.7.0_79]

at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[na:1.7.0_79]

at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[na:1.7.0_79]

at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[na:1.7.0_79]

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.7.0_79]

at java.net.Socket.connect(Socket.java:579) ~[na:1.7.0_79]

at redis.clients.jedis.Connection.connect(Connection.java:158) ~[jedis-2.8.2.jar!/:na]

... 38 common frames omitted

感谢您提供任何线索!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值