Cookie Consent by Free Privacy Policy Generator 📌 Introduction to Spring Cloud Function and Spring Cloud Stream

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Introduction to Spring Cloud Function and Spring Cloud Stream


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

What is Streaming and Stream processing?

Event Stream Processing (ESP) is the practice of taking action on a series of data points/events (stream) that originate from a system that continuously creates data.

Stream processing can typically be represented using a DAG:

Stream Processing DAG

This is a very high level abstraction. Can we model it using something we already have in Java? Yes — Java8 Functions!

Stream Processing DAG — Java Functional Toolkit

Spring Cloud Function

Spring Cloud Function is a framework that promotes the implementation of business logic as Java Functions further instrumenting them to execute in context of different runtimes (HTTP, Messaging, etc.)!

Example — Let’s create a function that turns a string into uppercase:

public Function<String, String> toUpper() {
    return (str) -> {
        System.out.println("Original: " + str);
        return str.toUpperCase();
    };
}

If you are using spring initializer, and simply add the Spring Cloud Function dependency, it will add the spring-cloud-function-context dependency. If you have the Spring Web dependency in your project and add the Spring Cloud Function dependency, it will add the spring-cloud-function-web dependency i.e. we will be able to expose the above function as a rest endpoint (i.e this function is automatically bound to an HTTP context). Let’s see it in action!

//Returns a Function (A Processor node -> has both Input and Output!)
@Bean
public Function<String, String> toUpper() {
    return (str) -> {
        System.out.println("Original: " + str);
        return str.toUpperCase();
    };
}

Start your application and invoke this function at:

localhost:/toUpper/ishan

Output:

Spring Cloud Function Output

With Spring Cloud Function, we were successfully able to bind this function to an HTTP context!

Spring Cloud Stream

A framework that instruments your Spring Cloud Functions as event handlers bound to destinations using binders specific to a particular messaging system. Also see — why do you need a messaging system?

Eg — You want your Spring Cloud Function to become a message handler for a RabbitMQ destination(queue)!

Setup

Add the spring-cloud-stream-binder-rabbit dependency.

spring-cloud-stream-binder-rabbit dependency tree

Bring up RabbitMQ using docker-compose

---
version: “3.8”
services:
  rabbitmq:
    image: rabbitmq:3.11-management-alpine
    container_name: rabbitmq
    ports:
      - 5672:5672
      - 15672:15672
    environment:
      RABBITMQ_DEFAULT_USER: guest
      RABBITMQ_DEFAULT_PASS: guest
      RABBITMQ_DEFAULT_VHOST: /

RabbitMQ before

Connect RabbitMQ to your spring application (application.properties/yaml)

rabbitmq.host=localhost
rabbitmq.port=5672
rabbitmq.username=guest
rabbitmq.password=guest

When you bring the application up, you’ll see two new exchanges!

RabbitMQ after

If you drop a message in toUpper-in-0 exchange, your toUpper function will get invoked and the output will be pushed to toUpper-out-0 exchange! Note — The REST access to this function will still work if you also have the spring-cloud-function-web dependency!

Note — A Function is a processor. That is why it created 2 exchanges! It listens to toUpper-in-0 and sends the output to toUpper-out-0

Let’s create a consumer for toUpper-out-0 exchange:

@Bean
public Consumer<String> consumeUpper() {
    return (upper) -> {
        System.out.println("Consumed: " + upper);
    };
}
# Spring Cloud Functions defined in your application
# Define multiple functions definitions by using ;
spring.cloud.function.definition=toUpper;consumeUpper

# Based on the Function definitions, Spring Cloud Stream will create bindings
# which connects the Function's input and/or output to destinations! 
# The toUpper-in-0 (the toUpper function listens to this)
# and toUpper-out-0 (the toUpper function produces results to this) were 
# automatically created

# If you do not do any configuration for the consumeUpper function,
# it will automatically create a consumeUpper-in-0 exchange and listen 
# to that. But we want this function to listen to toUpper-out-0
# What we are saying below is ->
# Bind consumeUpper-in-0 binding to listen to the toUpper-out-0 destination
# Since this is a Consumer function, the binding that is created is consumeUpper-in-0
spring.cloud.stream.bindings.consumeUpper-in-0.destination=toUpper-out-0
spring.cloud.stream.bindings.consumeUpper-in-0.group=school-service

The concept of groups is similar to consumer groups in Kafka. But more on that in another article.

Let’s drop the message “Ishan” into the exchange “toUpper-in-0” using the RabbitMQ admin console. You’ll see the following in your application logs:

From the toUpper function

From the consumeUpper function

Producer

You can directly produce data to a destination using the StreamBridge class:

@Autowired
private StreamBridge streamBridge;

...

streamBridge.send(Destination, data);

or you can use a Supplier

Supplier is a little different that a Function or a Consumer. Functions/Consumers are triggered whenever there is an input. But a Supplier doesn’t have a trigger. In Spring Cloud Function, the Supplier has an automated polling mechanism which polls the supplier every second (which you can override)

@Bean
public Supplier<String> createData() {
    return () -> {
        System.out.println("Creating some data");
        return "Ishan-" +
                ThreadLocalRandom.current().nextInt(0, 10000) +
                "-" +
                LocalDateTime.now();
    };
}
spring.cloud.function.definition=toUpper;consumeUpper;createData

...

# Modify the poll interval of the supplier
spring.cloud.stream.poller.fixed-delay=10000

# The binding destination of the supplier function.
# If we had provided no configuration, spring cloud stream would have
# automatically created a createData-out-0 exchange and the data this
# supplier creates will be pushed to that exchange. But we want this
# supplier to push data to the toUpper-in-0 exchange
spring.cloud.stream.bindings.createData-out-0.destination=toUpper-in-0

Restart your application again:

Output

Sending and Consuming data based on specific binding and routing keys

If you are unfamiliar with RabbitMQ routing and binding keys, see this article first.

Example — You are using StreamBridge to send messages to a students exchange. How do you set the routing key of the message?

spring.cloud.stream.rabbit.bindings.students.producer.routingKeyExpression=headers['type']

What you are saying is, treat the type header as a routing key

import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;

...

Message<StudentCreated> message = MessageBuilder.withPayload(studentCreated)
        .setHeader("type", "student.created").build();
streamBridge.send("students", message);

Now, how do you bind the below consumer and the queue it listens to a specific binding key on an exchange? (i.e this consumer only wants to listen to StudentCreated events from the students exchange)

@Bean
public Consumer<StudentCreated> studentCreated() {
  ...
}
spring.cloud.function.definition=studentCreated
spring.cloud.stream.bindings.studentCreated-in-0.destination=students
spring.cloud.stream.bindings.studentCreated-in-0.group=school-service
#IMP
spring.cloud.stream.rabbit.bindings.studentCreated-in-0.consumer.bindingRoutingKey=student.created
spring.cloud.stream.rabbit.bindings.studentCreated-in-0.consumer.bindingRoutingKeyDelimiter=,
...



📌 Introduction to Spring Cloud Function and Spring Cloud Stream


📈 51.54 Punkte

📌 Introduction to Spring Scheduled and monitoring the task with Spring Actuator ️🕛️📈️


📈 29.62 Punkte

📌 software-architektur.tv: GraalVM mit Spring Native, Spring Boot und Spring Cloud


📈 28.69 Punkte

📌 Microsoft and VMware: Supporting Spring and Open Source Developers | Azure Spring Cloud GA


📈 23.63 Punkte

📌 Function declaration and Function Expression


📈 23.06 Punkte

📌 [Question] It is possible to jump to a function with BL, store value stored in x1 and return it in the first function ?


📈 23.06 Punkte

📌 Differences between arrow function and regular function in JavaScript


📈 23.06 Punkte

📌 How to Create and Test an HTTP-Triggered Function with Azure Function App


📈 23.06 Punkte

📌 Building an Address Search Function with Amazon Location SDK and API key function


📈 23.06 Punkte

📌 Make the OpenAI Function Calling Work Better and Cheaper with a Two-Step Function Call 🚀


📈 23.06 Punkte

📌 Spring Cloud Config up to 1.4.5/2.0.3/2.1.1 spring-cloud-config-server directory traversal


📈 22.85 Punkte

📌 Accelerate Spring apps to cloud at scale – A discussion with Azure Spring Cloud customers


📈 22.85 Punkte

📌 Spring Cloud Config up to 2.1.8/2.2.2 spring-cloud-config-server path traversal


📈 22.85 Punkte

📌 Medium CVE-2022-22979: Vmware Spring cloud function


📈 22.07 Punkte

📌 CVE-2022-22963 | VMware Spring Cloud Function up to 3.1.6/3.2.2 SpEL Expression code injection


📈 22.07 Punkte

📌 Introduction to Spring Cloud Kubernetes


📈 22 Punkte

📌 Rapidly dev and deploy Spring apps to Azure Spring Cloud


📈 21.84 Punkte

📌 Modifing XKB to disable function keys, Have yet to be successful in disabling any of the function keys at all.


📈 21.28 Punkte

📌 Arrow Function vs Function


📈 21.28 Punkte

📌 Function Declaration vs Function Expression


📈 21.28 Punkte

📌 When to Use a Function Expression vs. Function Declaration


📈 21.28 Punkte

📌 Function declarations & Function expressions


📈 21.28 Punkte

📌 Introduction to Lambda Function URLs


📈 21.21 Punkte

📌 Introduction to format string vulnerabilities - Introduction to Binary Exploitation - Hack The Box Leet Test


📈 21.15 Punkte

📌 Gemini-1.5-pro Function Calling with Java, Spring and HTTP


📈 21.05 Punkte

📌 Azure Spring Cloud - a fully managed service for Spring Boot apps


📈 20.06 Punkte

📌 Introducing Azure Spring Cloud: fully managed service for Spring Boot microservices


📈 20.06 Punkte

📌 Introducing Azure Spring Cloud: fully managed service for Spring Boot microservices


📈 20.06 Punkte

📌 Und Microsoft so: Cloud, Cloud, Cloud, Cloud, Cloud, Cloud, Cloud


📈 19.56 Punkte

📌 Spring Boot 3 application on AWS Lambda - Part 2 Introduction to AWS Serverless Java Container


📈 19.21 Punkte

📌 Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl


📈 19.05 Punkte

📌 Querydsl vs. JPA Criteria, Part 6: Upgrade Guide To Spring Boot 3.2 for Spring Data JPA and Querydsl Project


📈 19.05 Punkte

📌 Learn Spring Boot and Spring Data JPA


📈 19.05 Punkte

📌 Vuln: Spring Security and Spring Framework CVE-2018-1258 Authorization Bypass Vulnerability


📈 19.05 Punkte

📌 Build CRUD RESTful API Using Spring Boot 3, Spring Data JPA, Hibernate, and MySQL Database


📈 19.05 Punkte











matomo