0

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

{"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);
}

}
4

0 回答 0