DevShivmohan / Learning-everything

Learning for developer only
0 stars 1 forks source link

Spring boot microservices #29

Open DevShivmohan opened 1 year ago

DevShivmohan commented 1 year ago

Microservices communication using RestTemplate

declare RestTemplate as a bean with Load balance, @LoadBalanced is mandatory otherwise not working by service name
       @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization","Shiv1");
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity<User> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = null;
        try {
            response= restTemplate.exchange("http://API-GATEWAY/BaseR4/Patient/2", HttpMethod.GET, entity, String.class);
            System.out.println(response);
            System.out.println("statusCode-"+response.getStatusCode());
            System.out.println("statusBody-"+response.getBody());
        }catch (HttpClientErrorException | HttpServerErrorException  e){
            System.out.println("exception-"+response);
            System.out.println("msg-"+e.getMessage());
            System.out.println("statusCode-"+e.getStatusCode());
            System.out.println("statusBody-"+e.getResponseBodyAsString());
            response=ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsString());
        }
DevShivmohan commented 1 year ago

For routing in API gateway

DevShivmohan commented 1 year ago

Passing argument in native query [JpaRepository]


-  **Indexed parameter**.

public interface CarRepository extends JpaRepository<TRace, String> { @Query(nativeQuery = true, value = "select * from car_records where carVinNo = ?1 and carSerialNo >= ?2") List retrieveCars(Long vinNo, Long serialNo); }

DevShivmohan commented 1 year ago

Getting response as a Object using RestTemplate

URL - https://jservice.io/api/random HttpMethod - GET

Object model class

package org.shiv.razorpay.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RandomObject {
    private Integer id;
    private String answer;
    private String question;
    private Integer value;
    private String airdate;
    private String created_at;
    private String updated_at;
    private Integer category_id;
    private Integer game_id;
    private Integer invalid_count;

    private Category category;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Category{
    private Integer id;
    private String title;
    private String created_at;
    private String updated_at;
    private Integer clues_count;
}

Requesting for URL

@GetMapping("/test")
    public ResponseEntity<?> gettest(){
        var response= restTemplate.getForEntity("https://jservice.io/api/random", RandomObject[].class);
        System.out.println("Actual data");
        RandomObject randomObject=Objects.requireNonNull(response.getBody())[0]; 
        System.out.println(randomObject);
        return ResponseEntity.status(HttpStatus.OK).body(response.getBody());
    }
DevShivmohan commented 10 months ago

Distributed Transactions over microservices

There are two way to manage the consistency in microservices is also called as SAGA pattern -

Saga-with temporal framework

Live testing of Saga pattern with Success and Failure case