Share
Embed
Microservices.pdf
Coding Saint
May 22, 2020
Transcript
64 slides
-
user service product service customer service cart service payment service
Monolith Load Balancer user service product service customer service cart service payment service Monolith Database -
user service product service customer service cart service payment service
product service customer service Communication Channel (HTTP protocol / Messaging services like RabbitMQ ,Kafka) recommendation service customer db user db cart db product db payment db -
Advantages 1. Improved fault tolerance 2. Easy to understand. 3.
No vendor or technology lock in 4. Easy to add new modules. -
Drawbacks 1. Distributed systems are complex 2. Multiple databases makes
it difficult for transactional consistency 3. Tough to test 4. Could be tough to deploy -
Feign client ribbon sleuth hystrix user service Feign client ribbon
sleuth hystrix task service config server service-discovery (eureka) RabbitMq zipkins ui API Gateway (ZUUL) user db todo db -
task-service Anatomy and structure A simple microservice . Create tasks
Tasks can be tagged to multiple category Task can be assigned to users. Sends list of tasks and lists of task assigned to a user -
Config Server Advantages • A central place for all the
configurations. • Easy to manage when number of microservices increases • Easy to track configurations changes and act in case of breaking changes due to version control -
user service task service config server user service user service
task service task service task service task service git -
user service task service config server user service user service
task service task service task service task service git -
Feign A declarative REST client Helps to write clean code.
Focus on business logic rather implementing REST API contracts. -
Ribbon The client side load balancer Load balancing is important
in microservices. Ribbon a client side load balancer helps to distribute calls to multiple instances of a service -
user service task service task service task service Ribbon Client
side load balancing -
Eureka Eureka - The Service is discovered • Helps to
find different services • Keeps register of new instances of services going up and down -
API Gateway - Zuul • API gateway acts as single
point of contact for outside services • It’s a unique gateway to client , client make calls to API gateway and gateway in turn delegate requests to required microservices. • Obviously it can be used to aggregate responses from different microservices and send a unified response. • Apart from aggregating responses it can also be used for rate limiting ,authorization and authentication ,fault tolerance and any custom filter required to be executed during a service call. -
Distributed tracing Why ? • One request often requires multiple
services • Each request performs more than one task like database operation, messaging etc • Difficult to troubleshoot and understand behaviour of application -
Distributed tracing Solution • Add a unique ID to each
request • Record it in logs to trace the request • Record information like start and end time , use a tool to visualize your traces -
Sleuth The detective • As the name suggest , it
silently adds tracing ID. • Each microservice prints this trace id -
Sleuth in action • Just add sleuth to pom. •
To visualize using Zipkin UI ,add cloud stream and rabbit MQ -
Hystrix Fallback Mechanism • Provides better fault tolerance • Helps
to rescue in case of failures -
Spring Cloud Bus and Config server Update Configs on the
Go • Helps to update config • You can manage configs and no need to restart -
Steps to Update 1. Add actuator to dependency 2. @RefreshScope
of Bean 3. Allow actuator endpoints a. management.endpoints.web.exposure.include=* b. management.security.enabled=false 4. Use git to update files 5. Use actuator/refresh endpoints -
Benefits of Event Driven Microservices Love Async • Helps to
build more robust microservices • Helps at achieving eventual consistency • Helps to decouple the microservices -
Steps to Install (Generic) 1. Download the Kafka a. http://mirrors.estointernet.in/apache/kafka/2.4.0/kafka_2.11-2.4.0.tgz
b. Unzip the tar i. tar xzf kafka_2.11-2.4.0.tgz 2. Run Zookeeper a. bin/zookeeper-server-start.sh config/zookeeper.properties 3. Run Kafka a. bin/kafka-server-start.sh config/server.properties -
Send & Receive Messages 1. Create a topic a. bin/kafka-topics.sh
--create --zookeeper localhost:2181 --replication- factor 1 --partitions 1 --topic kafka-test-topic 2. Verify if Topic Got Created a. bin/kafka-topics.sh –list –zookeeper localhost:2181 3. Send Messages a. bin/kafka-console-producer.sh --broker-list localhost:9092 --topic kafka-test-topic >Welcome to Kafka 4. Receive Messages a. bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 -- topic kafka-test-topic --from-beginning -
Adding dependencies to Spring Project <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> <dependency>
<groupId>org.apache.kafka</groupId> <artifactId>kafka-streams</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-kafka</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka-test</artifactId> <scope>test</scope> </dependency> -
Configuring application properties spring: stream: default-binder: rabbit kafka: client-id :
user-service bootstrap-servers: - localhost:9092 template: default-topic: tasks -
Creating Required Beans @EnableKafka public class TaskConfig { @Autowired private
KafkaProperties kafkaProperties; @Bean public Map<String, Object> producerConfigs() { Map<String, Object> props = new HashMap<>(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers()); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); return props; } @Bean public ProducerFactory<String, String> producerFactory() { return new DefaultKafkaProducerFactory<>(producerConfigs()); } @Bean public KafkaTemplate<String, String> kafkaTemplate() { return new KafkaTemplate<>(producerFactory()); } } -
Send messages @PostMapping("send") public ResponseEntity<?> send(@RequestParam ("message") String message){ kafkaTemplate.send("tasks",message);
return new ResponseEntity<>(HttpStatus.OK); } -
Configuring application properties spring: name: user-service cloud: stream: default-binder: rabbit
kafka: listener: missing-topics-fatal: false client-id : user-service bootstrap-servers: - localhost:9092 template: default-topic: tasks -
Creating Required Java Config @EnableKafka public class UserConfig { @Autowired
private KafkaProperties kafkaProperties; @Bean public ConsumerFactory<String, String> consumerFactory() { Map<String, Object> props = new HashMap<>(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaProperties.getBootstrapServers()); props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class); props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class); return new DefaultKafkaConsumerFactory<>(props); } @Bean public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() { ConcurrentKafkaListenerContainerFactory<String, String> factory =new ConcurrentKafkaListenerContainerFactory<>(); factory.setConsumerFactory(consumerFactory()); return factory; } } -
Recieve messages @KafkaListener(id = "tasks", topics = "tasks", containerFactory =
"kafkaListenerContainerFactory") public void consume(@Payload String payload, @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition, @Header(KafkaHeaders.RECEIVED_TOPIC) String incomingTopic, @Header(KafkaHeaders.RECEIVED_TIMESTAMP) long ts ) { LOGGER.info("Incoming message {}-> {}", incomingTopic, payload); }