Spring Framework - один из наиболее часто используемых фреймворков для разработки корпоративных приложений. Множество высокопроизводительных решений уже построено на его основе. Если вы начинаете новый проект на Java, то, вероятнее всего, он также будет использовать Spring Framework.Использование микросервисного подхода позволяет реагировать на изменения требований быстрее за счет упрощения отдельных компонентов и возможности их параллельной разработки. Однако, использование этого подхода также сопряжено и с дополнительными проблемами - развертывание и отладка существенно усложнились, а для совместной работы сервисов необходимы дополнительные инфраструктурные компоненты, такие как, централизованная конфигурации, возможность повторной отправки сообщений или балансировка нагрузки между несколькими запущенными инстансами.Spring Boot изменил подход к разработке приложений, основанных на Spring Framework. Автоконфигурации, предоставляемые стартерами, позволяют сразу приступить к реализации основной функциональности и не тратить время на настройку инфраструктурных компонентов. Spring Cloud развил эту идею и предоставляет готовые стартеры для реализации микросервисных паттернов.
Презентация подготовлена по материалам выступления Александра Бармина на витебской конференции “Developer's Software Conference” (30.11.2019).
ICT role in 21st century education and its challenges
Микросервисы со Spring Boot & Spring Cloud
1. Микросервисы со Spring
Boot & Spring Cloud
L e a d S o f t w a r e E n g i n e e r a t E PA M
A l e k s a n d r B a r m i n
3 0 Н О Я Б Р Я
2. About Myself
Lead Software Engineer at EPAM
EPAM Lab mentor
CONTACTS
Email: Aleksandr_Barmin@epam.com
Twitter: @AlexBarmin
ALEKSANDR BARMIN
2
3. Agenda
• Why Spring?
• Configuration for a distributed environment.
• Service Discovery using the Spring Framework.
• Client-side Load Balancing.
• Server-side Load Balancing.
• Q&A Session.
3
4. Why Spring?
• Spring Framework is one of the most convenient ways to start a new Java-
based application.
• Spring Boot allows configuring subsystems just by adding dependencies.
• Spring Cloud focuses on providing good out-of-the-box experience for typical
use cases and extensibility mechanism to cover others.
• Spring Cloud takes a very declarative approach, and often you get a lot of
features with just a classpath change and/or an annotation.
5. Main Subprojects
• Spring Cloud Config
• Spring Cloud Netflix
• Spring Cloud Zookeeper
• Spring Cloud Gateway
• Spring Cloud OpenFeign
• Spring Cloud Security
• Spring Cloud Bus
• Spring Cloud Cluster
• Spring Cloud CLI
• Spring Cloud Commons
• Spring Cloud Connectors
• Spring Cloud Contract
• Spring Cloud Functions
• Spring Cloud anything
10. Reasoning
• Centralized external configuration management.
• Different configuration for different environment and profiles.
• Server:
• HTTP, resource-based API for external configuration.
• Encryption and decryption of property values.
• Embeddable using Boot Starter.
• Client:
• Binds to an external Config Server.
• Encryption and decryption of property values.
11. Spring Cloud Config Server
• Configuration Storage:
• Git Backend
• SVN Backend
• File System Backend
• JDBC Backend
• Composite Environment Repositories
12. Spring Cloud Config
12
Cloud Config
Server
Spring Boot
Application
GIT repository
SVN repository
Configuration
Database
bootstrap.yml
name: spring-boot-application
profiles: default, db, local
application.
yml
16. Reasoning
• Client-side service discovery allows services to find and communicate with
each other without hard coding hostname and port. The only “fixed point” in
such an architecture consists of a service registry, with which each service
has to register.
• A drawback is that all clients must implement a certain logic to interact with
this fixed point. This assumes an additional network round trip before the
actual request.
• To be informed about the presence of a client, they have to send a heartbeat
signal to the registry.
17. EUREKA
• Open Source
• The architecture is primarily
client/server, with a set of Eureka
servers per datacenter.
• Provides a weakly consistent view of
services, using best effort replication.
• Service registrations have a short
Time-To-Live (TTL), requiring clients to
heartbeat with the servers.
• Easy cluster administration and high
scalability.
• Can use additional tools like Ribbon (a
client-side load balancer).
• Eureka does not provide similar
guarantees, and typically requires
running ZooKeeper for services that
need to perform coordination or have
stronger consistency needs.
17
https://github.com/Netflix/eureka
18. Spring Cloud Netflix Eureka
18
Eureka Server
Some Business
Service
name:business-service
host:host.server
I’malive!
Gateway
Service
Client like
Web App
I need a response from the
business service!
Do you know where is the
business service?
Do some job for me pls!
This is the response you’ve
asked!
23. Feign Client
• Feign client is a discovery-aware RestTemplate that uses interfaces to
communication with endpoints. This interfaces will be automatically
implemented at runtime, and instead of service-URLs it is using service-
names.
• We need to create an annotation with @EnableFeignClients to use it.
• In order to create Feign Client, an interface need to be created and
annotated with @FeignCleint(“service-name”) annotation.
• Eureka Client needs to be enabled as well.
27. Netflix Ribbon
Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily
provides client-side load balancing algorithms.
Netflix Ribbon also provides the following features:
• Service Discovery Integration – Ribbon load balancers provide service discovery in dynamic
environments like a cloud. Integration with Eureka and Netflix service discovery
component is included in the ribbon library.
• Fault Tolerance – the Ribbon API can dynamically determine whether the servers are up
and running in a live environment and can detect those servers that are down.
• Configurable load-balancing rules – Ribbon
supports RoundRobinRule, AvailabilityFilteringRule, WeightedResponseTimeRule out of the
box and supports defining custom rules.
28. Netflix Ribbon
Ribbon API enables us to configure the following components of the load
balancer:
• Rule – logic component which specifies the load balancing rule we are using
in our application.
• Ping – a component which specifies the mechanism we use to determine the
server’s availability in real-time.
• ServerList – can be dynamic or static. In our case, we are using a static list of
servers; hence we are defining them in the application configuration file
directly.
32. HYSTRIX
https://github.com/Netflix/Hystrix
• Provides fault tolerance and latency
tolerance.
• Defensive Programming with
Timeout.
• Defensive Programming with Limited
Thread Pool.
• Defensive Programming with Short
Circuit Breaker Pattern.
• Real time monitoring and alerting of
command center on failures.