Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Spring Cloud Hoxton #139 #140

Merged
merged 8 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ the URL for an event stream to monitor, for example the `api-gateway` service ru
or running into docker: `http://api-gateway:8080/actuator/hystrix.stream`


You can tell Config Server to use your local Git repository by using `local` Spring profile and setting
You can tell Config Server to use your local Git repository by using `native` Spring profile and setting
`GIT_REPO` environment variable, for example:
`-Dspring.profiles.active=local -DGIT_REPO=/projects/spring-petclinic-microservices-config`
`-Dspring.profiles.active=native -DGIT_REPO=/projects/spring-petclinic-microservices-config`

## Starting services locally with docker-compose
In order to start entire infrastructure using Docker, you have to build images by executing `./mvnw clean install -PbuildDocker`
Expand Down
28 changes: 21 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<version>2.2.1.RELEASE</version>
</parent>

<groupId>org.springframework.samples</groupId>
<artifactId>spring-petclinic-microservices</artifactId>
<version>2.1.4</version>
<version>2.2.1</version>
<name>${project.artifactId}</name>
<packaging>pom</packaging>

Expand All @@ -30,8 +30,8 @@
<java.version>1.8</java.version>
<assertj.version>3.11.1</assertj.version>

<spring-boot.version>2.1.2.RELEASE</spring-boot.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<spring-boot.version>2.2.0.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.RC2</spring-cloud.version>

<maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version>

Expand Down Expand Up @@ -177,10 +177,24 @@

<repositories>
<repository>
<id>repository.spring.milestone</id>
<name>Spring Milestone Repository</name>
<url>http://repo.spring.io/milestone</url>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>
2 changes: 1 addition & 1 deletion spring-petclinic-admin-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.springframework.samples</groupId>
<artifactId>spring-petclinic-microservices</artifactId>
<version>2.1.4</version>
<version>2.2.1</version>
</parent>

<properties>
Expand Down
27 changes: 26 additions & 1 deletion spring-petclinic-api-gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.samples</groupId>
<artifactId>spring-petclinic-microservices</artifactId>
<version>2.1.4</version>
<version>2.2.1</version>
</parent>

<properties>
Expand Down Expand Up @@ -60,6 +60,20 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-ribbon</artifactId>
</exclusion>
<exclusion>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-eureka</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
Expand All @@ -72,6 +86,12 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Third parties -->
Expand Down Expand Up @@ -125,6 +145,11 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
Expand All @@ -50,6 +51,12 @@ RestTemplate loadBalancedRestTemplate() {
return new RestTemplate();
}

@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}

@Value("classpath:/static/index.html")
private Resource indexHtml;

Expand All @@ -61,7 +68,7 @@ RestTemplate loadBalancedRestTemplate() {
RouterFunction<?> routerFunction() {
RouterFunction router = RouterFunctions.resources("/**", new ClassPathResource("static/"))
.andRoute(RequestPredicates.GET("/"),
request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).syncBody(indexHtml));
request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(indexHtml));
return router;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.springframework.samples.petclinic.api.dto.OwnerDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
* @author Maciej Szarlinski
Expand All @@ -27,9 +29,12 @@
@RequiredArgsConstructor
public class CustomersServiceClient {

private final RestTemplate loadBalancedRestTemplate;
private final WebClient.Builder webClientBuilder;

public OwnerDetails getOwner(final int ownerId) {
return loadBalancedRestTemplate.getForObject("http://customers-service/owners/{ownerId}", OwnerDetails.class, ownerId);
public Mono<OwnerDetails> getOwner(final int ownerId) {
return webClientBuilder.build().get()
.uri("http://customers-service/owners/{ownerId}", ownerId)
.retrieve()
.bodyToMono(OwnerDetails.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@
*/
package org.springframework.samples.petclinic.api.application;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.RequiredArgsConstructor;
import org.springframework.samples.petclinic.api.dto.VisitDetails;
import org.springframework.samples.petclinic.api.dto.Visits;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static org.springframework.web.util.UriComponentsBuilder.fromHttpUrl;

/**
* @author Maciej Szarlinski
Expand All @@ -39,24 +35,29 @@
@RequiredArgsConstructor
public class VisitsServiceClient {

private final RestTemplate loadBalancedRestTemplate;
// Could be changed for testing purpose
private String hostname = "http://visits-service/";

@HystrixCommand(fallbackMethod = "emptyVisitsForPets")
public Map<Integer, List<VisitDetails>> getVisitsForPets(final List<Integer> petIds) {
UriComponentsBuilder builder = fromHttpUrl("http://visits-service/pets/visits")
.queryParam("petId", joinIds(petIds));
private final WebClient.Builder webClientBuilder;

return loadBalancedRestTemplate.getForObject(builder.toUriString(), Visits.class)
.getItems()
.stream()
.collect(groupingBy(VisitDetails::getPetId));
// FIXME HYSTRIX @HystrixCommand(fallbackMethod = "emptyVisitsForPets")
public Mono<Visits> getVisitsForPets(final List<Integer> petIds) {
return webClientBuilder.build()
.get()
.uri(hostname + "pets/visits?petId={petId}", joinIds(petIds))
.retrieve()
.bodyToMono(Visits.class);
}

private String joinIds(List<Integer> petIds) {
return petIds.stream().map(Object::toString).collect(joining(","));
}

private Map<Integer, List<VisitDetails>> emptyVisitsForPets(List<Integer> petIds) {
return Collections.emptyMap();
private Mono<Map<Integer, List<VisitDetails>>> emptyVisitsForPets(Mono<List<Integer>> petIds) {
return Mono.just(Collections.emptyMap());
}

void setHostname(String hostname) {
this.hostname = hostname;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@
*/
package org.springframework.samples.petclinic.api.boundary.web;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.samples.petclinic.api.application.CustomersServiceClient;
import org.springframework.samples.petclinic.api.dto.OwnerDetails;
import org.springframework.samples.petclinic.api.application.VisitsServiceClient;
import org.springframework.samples.petclinic.api.dto.VisitDetails;
import org.springframework.samples.petclinic.api.dto.OwnerDetails;
import org.springframework.samples.petclinic.api.dto.Visits;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;


import static java.util.Collections.emptyList;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author Maciej Szarlinski
Expand All @@ -44,14 +42,24 @@ public class ApiGatewayController {
private final VisitsServiceClient visitsServiceClient;

@GetMapping(value = "owners/{ownerId}")
public OwnerDetails getOwnerDetails(final @PathVariable int ownerId) {
final OwnerDetails owner = customersServiceClient.getOwner(ownerId);
supplyVisits(owner, visitsServiceClient.getVisitsForPets(owner.getPetIds()));
return owner;
public Mono<OwnerDetails> getOwnerDetails(final @PathVariable int ownerId) {
return customersServiceClient.getOwner(ownerId)
.flatMap(owner ->
visitsServiceClient.getVisitsForPets(owner.getPetIds())
.map(addVisitsToOwner(owner))
);

}

private void supplyVisits(final OwnerDetails owner, final Map<Integer, List<VisitDetails>> visitsMapping) {
owner.getPets().forEach(pet ->
pet.getVisits().addAll(Optional.ofNullable(visitsMapping.get(pet.getId())).orElse(emptyList())));
private Function<Visits, OwnerDetails> addVisitsToOwner(OwnerDetails owner) {
return visits -> {
owner.getPets()
.forEach(pet -> pet.getVisits()
.addAll(visits.getItems().stream()
.filter(v -> v.getPetId() == pet.getId())
.collect(Collectors.toList()))
);
return owner;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
spring:
cloud:
loadbalancer:
ribbon:
enabled: false
gateway:
routes:
- id: vets-service
Expand Down
Loading