Creating a chat service to handle WebSocket traffic

If we visit http://start.spring.io, select Gradle, Spring Boot 2.0, Eureka Discovery, Config Client, Stream Rabbit, Lombok, and Reactive Web, we'll have a nice little service ready to chat:

    compile('org.springframework.boot:spring-boot-starter-webflux') 
    compile('org.projectlombok:lombok') 
    compile('org.springframework.cloud:spring-cloud-starter-stream-
rabbit') compile('org.springframework.cloud:spring-cloud-stream-reactive') compile('org.springframework.cloud:spring-cloud-starter-eureka') compile('org.springframework.cloud:spring-cloud-starter-config')

These aforementioned dependencies in our new chat service can be described as follows:

  • spring-boot-starter-webflux: This comes with a Reactive Streams capable WebSocket API
  • lombok: This is the library that gets us out of the business of coding getters, setters, and other boilerplate Java code
  • spring-cloud-starter-stream-rabbit: This is the Spring Cloud Stream library that uses RabbitMQ as the underlying technology
  • spring-cloud-stream-reactive: This layers on Reactive Streams support
  • spring-cloud-starter-eureka: This makes the microservice capable of registering itself with our Eureka Server and of consuming other Eureka-based services
  • spring-cloud-starter-config: This lets the microservice get its configuration details from the Config Server

There is little value in looking at the rest of the build file, since it's the same as our other microservices.

With these dependencies, the only thing needed to make this Yet Another Microservice is to fashion our Spring Boot public static void main like this:

    @SpringCloudApplication 
    @EnableEurekaClient 
    public class LearningSpringBootChatApplication { 
 
      public static void main(String[] args) { 
        SpringApplication.run( 
          LearningSpringBootChatApplication.class, args); 
      } 
    } 

The last code can be described quite simply:

  • @SpringCloudAppplication is a @SpringBootApplication combined with a Eureka Discovery, and with circuit breaker enabled

We're close. Early in this book, we would put the needed settings in application.yml (or application.properties), but since we have adopted Spring Cloud Config Server, we, instead, need to create the following bootstrap.yml file:

    spring: 
      application: 
        name: chat 

This bootstrap.yml file now identifies our application as the chat microservice to Eureka, and will cause it to ask the Config Server for chat.yml on startup.

To support that, we need to add the following to our Config Server's Git repository:

    server: 
      port: 8200 
 
    spring: 
      cloud: 
        stream: 
          bindings: 
            input: 
              destination: learning-spring-boot-chat 
              group: comments-chat 
              content-type: application/json 
            newComments: 
              destination: learning-spring-boot-chat 
              group: comments-chat 
              content-type: application/json 
            clientToBroker: 
              destination: learning-spring-boot-chat-user-messages 
              group: app-chatMessages 
            brokerToClient: 
              destination: learning-spring-boot-chat-user-messages 
              group: topic-chatMessages 

Wow! That's a lot of settings. Let's take them apart:

  • server.port shows this service will listen on port 8200. (Why not?)
  • spring.cloud.stream.bindings.input contains the exact same settings we saw earlier in the comments spring.cloud.stream.bindings.output settings. This ensures that the two are talking to each other.
  • We also have spring.cloud.stream.bindings.newComments, .clientToBroker, and .brokerToClient. This part is a little complex, so let's discuss what happens.
Before we dig into moving WebSocket messages around, don't forget to commit this change, and push to origin!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.187