Q & A
Q & A
soln: */2 * * * *
if (myThread.isAlive()) {
System.out.println("The thread is still running.");
} else {
System.out.println("The thread has completed.");
}
Q) How to add another server in spring boot?
By using exclusion first we can exclude exsting server and add new server
dependency
<dependency>
<groupid>org.springframework.boot</>
<artifactid>spring-boot-starter-web</>
<exclusions>
<exclusion>
<groupid>org.springframework.boot</>
<artifactid>spring-boot-starter-tomcat</>
</exclusion> </exclusions>
</dependency>
Q) how to handle exceptions in spring boot ?
we use this for custom exceptions @ExceptionHandler(CustomException.class)
Q) How to connect multiple microservices
we two types 1.synhronous and 2.asynchronous
1.by using rest template and feign
2.by using Message Brokers like Apache Kafka,Active MQ or
RabbitMQ etc.
Q) Connecting multiple microservices using Apache Kafka
Set up Apache Kafka:
Install and set up Apache Kafka on your local machine or
infrastructure.
Create a Kafka topic that will be used for communication between
microservices.
Add Kafka dependency:the following dependency to your pom.xml (if using
Maven)
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
Configure Kafka connection:
In each microservice, add the following properties to your
application.properties or application.yml file, specifying the connection details
for Apache Kafka: in yaml file
spring.kafka.bootstrap-servers=localhost:9092
Create a message producer:
In the microservice that wants to send messages, create a component or
service that will act as the message producer.
Create a message consumer:
In the microservice that wants to receive messages, create a component
or service that will act as the message consumer.
The @KafkaListener annotation is used to indicate that this method
should listen for messages from the specified topic, which is configured in the
application properties.
Send and receive messages:
In your microservice code, you can now use the MessageProducer
component to send messages and the MessageConsumer component to receive messages.
By following these steps, you can establish communication between multiple
Spring microservices using Apache Kafka as the messaging system. The message
producer sends messages to a specified Kafka topic, and the message consumer
listens for and processes messages from the same topic.