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; ...@@ -3,8 +3,6 @@ package org.example.resources;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import org.example.models.Invoice; import org.example.models.Invoice;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
......
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
...@@ -64,8 +63,4 @@ ...@@ -64,8 +63,4 @@
</snapshots> </snapshots>
</repository> </repository>
</repositories> </repositories>
</project> </project>
\ No newline at end of file
...@@ -2,6 +2,7 @@ package org.example; ...@@ -2,6 +2,7 @@ package org.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
...@@ -13,6 +14,7 @@ public class UsersServiceApplication { ...@@ -13,6 +14,7 @@ public class UsersServiceApplication {
SpringApplication.run(UsersServiceApplication.class, args); SpringApplication.run(UsersServiceApplication.class, args);
} }
@Bean @Bean
@LoadBalanced
public RestTemplate getRestTemplate(){ public RestTemplate getRestTemplate(){
return new RestTemplate(); return new RestTemplate();
} }
......
...@@ -24,10 +24,10 @@ public class UserResource { ...@@ -24,10 +24,10 @@ public class UserResource {
@PostMapping("/payInvoice/{userId}/{invoiceId}") @PostMapping("/payInvoice/{userId}/{invoiceId}")
public boolean payInvoice(@PathVariable("userId") String userId,@PathVariable("invoiceId") String invoiceId){ public boolean payInvoice(@PathVariable("userId") String userId,@PathVariable("invoiceId") String invoiceId){
User user = new User(userId,"Ali",5000.0); 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()){ if(user.getBalance() >= invoice.getValue()){
user.setBalance(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); ResponseEntity<Boolean> response = restTemplate.exchange(url, HttpMethod.POST, null, Boolean.class);
boolean status = response.getBody(); boolean status = response.getBody();
return status; return status;
...@@ -39,7 +39,7 @@ public class UserResource { ...@@ -39,7 +39,7 @@ public class UserResource {
} }
@GetMapping("/getAllUsersHaveUnPaidInvoices") @GetMapping("/getAllUsersHaveUnPaidInvoices")
public HashMap<User,Invoice> 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<>(); HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>();
for (Invoice invoice: invoices) for (Invoice invoice: invoices)
{ {
...@@ -57,7 +57,7 @@ public class UserResource { ...@@ -57,7 +57,7 @@ public class UserResource {
public HashMap<User,Invoice> getAllUsersHavePaidInvoices(){ public HashMap<User,Invoice> getAllUsersHavePaidInvoices(){
HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>(); 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) 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