Spring Cloud Stream Message Driven Components Basic Tutorial (kafaka)

Keywords: Programming Spring Kafaka kafka github

This paper uses Spring cloud as 2.1.8 RELEASE, version=Greenwich.SR3

This paper is based on the implementation of the first two articles, eureka-server, eureka-client, eureka-ribbon and spring-gateway. Reference resources

Summary

Spring Cloud Stream is a framework for building message-driven microservices.It connects message proxy middleware and implements message event-driven microservice applications by using Spring Integration.Spring Cloud Stream provides personalized automated configuration implementations for some vendor's messaging middleware products, and introduces three core concepts: publish-subscribe, consumer group, and message partitioning.Simply put, Spring Cloud Stream is essentially a combination of Spring Boot and Spring Integration, which implements a lightweight message-driven micro-service framework.

1. Create a Spring Cloud Stream application: spring-cloud-stream

1.1 Increase pom dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>

1.2 Configure application.yml file

spring:
  application:
    name: spring-cloud-stream
  cloud:
    stream:
      kafaka:
        binder:
          brokers: 192.168.10.196:9092 #kafaka service address
          zk-noeds: 192.168.10.196:2181 #zk service address
          auto-create-topics: true
      bindings:
        output:  #output provided by stream default
          destination: stream-kafaka #Destination to which messages are sent
          content-type: text/plain #Message sending format. Receiver does not specify a format, but sender does.

server:
  port: 1000

1.3 Create Message Sending Service: KafakaSendService

package com.mm.spring.cloud.springcloudstream.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;

@EnableBinding(Source.class)
public class KafakaSendService {

    @Autowired
    private Source source;

    public void sendMsg(String msg) {
        source.output().send(MessageBuilder.withPayload(msg).build());
    }
}

1.4 Create call message Controller:KafakaProducerController

This KafakaProducerController is for easy demonstration

package com.mm.spring.cloud.springcloudstream.controller;

import com.mm.spring.cloud.springcloudstream.service.KafakaSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class KafakaProducerController {

    @Autowired
    private KafakaSendService kafakaSendService;

    @RequestMapping("/send/{msg}")
    public void send(@PathVariable String msg) {
        kafakaSendService.sendMsg(msg);
    }
}

1.5 Modify eureka-ribbon application

1.5.1 Increases pom dependence

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>

1.5.2 application.yml Add Configuration

spring:
  cloud:
    stream:
      kafka:
        binder:
          brokers: 192.168.10.196:9092
          auto-create-topics: true
      bindings:
        input:
          destination: stream-kafaka

1.5.3 New message processing class: RecieceService

package spring.cloud.demo.eurekaribbon.service;

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;

@EnableBinding(Sink.class)
public class RecieceService {

    @StreamListener(Sink.INPUT)
    public void recieve(Object payload) {
        System.out.println(payload);
    }
}

1.6 Start Services

Start the services of the eureka-server, eureka-client, eureka-ribbon, spring-cloud-stream application in sequence, then visit http://localhost:1000/send/maomao, and look at the console of eureka-ribbon to see as follows:

Prove that eureka-ribbon has received the message.

1.7 Summary

So far, a simple kafaka application for Spring Cloud Stream has been built using Stream's default Source and Ink methods.

summary

Spring Cloud Stream involves a lot of content, here is a simple implementation, so the "spring cloud 2.x version of the Small White Series Tutorial" has ended. The purpose of this small white series of tutorials is to give you a preliminary understanding and understanding of Spring cloud. In the future, I will update the advanced Spring cloud tutorial here. Please look forward to it and thank your friends.Support, thank you!!!

Code Address

gitHub address

<center><font color=red> Srping Cloud 2.X Small White Tutorial directory</font></center>

  • Writing is not easy. Please indicate the source for the reprint. Favorite partners can watch the public number to see more favorite articles.
  • Contact: 4272231@163.com

Posted by tcr480 on Sun, 24 Nov 2019 18:55:48 -0800