Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
M
Micro_Services
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ali.saeed
Micro_Services
Commits
9dbbef7d
Commit
9dbbef7d
authored
Apr 26, 2024
by
Ali Saeed
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use service discovery to call APIs
parent
c7223662
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
6 additions
and
11 deletions
+6
-11
ExternalAPI.java
...vice/src/main/java/org/example/resources/ExternalAPI.java
+0
-2
pom.xml
users-service/pom.xml
+0
-5
UsersServiceApplication.java
...ce/src/main/java/org/example/UsersServiceApplication.java
+2
-0
UserResource.java
...ice/src/main/java/org/example/resources/UserResource.java
+4
-4
No files found.
invoicing-service/src/main/java/org/example/resources/ExternalAPI.java
View file @
9dbbef7d
...
...
@@ -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
;
...
...
users-service/pom.xml
View file @
9dbbef7d
...
...
@@ -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
users-service/src/main/java/org/example/UsersServiceApplication.java
View file @
9dbbef7d
...
...
@@ -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
();
}
...
...
users-service/src/main/java/org/example/resources/UserResource.java
View file @
9dbbef7d
...
...
@@ -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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment