Skip to content

Commit 38ecda9

Browse files
committed
Edge service integrated using Spring Cloud Gateway
1 parent 935a827 commit 38ecda9

File tree

7 files changed

+83
-17
lines changed

7 files changed

+83
-17
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.

docker/docker-compose.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ services:
4545
build: ../microservices/course-composite-service
4646
image: javatab/course_composite_service:v1
4747
mem_limit: 512m
48-
ports:
49-
- "8080:8080" # Only this service will be exposed to outside containers so no port binding for above images
5048
environment:
5149
- SPRING_PROFILES_ACTIVE=docker
5250

@@ -112,5 +110,11 @@ services:
112110
eureka:
113111
build: ../spring-cloud/eureka-server
114112
mem_limit: 512m
113+
114+
gateway:
115+
environment:
116+
- SPRING_PROFILES_ACTIVE=docker
117+
build: ../spring-cloud/gateway
118+
mem_limit: 512m
115119
ports:
116-
- "8761:8761"
120+
- "8080:8080"

microservices/search-service/src/main/java/io/javatab/microservices/core/search/SearchServiceApplication.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,9 @@
99

1010
@SpringBootApplication
1111
@ComponentScan("io.javatab")
12-
public class SearchServiceApplication implements CommandLineRunner {
13-
14-
private ElasticRepository repository;
15-
16-
public SearchServiceApplication(ElasticRepository repository) {
17-
this.repository = repository;
18-
}
12+
public class SearchServiceApplication {
1913

2014
public static void main(String[] args) {
2115
SpringApplication.run(SearchServiceApplication.class, args);
2216
}
23-
24-
@Override
25-
public void run(String... args) throws Exception {
26-
repository.deleteCourse().block();
27-
repository.createCourse(new SearchEntity("Course Name", 1))
28-
.subscribe(indexResponse -> System.out.println("Index Response " + indexResponse));
29-
}
3017
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
<module>microservices/student-service</module>
2828
<module>microservices/vote-service</module>
2929
<module>spring-cloud/eureka-server</module>
30+
<module>spring-cloud/gateway</module>
3031
</modules>
3132
</project>

spring-cloud/gateway/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# stage 1
2+
# Start with a base image containing Java runtime
3+
# TODO :: Upgrade to slim jre version for 17 once available
4+
FROM openjdk:17-alpine as builder
5+
WORKDIR application
6+
ARG JAR_FILE=target/*.jar
7+
COPY ${JAR_FILE} application.jar
8+
RUN java -Djarmode=layertools -jar application.jar extract
9+
10+
# the second stage of our build will copy the extracted layers
11+
FROM openjdk:17-alpine
12+
WORKDIR application
13+
COPY --from=builder application/dependencies/ ./
14+
COPY --from=builder application/spring-boot-loader/ ./
15+
COPY --from=builder application/snapshot-dependencies/ ./
16+
COPY --from=builder application/application/ ./
17+
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.javatab.springcloud.gateway.configuration;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.actuate.health.CompositeReactiveHealthContributor;
7+
import org.springframework.boot.actuate.health.Health;
8+
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
9+
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.web.reactive.function.client.WebClient;
13+
import reactor.core.publisher.Mono;
14+
15+
import java.util.LinkedHashMap;
16+
import java.util.Map;
17+
18+
import static java.util.logging.Level.FINE;
19+
20+
@Configuration
21+
public class HealthCheckConfiguration {
22+
23+
private static final Logger LOG = LoggerFactory.getLogger(HealthCheckConfiguration.class);
24+
25+
private WebClient webClient;
26+
27+
@Autowired
28+
public HealthCheckConfiguration(WebClient.Builder webClientBuilder) {
29+
this.webClient = webClientBuilder.build();
30+
}
31+
32+
@Bean
33+
ReactiveHealthContributor healthCheckMicroservices() {
34+
35+
final Map<String, ReactiveHealthIndicator> registry = new LinkedHashMap<>();
36+
37+
registry.put("course", () -> getHealth("http://course"));
38+
registry.put("student", () -> getHealth("http://student"));
39+
registry.put("vote", () -> getHealth("http://vote"));
40+
registry.put("search", () -> getHealth("http://search"));
41+
registry.put("course-composite", () -> getHealth("http://course-composite"));
42+
43+
return CompositeReactiveHealthContributor.fromMap(registry);
44+
}
45+
46+
private Mono<Health> getHealth(String baseUrl) {
47+
String url = baseUrl + "/actuator/health";
48+
LOG.debug("Setting up a call to the Health API on URL: {}", url);
49+
return webClient.get().uri(url).retrieve().bodyToMono(String.class)
50+
.map(s -> new Health.Builder().up().build())
51+
.onErrorResume(ex -> Mono.just(new Health.Builder().down(ex).build()))
52+
.log(LOG.getName(), FINE);
53+
}
54+
}

0 commit comments

Comments
 (0)