尝试从VM连接到GCP cloudmemorystore时遇到Jedis错误

时间:2018-07-26 20:08:19

标签: redis google-cloud-platform google-cloud-memorystore

我正在尝试从同一VM连接GCP memorystore。但是得到

{“ timestamp”:“ 2018-07-26T20:02:48.127 + 0000”,“ status”:500,“ error”:“内部服务器错误”,“ message”:“连接到Jedis池时出错”, “ path”:“ / ds / initiate”},当我尝试直接从VM连接redis时,它已连接并且可以PING和PONG,所以redis已连接。但我收到上述错误。当在VM上本地安装Redis并运行该应用程序时,该应用程序运行正常,但是当我尝试从同一VM连接到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 个答案:

没有答案