C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
Photo
βΆοΈ Dependency for implementing Circuit Breaker using Resilience4j :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
βΆοΈ Dependency for implementing Circuit Breaker using Resilience4j : <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId> </dependency>
βΆοΈ Properties needed to define in application.properties file to implement circuit breaker using Resilience4j :
#Resilinece4j Properties
resilience4j.circuitbreaker.configs.default.registerHealthIndicator=true
resilience4j.circuitbreaker.configs.default.slidingWindowType=COUNT_BASED
resilience4j.circuitbreaker.configs.default.slidingWindowSize=10
resilience4j.circuitbreaker.configs.default.failureRateThreshold=50
resilience4j.circuitbreaker.configs.default.waitDurationInOpenState=5s
resilience4j.circuitbreaker.configs.default.permittedNumberOfCallsInHalfOpenState=3
resilience4j.circuitbreaker.configs.default.automaticTransitionFromOpenToHalfOpenEnabled=true
resilience4j.circuitbreaker.configs.default.minimum-number-of-calls=5
#Resilience4J Timeout Properties
resilience4j.timelimiter.configs.default.timeout-duration=3s
#Resilience4J Retry Properties
resilience4j.retry.configs.default.max-attempts=3
resilience4j.retry.configs.default.wait-duration=2s
#Resilinece4j Properties
resilience4j.circuitbreaker.configs.default.registerHealthIndicator=true
resilience4j.circuitbreaker.configs.default.slidingWindowType=COUNT_BASED
resilience4j.circuitbreaker.configs.default.slidingWindowSize=10
resilience4j.circuitbreaker.configs.default.failureRateThreshold=50
resilience4j.circuitbreaker.configs.default.waitDurationInOpenState=5s
resilience4j.circuitbreaker.configs.default.permittedNumberOfCallsInHalfOpenState=3
resilience4j.circuitbreaker.configs.default.automaticTransitionFromOpenToHalfOpenEnabled=true
resilience4j.circuitbreaker.configs.default.minimum-number-of-calls=5
#Resilience4J Timeout Properties
resilience4j.timelimiter.configs.default.timeout-duration=3s
#Resilience4J Retry Properties
resilience4j.retry.configs.default.max-attempts=3
resilience4j.retry.configs.default.wait-duration=2s
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
Photo
βΆοΈ What is circuit breaker and why to use it :
In our microservices application sometimes if one service stops working then it is waste of resources and time to call the entire services and get us back that a service is not working it is completely a waste of time and resources, so to avoid that we apply circuit breaker at the API gateway level so that we don't have to waste resources and time as api gateway is the entry point of any microservices application,so that at just entry level it shows that our service is not working,so to implement the functionality like that,to save resources and time,we need and use circuit breaker.
In our microservices application sometimes if one service stops working then it is waste of resources and time to call the entire services and get us back that a service is not working it is completely a waste of time and resources, so to avoid that we apply circuit breaker at the API gateway level so that we don't have to waste resources and time as api gateway is the entry point of any microservices application,so that at just entry level it shows that our service is not working,so to implement the functionality like that,to save resources and time,we need and use circuit breaker.
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
βΆοΈ What is circuit breaker and why to use it : In our microservices application sometimes if one service stops working then it is waste of resources and time to call the entire services and get us back that a service is not working it is completely a wasteβ¦
βΆοΈ There are 3 states of circuit breaker :
1. Open
2. Closed
3. Half Open
1. Open
2. Closed
3. Half Open
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
βΆοΈ There are 3 states of circuit breaker : 1. Open 2. Closed 3. Half Open
#How to add kafka in our project as a container through docker:
1.a) Add this property in docker-compose.yml file located in your root folder of your project or particular module :
zookeeper:
image: confluentinc/cp-zookeeper:7.5.0
hostname: zookeeper
container_name: zookeeper
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
broker:
image: confluentinc/cp-kafka:7.5.0
container_name: broker
ports:
- "9092:9092"
- "29092:29092"
depends_on:
- zookeeper
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
schema-registry:
image: confluentinc/cp-schema-registry:7.5.0
hostname: schema-registry
container_name: schema-registry
depends_on:
- broker
ports:
- "8085:8081"
environment:
SCHEMA_REGISTRY_HOST_NAME: schema-registry
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'broker:29092'
SCHEMA_REGISTRY_LISTENERS: http://schema-registry:8081
kafka-ui:
container_name: kafka-ui
image: provectuslabs/kafka-ui:latest
ports:
- "8086:8080"
depends_on:
- broker
environment:
KAFKA_CLUSTERS_NAME: local
KAFKA_CLUSTERS_BOOTSTRAPSERVERS: broker:29092
#KAFKA_CLUSTERS_SCHEMAREGISTRY: http://schema-registry:8081
DYNAMIC_CONFIG_ENABLED: 'true'
b) After that open terminal and go to where ur docker-compose.yml file is located and then run this command in terminal :
docker compose up -d
This will create a container of kafka in our project which we can use easily.
1.a) Add this property in docker-compose.yml file located in your root folder of your project or particular module :
zookeeper:
image: confluentinc/cp-zookeeper:7.5.0
hostname: zookeeper
container_name: zookeeper
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
broker:
image: confluentinc/cp-kafka:7.5.0
container_name: broker
ports:
- "9092:9092"
- "29092:29092"
depends_on:
- zookeeper
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
schema-registry:
image: confluentinc/cp-schema-registry:7.5.0
hostname: schema-registry
container_name: schema-registry
depends_on:
- broker
ports:
- "8085:8081"
environment:
SCHEMA_REGISTRY_HOST_NAME: schema-registry
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'broker:29092'
SCHEMA_REGISTRY_LISTENERS: http://schema-registry:8081
kafka-ui:
container_name: kafka-ui
image: provectuslabs/kafka-ui:latest
ports:
- "8086:8080"
depends_on:
- broker
environment:
KAFKA_CLUSTERS_NAME: local
KAFKA_CLUSTERS_BOOTSTRAPSERVERS: broker:29092
#KAFKA_CLUSTERS_SCHEMAREGISTRY: http://schema-registry:8081
DYNAMIC_CONFIG_ENABLED: 'true'
b) After that open terminal and go to where ur docker-compose.yml file is located and then run this command in terminal :
docker compose up -d
This will create a container of kafka in our project which we can use easily.
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
#How to add kafka in our project as a container through docker: 1.a) Add this property in docker-compose.yml file located in your root folder of your project or particular module : zookeeper: image: confluentinc/cp-zookeeper:7.5.0 hostname: zookeeperβ¦
2. Add kafka dependency as a dependency in your spring project :
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
2. Add kafka dependency as a dependency in your spring project : <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
3. Add these properties in application.properties file of your request producer service module :
#Kafka Properties.
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.template.default-topic=order-placed(topic_written_in_service_layer)
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
#Kafka Properties.
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.template.default-topic=order-placed(topic_written_in_service_layer)
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
3. Add these properties in application.properties file of your request producer service module : #Kafka Properties. spring.kafka.bootstrap-servers=localhost:9092 spring.kafka.template.default-topic=order-placed(topic_written_in_service_layer) spring.kafka.producer.keyβ¦
4. Dependencies to enable avro and schema-registry in our project(In Request Producer module/part of project):-
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-avro-serializer</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-client</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.11.3</version>
</dependency>
Also add plugin as follows :
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-avro-serializer</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-client</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.11.3</version>
</dependency>
Also add plugin as follows :
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
5. After that go to start.spring.io website download project with following dependencies and settings and plugins :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
5. After that go to start.spring.io website download project with following dependencies and settings and plugins : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> β¦
<build>
<plugins>
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<plugins>
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
5. After that go to start.spring.io website download project with following dependencies and settings and plugins : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> β¦
6. After downloading the Kafka consumer module(here we named it as notification-service),After that add these properties to your project's module's application.properties file(consumer's application.properties file) :
spring.application.name=notification-service
server.port=8084
#Mail Properties
spring.mail.host=sandbox.smtp.mailtrap.io
spring.mail.port=2525
spring.mail.username=a2adfdfdca22a5
spring.mail.password=f470820b1b0fad
#Kafka Consumer properties
spring.kafka.bootstrap-service=localhost:9092
spring.kafka.consumer.group-id=notificationService
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.trusted.packages=com.techie.microservices.order.event
spring.application.name=notification-service
server.port=8084
#Mail Properties
spring.mail.host=sandbox.smtp.mailtrap.io
spring.mail.port=2525
spring.mail.username=a2adfdfdca22a5
spring.mail.password=f470820b1b0fad
#Kafka Consumer properties
spring.kafka.bootstrap-service=localhost:9092
spring.kafka.consumer.group-id=notificationService
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.trusted.packages=com.techie.microservices.order.event
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
6. After downloading the Kafka consumer module(here we named it as notification-service),After that add these properties to your project's module's application.properties file(consumer's application.properties file) : spring.application.name=notificationβ¦
7. Go to project open in intellij/eclipse/SpringToolSuite and there make a package as order.event, there place this code with required variables as per your requirements and it should either match with schema defined in file defined as order-placed.avsc in avro folder in application.properties folder of module or with class defined with the same folder structure as in consumer's module as follows :
package com.techie.microservices.order.event;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderPlacedEvent {
private String orderNumber;
private String email;
}
or
Schema-registry file defined as order-placed.avsc in avro folder in application.properties folder of module:
{
"type": "record",
"name": "OrderPlacedEvent",
"namespace": "com.techie.microservices.order.event",
"fields": [
{ "name": "orderNumber", "type": "string" },
{ "name": "email", "type": "string" },
{ "name": "firstName", "type": "string" },
{ "name": "lastName", "type": "string" }
]
}
package com.techie.microservices.order.event;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderPlacedEvent {
private String orderNumber;
private String email;
}
or
Schema-registry file defined as order-placed.avsc in avro folder in application.properties folder of module:
{
"type": "record",
"name": "OrderPlacedEvent",
"namespace": "com.techie.microservices.order.event",
"fields": [
{ "name": "orderNumber", "type": "string" },
{ "name": "email", "type": "string" },
{ "name": "firstName", "type": "string" },
{ "name": "lastName", "type": "string" }
]
}
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
7. Go to project open in intellij/eclipse/SpringToolSuite and there make a package as order.event, there place this code with required variables as per your requirements and it should either match with schema defined in file defined as order-placed.avsc inβ¦
8. After that make a package named as service(you can keep it anything but most preferably you should write the name as service as it is providing service of sending mail) and then make a class in it(here it's NotificationService) and write the following code to enable consumer to consume the request and act accordingly here sending mail) like this :
package com.techie.microservices.notification.service;
import com.techie.microservices.order.event.OrderPlacedEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationService {
private final JavaMailSender javaMailSender;
@KafkaListener(topics = "order-placed")
public void listen(OrderPlacedEvent orderPlacedEvent){
log.info("Got Message from order-placed topic {}",orderPlacedEvent);
// Send email to the customer
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("springshop@gmail.com");
messageHelper.setTo(orderPlacedEvent.getEmail());
messageHelper.setSubject(String.format("Your Order with OrderNumber %s is placed successfully",orderPlacedEvent.getOrderNumber()));
messageHelper.setText(String.format("""
Hi
Any msg in the body(here i'm providing message as Your order with order number %s is now placed successfully)
Best Regards
Spring Shop
""",
Any Dynamic msg you want to print in as %s variable Name(here it's orderPlacedEvent.getOrderNumber()));
};
try{
javaMailSender.send(messagePreparator);
log.info("Order Notification email sent!!");
}catch(MailException e){
log.error("Exception occurred when sending mailed");
throw new RuntimeException("Exception occured when sending mail to springshop@email.com",e);
}
}
}
package com.techie.microservices.notification.service;
import com.techie.microservices.order.event.OrderPlacedEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationService {
private final JavaMailSender javaMailSender;
@KafkaListener(topics = "order-placed")
public void listen(OrderPlacedEvent orderPlacedEvent){
log.info("Got Message from order-placed topic {}",orderPlacedEvent);
// Send email to the customer
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("springshop@gmail.com");
messageHelper.setTo(orderPlacedEvent.getEmail());
messageHelper.setSubject(String.format("Your Order with OrderNumber %s is placed successfully",orderPlacedEvent.getOrderNumber()));
messageHelper.setText(String.format("""
Hi
Any msg in the body(here i'm providing message as Your order with order number %s is now placed successfully)
Best Regards
Spring Shop
""",
Any Dynamic msg you want to print in as %s variable Name(here it's orderPlacedEvent.getOrderNumber()));
};
try{
javaMailSender.send(messagePreparator);
log.info("Order Notification email sent!!");
}catch(MailException e){
log.error("Exception occurred when sending mailed");
throw new RuntimeException("Exception occured when sending mail to springshop@email.com",e);
}
}
}
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
8. After that make a package named as service(you can keep it anything but most preferably you should write the name as service as it is providing service of sending mail) and then make a class in it(here it's NotificationService) and write the following codeβ¦
9. After that Start all the services and it will work fine as required, sending emails whenever order event is triggered and request is send from the request producer to request consumer.
C,C++,JAVA,SPRING,R,PYTHON,SQL Developer, Javascript MOTIVATION and programming & coding channel.π»π»
#How to add kafka in our project as a container through docker: 1.a) Add this property in docker-compose.yml file located in your root folder of your project or particular module : zookeeper: image: confluentinc/cp-zookeeper:7.5.0 hostname: zookeeperβ¦