Commit 9dbbef7d authored by Ali Saeed's avatar Ali Saeed

Use service discovery to call APIs

parent c7223662
......@@ -3,8 +3,6 @@ package org.example.resources;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.example.models.Invoice;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
......
......@@ -24,7 +24,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
......@@ -64,8 +63,4 @@
</snapshots>
</repository>
</repositories>
</project>
\ No newline at end of file
......@@ -2,6 +2,7 @@ package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
......@@ -13,6 +14,7 @@ public class UsersServiceApplication {
SpringApplication.run(UsersServiceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
......
......@@ -24,10 +24,10 @@ public class UserResource {
@PostMapping("/payInvoice/{userId}/{invoiceId}")
public boolean payInvoice(@PathVariable("userId") String userId,@PathVariable("invoiceId") String invoiceId){
User user = new User(userId,"Ali",5000.0);
Invoice invoice = restTemplate.getForObject("http://localhost:8081/invoices/getInvoiceDetails/" + invoiceId, Invoice.class);
Invoice invoice = restTemplate.getForObject("http://invoicing-service/invoices/getInvoiceDetails/" + invoiceId, Invoice.class);
if(user.getBalance() >= invoice.getValue()){
user.setBalance(user.getBalance() - invoice.getValue());
String url = "http://localhost:8081/invoices/updateInvoiceStatus/" + invoiceId;
String url = "http://invoicing-service/invoices/updateInvoiceStatus/" + invoiceId;
ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, null, Boolean.class);
boolean status = response.getBody();
return status;
......@@ -39,7 +39,7 @@ public class UserResource {
}
@GetMapping("/getAllUsersHaveUnPaidInvoices")
public HashMap<User,Invoice> getAllUsersHaveUnPaidInvoices(){
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://localhost:8081/invoices/getUnPaidInvoices", Invoice[].class));
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://invoicing-service/invoices/getUnPaidInvoices", Invoice[].class));
HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>();
for (Invoice invoice: invoices)
{
......@@ -57,7 +57,7 @@ public class UserResource {
public HashMap<User,Invoice> getAllUsersHavePaidInvoices(){
HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>();
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://localhost:8081/invoices/getPaidInvoices", Invoice[].class));
List<Invoice> invoices = Arrays.asList(restTemplate.getForObject("http://invoicing-service/invoices/getPaidInvoices", Invoice[].class));
for (Invoice invoice: invoices)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment