当前位置: 首页 > 知识库问答 >
问题:

Docker-用springboot和redis创作

仲孙鸣
2023-03-14
    java-service_1  | 2020-11-20 10:30:54.053 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing 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] with root cause
java-service_1  | 
java-service_1  | java.net.ConnectException: Connection refused (Connection refused)
java-service_1  |       at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_252]
java-service_1  |       at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_252]
java-service_1  |       at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_252]
java-service_1  |       at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_252]
java-service_1  |       at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_252]
java-service_1  |       at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_252]
java-service_1  |       at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:53) ~[jedis-3.3.0.jar!/:na]
java-service_1  |       at redis.clients.jedis.Connection.connect(Connection.java:158) ~[jedis-3.3.0.jar!/:na]
java-service_1  |       at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:109) ~[jedis-3.3.0.jar!/:na]
@Bean
public RedisStandaloneConfiguration redisStandaloneConfiguration() {
    System.out.println("connecting with redis");
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("redis", 6379);
    return redisStandaloneConfiguration;
}

@Bean
public ClientOptions clientOptions() {
    return ClientOptions.builder()
            .disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
            .autoReconnect(true)
            .build();
}

@Bean
public RedisConnectionFactory connectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration) {

    LettuceClientConfiguration configuration = LettuceClientConfiguration.builder()
            .clientOptions(clientOptions()).build();

    return new LettuceConnectionFactory(redisStandaloneConfiguration, configuration);
}

@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
@Primary
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}

共有1个答案

鲁霄
2023-03-14

我认为出现问题是因为您的应用程序无法连接到DB,因为

RedisStandaloneConfiguration redisStandaloneConfiguration 
                                    = new RedisStandaloneConfiguration("redis", 6379);

现在,您使用redis作为主机名,这是不正确的。您应该使用容器名称作为主机,并确保两个容器位于同一网络上。

为了使其工作,您可以执行以下操作:

docker-compose.yml

version: '3'
services:
  redis-server:
    container_name: redis
    image: 'redis'
    ports:
      - "6379:6379"
  java-service:
    build: .
    links:
      - redis-server
    ports:
      - "8080:8080"

现在,同一网络上的其他容器将能够使用redis作为主机名与redis通信。

version: '3'
services:
  redis-server:
    hostname: redis
    image: 'redis'
    ports:
      - "6379:6379"
  java-service:
    build: .
    links:
      - redis-server
    ports:
      - "8080:8080"
 类似资料:
  • 我使用windows docker工具箱,我很困惑我错过了什么。我想使用redis commander(https://www.npmjs.com/package/redis-Commander)和来自docker Hub的docker图像redis。 我使用了上面链接中的docker-compose.yml: 在 工作,我可以到达服务器和er本地:6379 但是是什么意思 有什么帮助来正确设置这

  • 操作系统-Windows 7 Docker版本:Docker版本18.03.0-CE,版本0520E24302 springboot版本:1.5.3.发布 我从https://github.com/techprimers/docker-mysql--boot-example中获取了代码库(这是macOS中的工作示例代码) 我的应用程序.属性如下所示 Dockerfile如下所示 > 使用Docke

  • 我有一个运行Ubuntu的服务器,安装了Redis和Docker。我正在创建托管在Docker容器中的服务。如何将Docker容器中运行的服务与Docker外部运行的Redis连接起来? 我可以ping服务器。ioredis驱动程序无法连接到redis。错误:connect ECONREFUSE:6379

  • Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 的 NoSQL 数据库,并提供多种语言的 API。 1、查看可用的 Redis 版本 访问 Redis 镜像库地址: https://hub.docker.com/_/redis?tab=tags。 可以通过 Sort by 查看其他版本的 Redis,默认是最新版本 redis:la

  • Docker Flask Celery Redis A basic Docker Compose template for orchestrating a Flask application & a Celery queue with Redis Installation git clone https://github.com/mattkohl/docker-flask-celery-redis