Redis Pub/Sub

2024. 2. 19. 12:52Spring Boot

통신 사이에 연결을 중계하는 Message Broker를 위하여 Redis는 Pub/Sub라는 기능을 제공한다

 

 

 

 

 

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;

@Configuration
@PropertySource("classpath:redis.properties")
public class redis_config {

    @Value("${host}")
    private String host;

    @Value("${port}")
    private int port;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(host, port);
    }

    @Qualifier("forPub")
    @Bean
    public RedisTemplate<String, String> redis_template() {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory());
        return template;
    }
}

 

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import io.lettuce.core.RedisClient;
import io.lettuce.core.pubsub.RedisPubSubListener;
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;

@Service
public class pubsub_service {

    RedisClient client = null;
    StatefulRedisPubSubConnection<String, String> connect = null;

    @Qualifier("forPub") 
    @Autowired
    RedisTemplate<String, String> template;

    public pubsub_service() {  
        client = RedisClient.create("redis://localhost:6379");

        connect = client.connectPubSub();

        connect.addListener(new RedisPubSubListener<String,String>() {

            @Override
            public void message(String channel, String message) {
                System.out.println("channel: " + channel + " Message: " + message);
            }

            @Override
            public void message(String pattern, String channel, String message) {
                System.out.println("pattern: " + pattern + " channel: " + channel + " Message: " + message);
            }

            @Override
            public void subscribed(String channel, long count) {
                System.out.println("channel: " + channel + " count: " + count);
            }

            @Override
            public void psubscribed(String pattern, long count) {
                System.out.println("patter: " + pattern + " count: " + count);
            }

            @Override
            public void unsubscribed(String channel, long count) {
                System.out.println("channel: " + channel + " count: " + count);
            }

            @Override
            public void punsubscribed(String pattern, long count) {
                System.out.println("patter: " + pattern + " count: " + count);
            }
            
        });
        
        RedisPubSubCommands<String, String> sync = connect.sync();
        sync.subscribe("ch01");
    }
    void test() {
        template.convertAndSend("ch01", "hello, test pub");
    }
}

 

 

 

 

redis-cli를 이용해 ch01 채널에 message를 publish 했더니

ch01을 subscribe 하고 있던 Spring boot에 message가 전달 되었다

 

 

 

 

 

이번에는 Spring boot에 test 메소드를 이용해여 ch01에 message를 publish했더니

ch01을 subscribe중인 redis에 message가 전달 되었다

'Spring Boot' 카테고리의 다른 글

Naver Login  (0) 2024.02.19
Web Socket By STOMP  (0) 2024.02.19
Elasticsearch  (0) 2024.02.13
Image File Handling  (0) 2024.02.03
Mulit Database by MySQL  (0) 2024.02.03