Giter VIP home page Giter VIP logo

springboot_chatservice's Introduction

SpringBoot_ChatService

๐ŸŒฟ๐Ÿ’ฆ ์‹ค์‹œ๊ฐ„ ์ฑ„ํŒ…์„œ๋น„์Šค๋ฅผ ์œ„ํ•œ SrpingBoot REST API


Environment

  • Spring Boot 2.1.5.RELEASE
  • Java 1.8
  • spring-boot-starter-websocket
  • spring-kafka
  • guava

Tech Description

Review

WebSocketConfig.java

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");
        registry.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/chatting").setAllowedOrigins("*").withSockJS();
    }
}
  • configureMessageBroker : Message Broker์— ๋Œ€ํ•œ ์„ค์ •์„ ์ง„ํ–‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.
    • enableSimpleBroke : ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„ฃ์–ด ์ค€ ๊ฒฝ๋กœ๋กœ Simple Broker๋ฅผ ๋“ฑ๋กํ•œ๋‹ค. Simple Broker๋Š” ํ•ด๋‹น ๊ฒฝ๋กœ๋ฅผ Subscribeํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
    • setApplicationDestinationPrefixes : ํด๋ผ์ด์–ธํŠธ์—์„œ์˜ send ์š”์ฒญ์„ ์ฒ˜๋ฆฌ, ๋งŒ์•ฝ topic/hello ํ† ํ”ฝ์— ๋Œ€ํ•ด ๊ตฌ๋… ์‹ ์ฒญ์‹œ, ์‹ค์ œ ๊ฒฝ๋กœ๋Š” /app/topic/hello ๊ฐ€ ๋œ๋‹ค.
  • registerStompEndpoints : handshake์™€ ํ†ต์‹ ์„ ๋‹ด๋‹นํ•  endpoint๋ฅผ ์ง€์ •ํ•œ๋‹ค.๋˜ํ•œ setAllowedOrigins(*)๋ฅผ ํ†ตํ•ด ์›น์†Œ์ผ“ ํ†ต์‹ ์ด ์†Œ์ผ“ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ์‹œ์ ์ด ์•„๋‹ˆ๋”๋ผ๋„ ํ†ต์‹ ์ด ๊ฐ€๋Šฅ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด http://localhost:8080/stomp ๋กœ ์›น์†Œ์ผ“ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ–ˆ๋Š”๋ฐ (์„ค์ •์‹œ์ ) http://localhost ๋กœ ์›น์†Œ์ผ“ ํ†ต์‹ ์„ ์‹œ๋„ํ•˜๋ฉด (์š”์ฒญ์‹œ์ ) connect ์ด ์ด๋ค„์ง€์ง€ ์•Š๋Š”๋‹ค. ์ •ํ™•ํžˆ ํฌํŠธ๋ฒˆํ˜ธ๊นŒ์ง€ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์„ค์ •๋œ ๋„๋ฉ”์ธ ์™ธ์— ์—ฐ๊ฒฐ์„ ํ—ˆ์šฉํ•  ๋„๋ฉ”์ธ์„ ์ง€์ •ํ•ด์คŒ์œผ๋กœ์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค. ๋‹จ ์†Œ์ผ“ ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•œ ๋„๋ฉ”์ธ์—์„œ๋งŒ ์ฑ„ํŒ…์ด ์ด๋ฃจ์–ด์ง„๋‹ค๋ฉด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

Reciver.java

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

    @Autowired
    private SimpMessagingTemplate template;

    @KafkaListener(id = "main-listener", topics = "kafka-chatting")
    public void receive(ChattingMessage message) throws Exception{
        LOGGER.info("message='{}'", message);
        HashMap<String, String> msg = new HashMap<>();
        msg.put("timestamp", Long.toString(message.getTimeStamp()));
        msg.put("message", message.getMessage());
        msg.put("author", message.getUser());

        ObjectMapper mapper = new ObjectMapper(); // ์™œ ์“ฐ๋Š”๊ฑฐ์ง€?
        String json = mapper.writeValueAsString(msg);

        this.template.convertAndSend("/topic/public", json);
    }
}
  • ChattingMessage๋ฅผ HashMap์œผ๋กœ ๋ฐ”๊พธ๊ณ , ์ด๋ฅผ Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” Object mapper๋ฅผ ํ†ตํ•ด JSON ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ์ด๋ฅผ SimpMessagingTemplated์„ ํ†ตํ•ด /topic/public์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ธ๋‹ค.

ChattingHistoryDAO

@Component
public class ChattingHistoryDAO {

    private final Cache<UUID, ChattingMessage> chatHistoryCache = CacheBuilder
            .newBuilder().maximumSize(20).expireAfterWrite(10, TimeUnit.MINUTES)
            .build();

    public void save(ChattingMessage chatObj){
        this.chatHistoryCache.put(UUID.randomUUID(), chatObj);
    }

    public List<ChattingMessage> get(){
        return chatHistoryCache.asMap().values().stream()
                .sorted(Comparator.comparing(ChattingMessage::getTimeStamp))
                .collect(Collectors.toList());
    }
}
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋Œ€์‹  guava์—์„œ ์ œ๊ณตํ•˜๋Š” ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ €์žฅํ•œ๋‹ค.

Reference

https://dydtjr1128.github.io/spring/2019/05/26/Springboot-react-chatting.html

springboot_chatservice's People

Contributors

koogk7 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.