English 中文(简体)
靠单列集装箱进行再处理,使用cker器进行跳板,但跳板无法使用再处理服务器
原标题:Trying to connect standalone containers redis and springboot application using docker-compose but springboot is not able access the redis server

About application

Simple backend applicaction that counts number of visits happened to be done by an user. Redis stores the data of the visits made.


ERROR DESCRIPTION

春天电梯应用虽然显示再电,但无法与再电服务器连接,并且其应用也在同一cker网。

error @http://localhost:8080/ -

White label Error  (type=Internal Server Error, status=500).

Error in sprinboot-app container:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception 
[Request processing failed: 
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis] with root cause

Dockerfile:

FROM openjdk:17
WORKDIR /app
COPY target/visitsapplication-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]

DockerCompose.yml

version:  3 
services:
    redis-server:
    image:  redis 
    ports:
        - "6379:6379"
    springboot-app:
    build: .
    ports:
        - "8080:8080"  

application.properties

server.port=8080
spring.redis.host=redis-server
spring.redis.port=6379

VisitController.java

package com.visitapplication.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class VisitsController {
        @Autowired
        private RedisTemplate<String, String> redisTemplate;

        @GetMapping
        public String getVisits() {
            ValueOperations<String, String> ops = redisTemplate.opsForValue();
            String visits = ops.get("visits");

            if (visits == null) {
                visits = "0";
            }

            ops.set("visits", String.valueOf(Integer.parseInt(visits) + 1));

            return "Number of visits is " + visits;
        }
}

Commands run to check the issues


Command:
 docker-compose logs springboot-app
Output :
java.net.ConnectException: Connection refused.

Command:
docker network inspect visitsapplication_default
Output:
[
    {
        "Name": "visitsapplication_default",
        "Id": "671f39cacd25f8f2c694eebed7515c16dfae3efedc86ae57c567e8ab0567fed2",
        "Created": "2023-12-14T06:06:20.465060571Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "7437823988de4a68b31f72b423d75bd7172be555b7c77879a5f76eb3cb248d1d": {
                "Name": "visitsapplication-redis-server-1",
                "EndpointID": "c138d3fa863efed0accf193acc4992a382245e160840337554fc76f74e3f914e",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            },
            "be2ef8d7f9ed05fb7b98e21473948b7f005fa0fb3b268f03105a98ee5ae79eeb": {
                "Name": "visitsapplication-springboot-app-1",
                "EndpointID": "fa1ee3bccb4097dbe8cfb5388eb471c9836e2a84b21e94fc266d3542e0e81e5a",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "visitsapplication",
            "com.docker.compose.version": "2.17.3"
        }
    }
]
The Docker network inspection confirms that both visitsapplication-redis-server-1 and visitsapplication-springboot-app-1 containers are on the same Docker network named visitsapplication_default. Therefore, they should be able to communicate with each other using their service names (redis-server and springboot-app, respectively).But that is not the case

问题回答

I found the issue. In your application.properties file Can you replace

spring.redis.host=redis-server
spring.redis.port=6379

页: 1

spring.data.redis.host=redis-server
spring.data.redis.port=6379




相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

Listen to a port that is in use [duplicate]

Possible Duplicate: Get connecting IP from specified ports that using by other program. If a port is used by a program, is there any way I can listen that port and get the connected IP on that ...

Twisted Spread suitable for multiplayer racing sim?

Do you think that Twisted Spread may be suitable (in terms of performance) for a multiplayer racing simulator? The rest of the application is based on Python-Ogre. Can Perspective Broker run upon (...

Optimizing a LAN server for a game

I m the network programmer on a school game project. We want to have up to 16 players at once on a LAN. I am using the Server-Client model and am creating a new thread per client that joins. ...

multicast ip address - blocked in call to recvfrom

i am writing a simple multicast application. i intend to run it on localhost. i have done the following: char *maddr; . . . sendfd = socket(...); struct sockaddr_in sasend; sasend.sin_family = ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

热门标签