Posts

Showing posts from September, 2023

Setup Client Side Load Balancing With Ribbon

Image
in prvious video we have imvoked the currencyExchangeService From our CurrencyCalculationService  to calculate the Currency by the quentry and there currnecy exchange in last step we added Feign to make calling the currencyExchangeService from  currencyExchangeService  very easily     So in given Above you can see in Currncy Conversion service we have only 1 Prod instance while Currency Exchange Service we have 4 instances so now hile Conversion service trying to call the Exchange  service than we have to load balance means we would need to distribute the load between all these instance  Thats where Ribbon Comes into the Picture We were using Feing to call Currency Exchange Service Ribbon can make use of the Feign Configuration, that we have already done in previous tutotial and Help us Distirubution the calls between diffrent instances (prod1, prod2 , prod3, prod4) of currency exchange prod service When we enable Ribbon we will see the load will...

Using Feign Rest Client for Service invocation

 In this Tutorial will Learn Importatnt Spring Boot Cloud Component Called Feign F-E-I-G-N in previous Code It was Prety complex code we have written to call a REST Service, there was a lot of  manual stuff that we had to do , to call a very very simple service  Map<String, String> uriVariables = new HashMap<String, String>(); uriVariables .put( "from" , from ); uriVariables .put( "to" , to ); ResponseEntity<CurrencyConversionBean> responseEntity = new RestTemplate() .getForEntity( "http://localhost:8000/currency-exchange/from/{from}/to/{to}" , CurrencyConversionBean. class , uriVariables ); CurrencyConversionBean response = responseEntity .getBody(); There is no complexity was there but we had to still write a lot of code for it So if we talk about the Microservices than there will be a lot of calls to other services. so if you want to make your code much simpler than this Than one of the important problem s...

Create New Microservice CurrencyConverser

Image
List of Dependencied for Createing Currency Conversion Service user same Group as  com.khan.microserservices Web DevTools Actuator Config Client Also configure application.properties spring.application.name= currency-conversion-service server.port= 8100 Make sure to add < dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-bootstrap</ artifactId > </ dependency > in pom.xml to avoid the spring.config.import error   With Currency Calculation we have to calculate the FROM to To like USD to INR and convert how many 1000 USD to INR will be 86000 INR like that simply 1 USD is worth of 86 INR so 1000 USD will be 86000 INR Create Dummy Currency Conversion service STEP 1 Controller Class @RestController public class CurrencyConversionController { @GetMapping ( "/currency-converter/from/{from}/to/{to}/quantity/{quantity}" ) public CurrencyConversionBean convertCurrency( @PathVariable...

Create a JPA Repository

Image
STEP 1:  ExchangeValueRepository interface which extends JpaRepository public interface ExchangeValueRepository extends JpaRepository<ExchangeValue, Long> { //this method will give the implementation of given below method //this is the magic of Spring data JPA, //Once you provide method defination live given below you will get the implementation automaticaly ExchangeValue findByFromAndTo(String from , String to ); } STEP 2: Entity Class    @Entity public class ExchangeValue { @Id private Long id ; @Column (name= "currency_from" ) private String from ; @Column (name= "currency_to" ) private String to ; @Column (name= "conversion_multiple" ) private BigDecimal conversionMultiple ; @Column (name= "port" ) private int port ; //Getter //getter/setter for port //default and param constructor } STEP 3: Controller Class @RestController public class CurrencyExchangeController { @Autowired private Envi...

Spring Boot Microservice Configure JPA and intialize Data

Image
 STEP 1. Add Given below dependacny in your currencyExchangeService Project < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-jpa</ artifactId > </ dependency > < dependency > < groupId >com.h2database</ groupId > < artifactId > h2 </ artifactId > </ dependency >  STEP 2. Now Convert your POJO to @Entity Annotation to Entity class and @Id at data member @Entity public class ExchangeValue { @Id private Long id ; private String from ; private String to ; private BigDecimal conversionMultiple ; private int port ; //Getter/Setter //Constructor }  STEP 2. Create Database using file sql file data.sql in your resource folder

How to Congifure Dynamic port in spring boot Microservice

Image
 STEP 1; POJO Class public class ExchangeValue { private Long id ; private String from ; private String to ; private BigDecimal conversionMultiple ; private int port ; public ExchangeValue() { } public ExchangeValue(Long id , String from , String to , BigDecimal conversionMultiple ) { super (); this . id = id ; this . from = from ; this . to = to ; this . conversionMultiple = conversionMultiple ; } public Long getId() { return id ; } public String getFrom() { return from ; } public String getTo() { return to ; } public BigDecimal getConversionMultiple() { return conversionMultiple ; } public int getPort() { return port ; }         public void setPort( int port ) { this . port = port ; } } STEP 2; Controller Class @RestController public class CurrencyExchangeController { @Autowired private Environment environment ; @GetMapping ( "currency-exchange/...

STEP 12 Set Up new Microservice CurrencyExchange Service with hardcoded Value

Image
 STEP 1  Pom.xml will have given given below dependencies <? xml version = " 1.0 " encoding = " UTF-8 " ?> < project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd " > < modelVersion >4.0.0</ modelVersion > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >2.7.15</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >com.khan.microserservices</ groupId > < artifactId >currency-exchange-service</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >currency-exchange-service</ name > < description >currency-ex...

STEP 10 Configure Porfile for Limit service

Image
 Now by adding into bootrap.properties  the profile which you want to be in your Limit-Service spring.profiles.active= qa we are getting 2 as minimum but 888 is coming from default as maximum did not set into the limit-service-qa.properties this is default file it will pick up the value from the QA which is resited in the limits-service-qa.properties file spring.profiles.active= dev Similartly it will pick up the value from the DEV which is resited in the limits-service-dev.properties file 1. this is one of the way to pass in the active profile  2. And also add VM arguments spring.profiles.active-dev  3. Java command line arguments

STEP 9 Connect Limit service to Spring Cloud Config Server

Image
  Till now we have Connected the Spring cloud config server to GIT repository also configured with multiple environment as well But still we have not configured Limit service with spring cloud config server  In this Tutorial we will Connect LimitService to pick up the configuration from the spring cloud config server As of now all the properties are seted for limit service are present in application.properties STEP 1: Change application.properties to bootstrap.properties in Limit-service  STEP 2:   instead of using local service add the path of your spring-cloud-config-server spring.application.name= limits-service spring.cloud.config.uri= http://localhost:8888 Here is two Properties  1. spring.application.name this should be similar name of yoru spring-cloud-config-server file folder  2.  spring.cloud.config.uri this will contain the port information where our spring-cloud-config-server deployed Now able to achive the property of spring-cloud-...