SlideShare una empresa de Scribd logo
1 de 20
Presented by - Ashish Gautam
Spring Boot Microservices
Agenda
● Introduction
● Comparison with monolith architecture
● Advantages of microservices
● Challenges with microservices
● Eureka server
● Creating a spring boot microservice
● Communicating with other microservices
● Load balancing
Introduction
Microservices is an approach to developing a single application as a suite of small
services, each running in its own process and communicating with lightweight
mechanisms, often an HTTP resource API.
Microservices allow large systems to be built up from a number of collaborating
components. It does at the process level what Spring has always done at the
component level: loosely coupled processes instead of loosely coupled
components.
In comparison with Monolith application
Monolith applications are typically huge - more 100,000 line of code. In
some instances even more than million lines of code. Monoliths are
characterized by -
● Large Application Size
● Long Release Cycles
● Large Teams
Typical challenges in Monolith application
● Scalability challenges
● New technology adoption
● Difficult to perform automation tests
● Difficult to adapt to modern development practices
● Adapting to device explosion
Microservice architectures evolved as a solution to the scalability and
innovation challenges with monolithic architectures.
What Does Microservice Architecture Look Like?
This is how the same application would look like when developed using
microservices architecture.
Microservice architectures involve a number of small, well-designed
components interacting with messages.
Advantages of Microservices
● New technology and process adaptation becomes easier. You can try
new technologies with the newer microservices that we create.
● Faster release cycles.
● Scaling with the cloud.
● Quick setup needed: You cannot spend a month setting up each microservice.
You should be able to create microservices quickly.
● Automation: Because there are a number of smaller components instead of a
monolith, you need to automate everything - Builds, Deployment,
Monitoring, etc.
● Visibility: You now have a number of smaller components to deploy and
maintain, maybe 100 or maybe 1000 components. You should be able to
monitor and identify problems automatically. You need great visibility around
all the components.
● Configuration Management: You need to maintain configurations for
hundreds of components across environments. You would need a
Configuration Management solution
Challenges With Microservice Architectures
Spring Boot
Spring Boot enables building production-ready applications quickly and provides non-functional
features:
● Embedded servers (easy deployment with containers)
● Metrics (monitoring)
● Health checks (monitoring)
● Externalized configuration
Spring Cloud
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed
systems. Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud
developers can quickly stand up services and applications that implement those patterns.
Solutions to Challenges with Microservice Architectures
Important Spring Cloud Modules
● Dynamically scale up and down. using a combination of
○ Naming Server (Eureka)
○ Ribbon (Client Side Load Balancing)
○ Feign (Easier REST Clients)
● Visibility and monitoring with
○ Zipkin Distributed Tracing
○ Netflix API Gateway
● Configuration Management with Spring Cloud Config Server
● Fault Tolerance with Hystrix
Eureka Server
When you have multiple processes working together they need to find each
other. The developers at Netflix had this problem when building their systems
and created a registration server called Eureka (“I have found it” in Greek).
Fortunately for us, they made their discovery server open-source and Spring
has incorporated into Spring Cloud, making it even easier to run up a Eureka
server.
@SpringBootApplication
@EnableEurekaServer
public class ServiceRegistrationServer {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistrationServer.class, args);
}
}
Configurations in application.properties
spring.application.name=netflix-eureka-naming-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
The configuration specifies that I am not a client and stops the server process trying to register with itself.
Eureka Server Application
Creating a Microservice
A microservice is a stand-alone process that handles a well-defined requirement.
When configuring applications with Spring we emphasize Loose Coupling and Tight Cohesion, These are
not new concepts (Larry Constantine is credited with first defining these in the late 1960s - reference) but
now we are applying them, not to interacting components (Spring Beans), but to interacting processes.
@SpringBootApplication
@EnableEurekaClient
public class AccountsService {
public static void main(String[] args) {
SpringApplication.run(AccountsService.class, args);
}
}
What makes this a microservice is the registration with the eureka-server via @EnableEurekaClient
In application.properties-
spring.application.name=account-service
server.port=8000
eureka.client.service-url.default-zone=http://localhost:8761/eureka
Note that this file -
● Sets the application name as accounts-service. This service registers under this name.
● Specifies a custom port to listen on (8000).
● The URL of the Eureka Service process - from the previous section.
Communicating with other microservices
Using RestTemplate -
To consume a RESTful service, Spring provides the RestTemplate class. This allows you to send HTTP
requests to a RESTful server and fetch data in a number of formats - such as JSON and XML.
public Account getByNumber(String accountNumber) {
Account account = new RestTemplate().getForObject(serviceUrl
+ "/accounts/{number}", Account.class, accountNumber);
if (account == null)
throw new AccountNotFoundException(accountNumber);
else
return account;
}
Using Feign (Spring cloud module)
@FeignClient(name="account-service" url="localhost:8000")
public interface AccountNumberServiceProxy {
@GetMapping("/accounts/{number}")
public Account retrieveAccount(@PathVariable("number") String number);
}
Enabling Feign clients -
Before we are able to use Feign, we need to enable it by using @EnableFeignClients annotation on the appropriate package where
the client proxies are defined.
@SpringBootApplication
@EnableFeignClients("com.springboot.microservice.example.account")
@EnableDiscoveryClient
public class AccountNumberServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AccountNumberServiceApplication.class, args);
}
}
Load Balancing Using Ribbon
If you have multiple instances of a service available, ribbon picks one for you. (Eureka on their own
doesn’t perform load-balancing so we use Ribbon to do it instead).
Add dependency in build.gradle-
compile('org.springframework.cloud:spring-cloud-starter-ribbon')
@FeignClient(name="account-service")
@RibbonClient(name="account-service")
public interface AccountNumberServiceProxy {
@GetMapping("/accounts/{number}")
public Account retrieveAccount(@PathVariable("number") String number);
}
References
● https://spring.io/blog/2015/07/14/microservices-with-spring
● https://dzone.com/articles/microservices-with-spring-boot-part-1-getting-star
● https://github.com/paulc4/microservices-demo
● https://start.spring.io/
● http://www.baeldung.com/spring-cloud-netflix-eureka
Thank You..

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Service discovery with Eureka and Spring Cloud
Service discovery with Eureka and Spring CloudService discovery with Eureka and Spring Cloud
Service discovery with Eureka and Spring Cloud
 
Spring Security 5
Spring Security 5Spring Security 5
Spring Security 5
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
 
Microservice vs. Monolithic Architecture
Microservice vs. Monolithic ArchitectureMicroservice vs. Monolithic Architecture
Microservice vs. Monolithic Architecture
 
From Monolithic to Microservices
From Monolithic to Microservices From Monolithic to Microservices
From Monolithic to Microservices
 
Spring boot
Spring bootSpring boot
Spring boot
 
Monoliths and Microservices
Monoliths and Microservices Monoliths and Microservices
Monoliths and Microservices
 
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Microservices architecture
Microservices architectureMicroservices architecture
Microservices architecture
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Introduction to Spring Cloud
Introduction to Spring Cloud           Introduction to Spring Cloud
Introduction to Spring Cloud
 
Architecture: Microservices
Architecture: MicroservicesArchitecture: Microservices
Architecture: Microservices
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 
Why Microservice
Why Microservice Why Microservice
Why Microservice
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Container orchestration overview
Container orchestration overviewContainer orchestration overview
Container orchestration overview
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 

Similar a Springboot Microservices

Similar a Springboot Microservices (20)

Developing Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's GuideDeveloping Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's Guide
 
Building stateful serverless orchestrations with Azure Durable Azure Function...
Building stateful serverless orchestrations with Azure Durable Azure Function...Building stateful serverless orchestrations with Azure Durable Azure Function...
Building stateful serverless orchestrations with Azure Durable Azure Function...
 
Intro to spring cloud &microservices by Eugene Hanikblum
Intro to spring cloud &microservices by Eugene HanikblumIntro to spring cloud &microservices by Eugene Hanikblum
Intro to spring cloud &microservices by Eugene Hanikblum
 
Microservice creation using spring cloud, zipkin, ribbon, zull, eureka
Microservice creation using spring cloud, zipkin, ribbon, zull, eurekaMicroservice creation using spring cloud, zipkin, ribbon, zull, eureka
Microservice creation using spring cloud, zipkin, ribbon, zull, eureka
 
Netflix Cloud Platform and Open Source
Netflix Cloud Platform and Open SourceNetflix Cloud Platform and Open Source
Netflix Cloud Platform and Open Source
 
Microservices and modularity with java
Microservices and modularity with javaMicroservices and modularity with java
Microservices and modularity with java
 
.NET Core Apps: Design & Development
.NET Core Apps: Design & Development.NET Core Apps: Design & Development
.NET Core Apps: Design & Development
 
SpringBoot and Spring Cloud Service for MSA
SpringBoot and Spring Cloud Service for MSASpringBoot and Spring Cloud Service for MSA
SpringBoot and Spring Cloud Service for MSA
 
Microservices with kubernetes @190316
Microservices with kubernetes @190316Microservices with kubernetes @190316
Microservices with kubernetes @190316
 
Eseguire Applicazioni Cloud-Native con Pivotal Cloud Foundry su Google Cloud ...
Eseguire Applicazioni Cloud-Native con Pivotal Cloud Foundry su Google Cloud ...Eseguire Applicazioni Cloud-Native con Pivotal Cloud Foundry su Google Cloud ...
Eseguire Applicazioni Cloud-Native con Pivotal Cloud Foundry su Google Cloud ...
 
.NET microservices with Azure Service Fabric
.NET microservices with Azure Service Fabric.NET microservices with Azure Service Fabric
.NET microservices with Azure Service Fabric
 
Service Fabric – building tomorrows applications today
Service Fabric – building tomorrows applications todayService Fabric – building tomorrows applications today
Service Fabric – building tomorrows applications today
 
Ultimate Guide to Microservice Architecture on Kubernetes
Ultimate Guide to Microservice Architecture on KubernetesUltimate Guide to Microservice Architecture on Kubernetes
Ultimate Guide to Microservice Architecture on Kubernetes
 
Sumo Logic Cert Jam - Advanced Metrics with Kubernetes
Sumo Logic Cert Jam - Advanced Metrics with KubernetesSumo Logic Cert Jam - Advanced Metrics with Kubernetes
Sumo Logic Cert Jam - Advanced Metrics with Kubernetes
 
Open shift and docker - october,2014
Open shift and docker - october,2014Open shift and docker - october,2014
Open shift and docker - october,2014
 
MuleSoft Surat Virtual Meetup#27 - MuleSoft Runtime 4.4, Transit Gateway and ...
MuleSoft Surat Virtual Meetup#27 - MuleSoft Runtime 4.4, Transit Gateway and ...MuleSoft Surat Virtual Meetup#27 - MuleSoft Runtime 4.4, Transit Gateway and ...
MuleSoft Surat Virtual Meetup#27 - MuleSoft Runtime 4.4, Transit Gateway and ...
 
Spring Cloud: Why? How? What?
Spring Cloud: Why? How? What?Spring Cloud: Why? How? What?
Spring Cloud: Why? How? What?
 
Modern Cloud-Native Streaming Platforms: Event Streaming Microservices with A...
Modern Cloud-Native Streaming Platforms: Event Streaming Microservices with A...Modern Cloud-Native Streaming Platforms: Event Streaming Microservices with A...
Modern Cloud-Native Streaming Platforms: Event Streaming Microservices with A...
 
All About Microservices and OpenSource Microservice Frameworks
All About Microservices and OpenSource Microservice FrameworksAll About Microservices and OpenSource Microservice Frameworks
All About Microservices and OpenSource Microservice Frameworks
 
Stream and Batch Processing in the Cloud with Data Microservices
Stream and Batch Processing in the Cloud with Data MicroservicesStream and Batch Processing in the Cloud with Data Microservices
Stream and Batch Processing in the Cloud with Data Microservices
 

Más de NexThoughts Technologies

Más de NexThoughts Technologies (20)

Alexa skill
Alexa skillAlexa skill
Alexa skill
 
GraalVM
GraalVMGraalVM
GraalVM
 
Docker & kubernetes
Docker & kubernetesDocker & kubernetes
Docker & kubernetes
 
Apache commons
Apache commonsApache commons
Apache commons
 
HazelCast
HazelCastHazelCast
HazelCast
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Microservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & ReduxMicroservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & Redux
 
Swagger
SwaggerSwagger
Swagger
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Arango DB
Arango DBArango DB
Arango DB
 
Jython
JythonJython
Jython
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Smart Contract samples
Smart Contract samplesSmart Contract samples
Smart Contract samples
 
My Doc of geth
My Doc of gethMy Doc of geth
My Doc of geth
 
Geth important commands
Geth important commandsGeth important commands
Geth important commands
 
Ethereum genesis
Ethereum genesisEthereum genesis
Ethereum genesis
 
Ethereum
EthereumEthereum
Ethereum
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
Google authentication
Google authenticationGoogle authentication
Google authentication
 
Java 9 Features
Java 9 FeaturesJava 9 Features
Java 9 Features
 

Último

Último (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Springboot Microservices

  • 1. Presented by - Ashish Gautam Spring Boot Microservices
  • 2. Agenda ● Introduction ● Comparison with monolith architecture ● Advantages of microservices ● Challenges with microservices ● Eureka server ● Creating a spring boot microservice ● Communicating with other microservices ● Load balancing
  • 3. Introduction Microservices is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. Microservices allow large systems to be built up from a number of collaborating components. It does at the process level what Spring has always done at the component level: loosely coupled processes instead of loosely coupled components.
  • 4. In comparison with Monolith application Monolith applications are typically huge - more 100,000 line of code. In some instances even more than million lines of code. Monoliths are characterized by - ● Large Application Size ● Long Release Cycles ● Large Teams
  • 5. Typical challenges in Monolith application ● Scalability challenges ● New technology adoption ● Difficult to perform automation tests ● Difficult to adapt to modern development practices ● Adapting to device explosion Microservice architectures evolved as a solution to the scalability and innovation challenges with monolithic architectures.
  • 6. What Does Microservice Architecture Look Like? This is how the same application would look like when developed using microservices architecture.
  • 7. Microservice architectures involve a number of small, well-designed components interacting with messages.
  • 8. Advantages of Microservices ● New technology and process adaptation becomes easier. You can try new technologies with the newer microservices that we create. ● Faster release cycles. ● Scaling with the cloud.
  • 9. ● Quick setup needed: You cannot spend a month setting up each microservice. You should be able to create microservices quickly. ● Automation: Because there are a number of smaller components instead of a monolith, you need to automate everything - Builds, Deployment, Monitoring, etc. ● Visibility: You now have a number of smaller components to deploy and maintain, maybe 100 or maybe 1000 components. You should be able to monitor and identify problems automatically. You need great visibility around all the components. ● Configuration Management: You need to maintain configurations for hundreds of components across environments. You would need a Configuration Management solution Challenges With Microservice Architectures
  • 10. Spring Boot Spring Boot enables building production-ready applications quickly and provides non-functional features: ● Embedded servers (easy deployment with containers) ● Metrics (monitoring) ● Health checks (monitoring) ● Externalized configuration Spring Cloud Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems. Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. Solutions to Challenges with Microservice Architectures
  • 11. Important Spring Cloud Modules ● Dynamically scale up and down. using a combination of ○ Naming Server (Eureka) ○ Ribbon (Client Side Load Balancing) ○ Feign (Easier REST Clients) ● Visibility and monitoring with ○ Zipkin Distributed Tracing ○ Netflix API Gateway ● Configuration Management with Spring Cloud Config Server ● Fault Tolerance with Hystrix
  • 12. Eureka Server When you have multiple processes working together they need to find each other. The developers at Netflix had this problem when building their systems and created a registration server called Eureka (“I have found it” in Greek). Fortunately for us, they made their discovery server open-source and Spring has incorporated into Spring Cloud, making it even easier to run up a Eureka server.
  • 13. @SpringBootApplication @EnableEurekaServer public class ServiceRegistrationServer { public static void main(String[] args) { SpringApplication.run(ServiceRegistrationServer.class, args); } } Configurations in application.properties spring.application.name=netflix-eureka-naming-server server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false The configuration specifies that I am not a client and stops the server process trying to register with itself. Eureka Server Application
  • 14. Creating a Microservice A microservice is a stand-alone process that handles a well-defined requirement. When configuring applications with Spring we emphasize Loose Coupling and Tight Cohesion, These are not new concepts (Larry Constantine is credited with first defining these in the late 1960s - reference) but now we are applying them, not to interacting components (Spring Beans), but to interacting processes.
  • 15. @SpringBootApplication @EnableEurekaClient public class AccountsService { public static void main(String[] args) { SpringApplication.run(AccountsService.class, args); } } What makes this a microservice is the registration with the eureka-server via @EnableEurekaClient In application.properties- spring.application.name=account-service server.port=8000 eureka.client.service-url.default-zone=http://localhost:8761/eureka Note that this file - ● Sets the application name as accounts-service. This service registers under this name. ● Specifies a custom port to listen on (8000). ● The URL of the Eureka Service process - from the previous section.
  • 16. Communicating with other microservices Using RestTemplate - To consume a RESTful service, Spring provides the RestTemplate class. This allows you to send HTTP requests to a RESTful server and fetch data in a number of formats - such as JSON and XML. public Account getByNumber(String accountNumber) { Account account = new RestTemplate().getForObject(serviceUrl + "/accounts/{number}", Account.class, accountNumber); if (account == null) throw new AccountNotFoundException(accountNumber); else return account; }
  • 17. Using Feign (Spring cloud module) @FeignClient(name="account-service" url="localhost:8000") public interface AccountNumberServiceProxy { @GetMapping("/accounts/{number}") public Account retrieveAccount(@PathVariable("number") String number); } Enabling Feign clients - Before we are able to use Feign, we need to enable it by using @EnableFeignClients annotation on the appropriate package where the client proxies are defined. @SpringBootApplication @EnableFeignClients("com.springboot.microservice.example.account") @EnableDiscoveryClient public class AccountNumberServiceApplication { public static void main(String[] args) { SpringApplication.run(AccountNumberServiceApplication.class, args); } }
  • 18. Load Balancing Using Ribbon If you have multiple instances of a service available, ribbon picks one for you. (Eureka on their own doesn’t perform load-balancing so we use Ribbon to do it instead). Add dependency in build.gradle- compile('org.springframework.cloud:spring-cloud-starter-ribbon') @FeignClient(name="account-service") @RibbonClient(name="account-service") public interface AccountNumberServiceProxy { @GetMapping("/accounts/{number}") public Account retrieveAccount(@PathVariable("number") String number); }
  • 19. References ● https://spring.io/blog/2015/07/14/microservices-with-spring ● https://dzone.com/articles/microservices-with-spring-boot-part-1-getting-star ● https://github.com/paulc4/microservices-demo ● https://start.spring.io/ ● http://www.baeldung.com/spring-cloud-netflix-eureka