Skip to content

Commit b064d4d

Browse files
committed
Adding spring cloud eureka for discovery service
1 parent 385ea08 commit b064d4d

File tree

19 files changed

+193
-46
lines changed

19 files changed

+193
-46
lines changed

.idea/compiler.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker/docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,9 @@ services:
113113
interval: 5s
114114
timeout: 2s
115115
retries: 60
116+
117+
eureka:
118+
build: ../spring-cloud/eureka-server
119+
mem_limit: 512m
120+
ports:
121+
- "8761:8761"

microservices/course-composite-service/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<description>Demo project for Spring Boot</description>
1616
<properties>
1717
<java.version>17</java.version>
18+
<spring-cloud.version>2021.0.1</spring-cloud.version>
1819
</properties>
1920
<dependencies>
2021
<dependency>
@@ -45,6 +46,11 @@
4546
<artifactId>lombok</artifactId>
4647
<optional>true</optional>
4748
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.cloud</groupId>
51+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
52+
</dependency>
53+
4854

4955
<!--Internal Dep-->
5056
<dependency>
@@ -71,6 +77,18 @@
7177
</dependency>
7278
</dependencies>
7379

80+
<dependencyManagement>
81+
<dependencies>
82+
<dependency>
83+
<groupId>org.springframework.cloud</groupId>
84+
<artifactId>spring-cloud-dependencies</artifactId>
85+
<version>${spring-cloud.version}</version>
86+
<type>pom</type>
87+
<scope>import</scope>
88+
</dependency>
89+
</dependencies>
90+
</dependencyManagement>
91+
7492
<build>
7593
<plugins>
7694
<plugin>

microservices/course-composite-service/src/main/java/io/javatab/microservices/composite/course/CourseCompositeServiceApplication.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
66
import org.springframework.boot.context.properties.EnableConfigurationProperties;
7+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
78
import org.springframework.context.annotation.Bean;
89
import org.springframework.context.annotation.ComponentScan;
910
import org.springframework.web.client.RestTemplate;
11+
import org.springframework.web.reactive.function.client.WebClient;
1012

1113
@SpringBootApplication
1214
@ComponentScan("io.javatab")
@@ -18,8 +20,9 @@ public static void main(String[] args) {
1820
}
1921

2022
@Bean
21-
RestTemplate restTemplate() {
22-
return new RestTemplate();
23+
@LoadBalanced
24+
public WebClient.Builder loadBalancedWebClientBuilder() {
25+
return WebClient.builder();
2326
}
2427

2528
}

microservices/course-composite-service/src/main/java/io/javatab/microservices/composite/course/service/CourseCompositeIntegration.java

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,50 @@
1212
import org.springframework.beans.factory.annotation.Value;
1313
import org.springframework.stereotype.Component;
1414
import org.springframework.web.client.RestTemplate;
15+
import org.springframework.web.reactive.function.client.WebClient;
16+
import org.springframework.web.reactive.function.client.WebClientResponseException;
1517
import reactor.core.publisher.Mono;
1618

19+
import static java.util.logging.Level.FINE;
20+
1721
@Component
1822
public class CourseCompositeIntegration implements CourseService, StudentService, VoteService {
1923

2024
private static final Logger LOG = LoggerFactory.getLogger(CourseCompositeIntegration.class);
2125

22-
private final RestTemplate restTemplate;
26+
private WebClient webClient;
2327
private final ObjectMapper objectMapper;
2428

25-
private final String courseServiceUrl;
26-
private final String studentServiceUrl;
27-
private final String voteServiceUrl;
29+
private static final String COURSE_SERVICE_URL = "http://course";
30+
private static final String STUDENT_SERVICE_URL = "http://student";
31+
private static final String VOTE_SERVICE_URL = "http://vote";
2832

2933
public CourseCompositeIntegration(
30-
RestTemplate restTemplate,
31-
ObjectMapper objectMapper,
32-
@Value("${app.course-service.host}")
33-
String courseServiceHost,
34-
@Value("${app.course-service.port}")
35-
String courseServicePort,
36-
@Value("${app.student-service.host}")
37-
String studentServiceHost,
38-
@Value("${app.student-service.port}")
39-
String studentServicePort,
40-
@Value("${app.vote-service.host}")
41-
String voteServiceHost,
42-
@Value("${app.vote-service.port}")
43-
String voteServicePort) {
44-
this.restTemplate = restTemplate;
34+
WebClient.Builder webClientBuilder,
35+
ObjectMapper objectMapper) {
36+
this.webClient = webClientBuilder.build();
4537
this.objectMapper = objectMapper;
46-
this.courseServiceUrl = "http://" + courseServiceHost + ":" + courseServicePort + "/course/";
47-
this.studentServiceUrl = "http://" + studentServiceHost + ":" + studentServicePort + "/student/";
48-
this.voteServiceUrl = "http://" + voteServiceHost + ":" + voteServicePort + "/vote/";
4938
}
5039

5140

5241
@Override
5342
public Mono<Course> getCourse(int courseId) {
5443
LOG.info("Fetching courses in Integration layer");
55-
var url = this.courseServiceUrl + courseId;
56-
var course = restTemplate.getForObject(url, Course.class);
57-
return Mono.just(course);
44+
var url = COURSE_SERVICE_URL + "/course/" + courseId;
45+
return webClient.get().uri(url).retrieve().bodyToMono(Course.class).log(LOG.getName(), FINE);
5846
}
5947

6048
@Override
6149
public Mono<Student> getStudent(String studentId) {
6250
LOG.info("Fetching students in Integration layer");
63-
var url = this.studentServiceUrl + studentId;
64-
var student = restTemplate.getForObject(url, Student.class);
65-
return Mono.just(student);
51+
var url = STUDENT_SERVICE_URL + "/student/" + studentId;
52+
return webClient.get().uri(url).retrieve().bodyToMono(Student.class).log(LOG.getName(), FINE);
6653
}
6754

6855
@Override
6956
public Mono<Vote> getVote(int courseId) {
70-
LOG.info("Fetching voting details in Integration layer");
71-
var url = this.voteServiceUrl + courseId;
72-
var vote = restTemplate.getForObject(url, Vote.class);
73-
return Mono.just(vote);
57+
LOG.info("Fetching votes in Integration layer");
58+
var url = VOTE_SERVICE_URL + "/vote/" + courseId;
59+
return webClient.get().uri(url).retrieve().bodyToMono(Vote.class).log(LOG.getName(), FINE);
7460
}
7561
}

microservices/course-composite-service/src/main/resources/application.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
server.port: 8016
2-
2+
spring.application.name: course-composite
33
app:
44
course-service:
55
host: localhost
@@ -65,11 +65,24 @@ springdoc:
6565
packagesToScan: io.javatab.microservices.composite.course
6666
pathsToMatch: /**
6767

68+
app.eureka-server: localhost
69+
70+
eureka:
71+
client:
72+
serviceUrl:
73+
defaultZone: http://${app.eureka-server}:8761/eureka/
74+
initialInstanceInfoReplicationIntervalSeconds: 5
75+
registryFetchIntervalSeconds: 5
76+
instance:
77+
leaseRenewalIntervalInSeconds: 5
78+
leaseExpirationDurationInSeconds: 5
6879
---
6980
spring.config.activate.on-profile: docker
7081

7182
server.port: 8080
7283

84+
eureka.client.serviceUrl.defaultZone: http://eureka:8761/eureka/
85+
7386
app:
7487
course-service:
7588
host: course

microservices/course-composite-service/src/test/java/io/javatab/microservices/composite/course/CourseCompositeServiceApplicationTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.test.context.SpringBootTest;
55

6-
@SpringBootTest
7-
class CourseCompositeServiceApplicationTests {
6+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
87

9-
@Test
10-
void contextLoads() {
11-
}
8+
@SpringBootTest(webEnvironment=RANDOM_PORT, properties = {"eureka.client.enabled=false"})
9+
class CourseCompositeServiceApplicationTests {
1210

1311
}

microservices/course-service/pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<properties>
1717
<java.version>17</java.version>
1818
<testcontainers.version>1.16.2</testcontainers.version>
19+
<spring-cloud.version>2021.0.1</spring-cloud.version>
1920
</properties>
2021
<dependencies>
2122
<dependency>
@@ -30,7 +31,10 @@
3031
<groupId>org.springframework.boot</groupId>
3132
<artifactId>spring-boot-starter-data-mongodb</artifactId>
3233
</dependency>
33-
34+
<dependency>
35+
<groupId>org.springframework.cloud</groupId>
36+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
37+
</dependency>
3438
<dependency>
3539
<groupId>org.mapstruct</groupId>
3640
<artifactId>mapstruct</artifactId>
@@ -78,6 +82,13 @@
7882
<type>pom</type>
7983
<scope>import</scope>
8084
</dependency>
85+
<dependency>
86+
<groupId>org.springframework.cloud</groupId>
87+
<artifactId>spring-cloud-dependencies</artifactId>
88+
<version>${spring-cloud.version}</version>
89+
<type>pom</type>
90+
<scope>import</scope>
91+
</dependency>
8192
</dependencies>
8293
</dependencyManagement>
8394

0 commit comments

Comments
 (0)