Получение ошибки Jedis при попытке подключиться с виртуальной машины к GCP cloudmemorystore

Я пытаюсь подключиться к GCP memorystore с той же виртуальной машины. Но получать

{"отметка времени":"2018-07-26T20:02:48.127+0000","статус":500,"ошибка": "Внутренняя ошибка сервера", "сообщение": "Ошибка подключения к пулу Jedis", "путь":"/ds/initiate"}, когда я пытаюсь подключить Redis напрямую с ВМ, он подключен и может PING и PONG, поэтому Redis подключен. но я получаю вышеуказанную ошибку. Когда вы устанавливаете Redis локально на ВМ, а затем запускаете приложение, приложение работает нормально, но не тогда, когда я пытаюсь подключиться с той же виртуальной машины к Memorystore IP, помогите

package com.example.demo;

import java.io.IOException;
import java.net.SocketException;
import java.util.Properties;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Controller 
@RequestMapping("/ds")
public class ControllerMain {

@GetMapping("/initiate")
public void checkhe(HttpServletRequest req , HttpServletResponse resp) throws IOException {

    try {
          JedisPool jedisPool = (JedisPool) req.getServletContext().getAttribute("jedisPool");

          if (jedisPool == null) {
            throw new SocketException("Error connecting to Jedis pool");
          }
          Long visits;

          try (Jedis jedis = jedisPool.getResource()) {
            visits = jedis.incr("visits");
          }

          resp.setStatus(HttpServletResponse.SC_OK);
          resp.getWriter().println("Visitor counter: " + String.valueOf(visits));
        } catch (Exception e) {
          resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }
}



private Properties config = new Properties();

private JedisPool createJedisPool() throws IOException {
  String host;
  Integer port;
  config.load(
      Thread.currentThread()
          .getContextClassLoader()
          .getResourceAsStream("application.properties"));
  host = config.getProperty("redis.host");
  port = Integer.valueOf(config.getProperty("redis.port", "6379"));

  JedisPoolConfig poolConfig = new JedisPoolConfig();
  // Default : 8, consider how many concurrent connections into Redis you will need under load
  poolConfig.setMaxTotal(128);

  return new JedisPool(poolConfig, host, port);
}

}

0 ответов

Другие вопросы по тегам