Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导Redis配置不工作_Java_Spring Boot_Redis - Fatal编程技术网

Java Spring引导Redis配置不工作

Java Spring引导Redis配置不工作,java,spring-boot,redis,Java,Spring Boot,Redis,我正在开发一个带有servletializer的springboot[web]REST风格的应用程序(因为它需要部署到现有的Tomcat服务器上)。它有一个@RestController,其方法在调用时需要写入Redis发布子频道。我在本地主机上运行Redis服务器(默认端口,无密码)。POM文件的相关部分具有所需的启动程序依赖项: <dependency> <groupId>org.springframework.boot</groupId>

我正在开发一个带有
servletializer
的springboot[web]REST风格的应用程序(因为它需要部署到现有的Tomcat服务器上)。它有一个
@RestController
,其方法在调用时需要写入Redis
发布子频道
。我在本地主机上运行Redis服务器(默认端口,无密码)。POM文件的相关部分具有所需的启动程序依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
我在Spring Boot应用程序类中添加了以下内容:

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    return new JedisConnectionFactory();
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(jedisConnectionFactory());
    return template;
}
编辑(2017-05-09) 我的理解是,Spring Boot Redis starter假定默认值为
Spring.Redis.host=localhost
Spring.Redis.port=6379
,我仍然将这两个值添加到
application.properties
,但这并没有填补空白

更新(2017-05-10)
我添加了此线程的答案。

您需要使用应用程序配置redis服务器信息。属性:

# REDIS (RedisProperties)
spring.redis.cluster.nodes= # Comma-separated list of "host:port"
spring.redis.database=0 # Database index
spring.redis.url= # Connection URL, 
spring.redis.host=localhost # Redis server host.
spring.redis.password= # Login password of the redis server.
spring.redis.ssl=false # Enable SSL support.
spring.redis.port=6379 # Redis server port.

Spring数据文档:

这是一个与代理相关的问题,甚至连对本地主机的访问都受到了某种程度的限制。一旦我禁用了代理设置,Redis health就启动了!所以问题就解决了。我不必向
应用程序.properties添加任何属性,也不必在Spring Boot应用程序类中显式配置任何内容,因为Spring Boot和Redis Starter会根据Redis默认值自动配置(在我的开发环境中适用)。我刚刚将以下内容添加到
pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
要将简单消息发布到频道,这一行代码足以验证设置:

this.redisTemplate.convertAndSend(channelName, "hello world");

我感谢所有的评论,这些评论有助于支持我的支票

我用redis和spring boot做了一个简单的例子

首先,我在docker上安装了redis:

$docker run——说出一些redis-d redis服务器的名称——appendonly yes

然后我将此代码用于接收器:

import java.util.concurrent.CountDownLatch;

public class Receiver {
    private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

    private CountDownLatch latch;

    @Autowired
    public Receiver(CountDownLatch latch) {
        this.latch = latch;
    }

    public void receiveMessage(String message) {
        LOGGER.info("Received <" + message + ">");
        latch.countDown();
    }

}
import java.util.concurrent.CountDownLatch;
公共类接收器{
私有静态最终记录器Logger=LoggerFactory.getLogger(Receiver.class);
私人倒计时闩锁;
@自动连线
公用接收器(倒计时闩锁){
this.latch=闩锁;
}
公共无效接收消息(字符串消息){
LOGGER.info(“已收到”);
倒计时();
}
}
这是我的spring boot应用程序和我的侦听器:

@SpringBootApplication
// after add security library then it is need to use security configuration.
@ComponentScan("omid.spring.example.springexample.security")
public class RunSpring {
    private static final Logger LOGGER = LoggerFactory.getLogger(RunSpring.class);


    public  static   void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext contex =  SpringApplication.run(RunSpring.class, args);
    }

    @Autowired
    private ApplicationContext context;

    @RestController
    public class SimpleController{

        @RequestMapping("/test")
        public String getHelloWorld(){

            StringRedisTemplate template = context.getBean(StringRedisTemplate.class);
            CountDownLatch latch = context.getBean(CountDownLatch.class);

            LOGGER.info("Sending message...");

            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0 ;  i < 100 ; i++) {
                        template.convertAndSend("chat", i + " => Hello from Redis!");
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }

                }
            });
            t.start();

            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            return "hello world 1";
        }
    }

    ///////////////////////////////////////////////////////////////


    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("chat"));

        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    @Bean
    Receiver receiver(CountDownLatch latch) {
        return new Receiver(latch);
    }

    @Bean
    CountDownLatch latch() {
        return new CountDownLatch(1);
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }
}
@springboot应用程序
//添加安全库后,需要使用安全配置。
@组件扫描(“omid.spring.example.springexample.security”)
公共类RunSpring{
私有静态最终记录器Logger=LoggerFactory.getLogger(RunSpring.class);
公共静态void main(字符串[]args)引发InterruptedException{
ConfigurableApplicationContext contex=SpringApplication.run(RunSpring.class,args);
}
@自动连线
私有应用程序上下文上下文;
@RestController
公共类SimpleController{
@请求映射(“/test”)
公共字符串getHelloWorld(){
StringRedisTemplate=context.getBean(StringRedisTemplate.class);
CountDownLatch-latch=context.getBean(CountDownLatch.class);
LOGGER.info(“发送消息…”);
线程t=新线程(新的可运行线程(){
@凌驾
公开募捐{
对于(int i=0;i<100;i++){
convertAndSend(“聊天室”,i+“=>来自Redis的你好!”);
试一试{
睡眠(100);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
});
t、 start();
试一试{
satch.wait();
}捕捉(中断异常e){
e、 printStackTrace();
}
返回“你好,世界1”;
}
}
///////////////////////////////////////////////////////////////
@豆子
RedisMessageListenerContainer容器(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter){
RedisMessageListenerContainer=新的RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
addMessageListener(listenerAdapter,新模式主题(“聊天”);
返回容器;
}
@豆子
MessageListenerAdapter listenerAdapter(接收方){
返回新的MessageListenerAdapter(接收方,“receiveMessage”);
}
@豆子
接收器(倒计时闩锁闩锁){
返回新的接收器(闩锁);
}
@豆子
倒计时闩锁(){
返回新的倒计时锁存器(1);
}
@豆子
StringRedisTemplate模板(RedisConnectionFactory connectionFactory){
返回新的StringRedisTemplate(connectionFactory);
}
}
重要的一点是redis IP。如果你像我一样安装在docker上那么 您应该在application.properties中设置ip地址,如下所示: spring.redis.host=172.17.0.4

我将所有spring示例都放在github上

此外,我还使用redis stat来监视redis。这是简单的监控。

由于我在本地使用Redis(新安装,没有配置更改),我认为Spring Boot Redis starter采用了这些默认设置。我没有使用集群。唯一适用的Redis属性是
spring.Redis.host
(localhost)和
spring.Redis.port
(6379)。您的Redis服务正在运行吗。您是否能够通过redis cli连接该服务?您的代码看起来不错…您好@pranestramesh我的redis服务器正在运行。我运行的验证包括
telnet localhost 6379
(能够将telnet连接到该端口)和编写基于测试GET的方法
this.redisTemplate.convertAndSend(channelName, "hello world");
import java.util.concurrent.CountDownLatch;

public class Receiver {
    private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

    private CountDownLatch latch;

    @Autowired
    public Receiver(CountDownLatch latch) {
        this.latch = latch;
    }

    public void receiveMessage(String message) {
        LOGGER.info("Received <" + message + ">");
        latch.countDown();
    }

}
@SpringBootApplication
// after add security library then it is need to use security configuration.
@ComponentScan("omid.spring.example.springexample.security")
public class RunSpring {
    private static final Logger LOGGER = LoggerFactory.getLogger(RunSpring.class);


    public  static   void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext contex =  SpringApplication.run(RunSpring.class, args);
    }

    @Autowired
    private ApplicationContext context;

    @RestController
    public class SimpleController{

        @RequestMapping("/test")
        public String getHelloWorld(){

            StringRedisTemplate template = context.getBean(StringRedisTemplate.class);
            CountDownLatch latch = context.getBean(CountDownLatch.class);

            LOGGER.info("Sending message...");

            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0 ;  i < 100 ; i++) {
                        template.convertAndSend("chat", i + " => Hello from Redis!");
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }

                }
            });
            t.start();

            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            return "hello world 1";
        }
    }

    ///////////////////////////////////////////////////////////////


    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("chat"));

        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    @Bean
    Receiver receiver(CountDownLatch latch) {
        return new Receiver(latch);
    }

    @Bean
    CountDownLatch latch() {
        return new CountDownLatch(1);
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }
}