From 395612bcf345f5af2f4ab08b12f43fdfff67cd15 Mon Sep 17 00:00:00 2001 From: Maciej Szarlinski <mszarlinski@gmail.com> Date: Sat, 5 Nov 2016 20:38:01 +0100 Subject: [PATCH] Zuul API Gateway --- petclinic-api-gateway/.gitignore | 14 ++++ petclinic-api-gateway/pom.xml | 73 +++++++++++++++++++ .../petclinic/api/ApiGatewayApplication.java | 16 ++++ .../src/main/resources/bootstrap.yml | 6 ++ .../api/ApiGatewayApplicationTests.java | 16 ++++ .../boundary/web/owner/OwnerResource.java | 11 +-- .../clients/boundary/web/pet/PetResource.java | 27 +++---- .../boundary/web/pet/PetValidator.java | 1 + .../vets/web/boundary/VetResource.java | 6 +- .../boundary/web/visit/VisitResource.java | 18 ++--- pom.xml | 1 + 11 files changed, 160 insertions(+), 29 deletions(-) create mode 100644 petclinic-api-gateway/.gitignore create mode 100644 petclinic-api-gateway/pom.xml create mode 100644 petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java create mode 100644 petclinic-api-gateway/src/main/resources/bootstrap.yml create mode 100644 petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/ApiGatewayApplicationTests.java diff --git a/petclinic-api-gateway/.gitignore b/petclinic-api-gateway/.gitignore new file mode 100644 index 00000000..b06a9e2c --- /dev/null +++ b/petclinic-api-gateway/.gitignore @@ -0,0 +1,14 @@ +# Maven +target/ + +# Eclipse +.settings/ +.classpath +.project + +# IntelliJ IDEA +.idea +*.iml + +# Branch switching +generated/ diff --git a/petclinic-api-gateway/pom.xml b/petclinic-api-gateway/pom.xml new file mode 100644 index 00000000..b28dd9c8 --- /dev/null +++ b/petclinic-api-gateway/pom.xml @@ -0,0 +1,73 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.springframework.samples.petclinic.api</groupId> + <artifactId>petclinic-api-gateway</artifactId> + <version>1.4.1</version> + <packaging>jar</packaging> + + <name>petclinic-api-gateway</name> + <description>PetClinic API Gateway</description> + + <parent> + <groupId>org.springframework.samples</groupId> + <artifactId>springboot-petclinic</artifactId> + <version>1.4.1</version> + </parent> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + <java.version>1.8</java.version> + </properties> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-actuator</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-config</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-zuul</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-dependencies</artifactId> + <version>Camden.SR1</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + </plugins> + </build> + + +</project> diff --git a/petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java b/petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java new file mode 100644 index 00000000..ed03d994 --- /dev/null +++ b/petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/ApiGatewayApplication.java @@ -0,0 +1,16 @@ +package org.springframework.samples.petclinic.api; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; + +@EnableZuulProxy +@EnableDiscoveryClient +@SpringBootApplication +public class ApiGatewayApplication { + + public static void main(String[] args) { + SpringApplication.run(ApiGatewayApplication.class, args); + } +} diff --git a/petclinic-api-gateway/src/main/resources/bootstrap.yml b/petclinic-api-gateway/src/main/resources/bootstrap.yml new file mode 100644 index 00000000..42caccbd --- /dev/null +++ b/petclinic-api-gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,6 @@ +spring: + cloud: + config: + uri: http://localhost:8888 + application: + name: api-gateway diff --git a/petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/ApiGatewayApplicationTests.java b/petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/ApiGatewayApplicationTests.java new file mode 100644 index 00000000..3ea1424c --- /dev/null +++ b/petclinic-api-gateway/src/test/java/org/springframework/samples/petclinic/api/ApiGatewayApplicationTests.java @@ -0,0 +1,16 @@ +package org.springframework.samples.petclinic.api; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApiGatewayApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/owner/OwnerResource.java b/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/owner/OwnerResource.java index f27f2eb6..aa045e86 100644 --- a/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/owner/OwnerResource.java +++ b/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/owner/OwnerResource.java @@ -17,8 +17,8 @@ package org.springframework.samples.petclinic.clients.boundary.web.owner; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; -import org.springframework.samples.petclinic.clients.domain.model.owner.Owner; import org.springframework.samples.petclinic.clients.application.OwnerService; +import org.springframework.samples.petclinic.clients.domain.model.owner.Owner; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; @@ -31,6 +31,7 @@ import java.util.Collection; * @author Arjen Poutsma * @author Michael Isvy */ +@RequestMapping("/owners") @RestController public class OwnerResource { @@ -53,7 +54,7 @@ public class OwnerResource { /** * Create Owner */ - @RequestMapping(value = "/owner", method = RequestMethod.POST) + @PostMapping @ResponseStatus(HttpStatus.CREATED) public void createOwner(@Valid @RequestBody Owner owner) { this.ownerService.saveOwner(owner); @@ -62,7 +63,7 @@ public class OwnerResource { /** * Read single Owner */ - @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.GET) + @GetMapping(value = "/{ownerId}") public Owner findOwner(@PathVariable("ownerId") int ownerId) { return retrieveOwner(ownerId); } @@ -70,7 +71,7 @@ public class OwnerResource { /** * Read List of Owners */ - @GetMapping("/owner/list") + @GetMapping public Collection<Owner> findAll() { return ownerService.findAll(); } @@ -78,7 +79,7 @@ public class OwnerResource { /** * Update Owner */ - @RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.PUT) + @PutMapping(value = "/{ownerId}") public Owner updateOwner(@PathVariable("ownerId") int ownerId, @Valid @RequestBody Owner ownerRequest) { Owner ownerModel = retrieveOwner(ownerId); // This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct. diff --git a/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/pet/PetResource.java b/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/pet/PetResource.java index 07c15f85..2ebc2d79 100644 --- a/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/pet/PetResource.java +++ b/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/pet/PetResource.java @@ -19,16 +19,16 @@ import com.fasterxml.jackson.annotation.JsonFormat; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; -import org.springframework.samples.petclinic.clients.domain.model.owner.Owner; import org.springframework.samples.petclinic.clients.application.OwnerService; -import org.springframework.samples.petclinic.clients.domain.model.pet.Pet; import org.springframework.samples.petclinic.clients.application.PetService; +import org.springframework.samples.petclinic.clients.domain.model.owner.Owner; +import org.springframework.samples.petclinic.clients.domain.model.pet.Pet; import org.springframework.samples.petclinic.clients.domain.model.pet.PetType; import org.springframework.web.bind.annotation.*; import javax.validation.constraints.Size; +import java.util.Collection; import java.util.Date; -import java.util.Map; /** * @author Juergen Hoeller @@ -49,18 +49,19 @@ public class PetResource { } @GetMapping("/petTypes") - Object getPetTypes() { + public Collection<PetType> getPetTypes() { return petService.findPetTypes(); } - @GetMapping("/owners/{ownerId}/pets/new") - public String initCreationForm(@PathVariable("ownerId") int ownerId, Map<String, Object> model) { - Owner owner = ownerService.findOwnerById(ownerId); - Pet pet = new Pet(); - owner.addPet(pet); - model.put("pet", pet); - return "pets/createOrUpdatePetForm"; - } + //TODO: unused? +// @GetMapping("/owners/{ownerId}/pets/new") +// public String initCreationForm(@PathVariable("ownerId") int ownerId, Map<String, Object> model) { +// Owner owner = ownerService.findOwnerById(ownerId); +// Pet pet = new Pet(); +// owner.addPet(pet); +// model.put("pet", pet); +// return "pets/createOrUpdatePetForm"; +// } @PostMapping("/owners/{ownerId}/pets") @ResponseStatus(HttpStatus.NO_CONTENT) @@ -92,7 +93,7 @@ public class PetResource { petService.savePet(pet); } - @GetMapping("/owner/*/pet/{petId}") + @GetMapping("/pets/{petId}") public PetDetails findPet(@PathVariable("petId") int petId) { return new PetDetails(petService.findPetById(petId)); } diff --git a/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/pet/PetValidator.java b/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/pet/PetValidator.java index 9484bbb5..9f888d21 100644 --- a/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/pet/PetValidator.java +++ b/petclinic-clients-service/src/main/java/org/springframework/samples/petclinic/clients/boundary/web/pet/PetValidator.java @@ -25,6 +25,7 @@ import org.springframework.validation.Errors; * @author Ken Krebs * @author Juergen Hoeller */ +//TODO: unused? public class PetValidator { public void validate(Pet pet, Errors errors) { diff --git a/petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/boundary/VetResource.java b/petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/boundary/VetResource.java index 76f75e5f..c05c7d0f 100644 --- a/petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/boundary/VetResource.java +++ b/petclinic-vets-service/src/main/java/org/springframework/samples/petclinic/vets/web/boundary/VetResource.java @@ -16,9 +16,10 @@ package org.springframework.samples.petclinic.vets.web.boundary; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.samples.petclinic.vets.domain.model.vet.Vet; import org.springframework.samples.petclinic.vets.application.VetService; +import org.springframework.samples.petclinic.vets.domain.model.vet.Vet; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Collection; @@ -29,6 +30,7 @@ import java.util.Collection; * @author Ken Krebs * @author Arjen Poutsma */ +@RequestMapping("/vets") @RestController public class VetResource { @@ -39,7 +41,7 @@ public class VetResource { this.vetService = vetService; } - @GetMapping("/vets") + @GetMapping public Collection<Vet> showResourcesVetList() { return vetService.findVets(); } diff --git a/petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/boundary/web/visit/VisitResource.java b/petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/boundary/web/visit/VisitResource.java index 68b79228..8f528d1b 100644 --- a/petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/boundary/web/visit/VisitResource.java +++ b/petclinic-visits-service/src/main/java/org/springframework/samples/petclinic/visits/boundary/web/visit/VisitResource.java @@ -15,14 +15,14 @@ */ package org.springframework.samples.petclinic.visits.boundary.web.visit; - import org.springframework.beans.factory.annotation.Autowired; - import org.springframework.http.HttpStatus; - import org.springframework.samples.petclinic.visits.domain.model.visit.Visit; - import org.springframework.samples.petclinic.visits.application.VisitService; - import org.springframework.web.bind.annotation.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.samples.petclinic.visits.application.VisitService; +import org.springframework.samples.petclinic.visits.domain.model.visit.Visit; +import org.springframework.web.bind.annotation.*; - import javax.validation.Valid; - import java.util.List; +import javax.validation.Valid; +import java.util.List; /** * @author Juergen Hoeller @@ -40,7 +40,7 @@ public class VisitResource { this.visitService = visitService; } - @PostMapping("/owners/{ownerId}/pets/{petId}/visits") + @PostMapping("/pets/{petId}/visits") @ResponseStatus(HttpStatus.NO_CONTENT) public void create( @Valid @RequestBody Visit visit, @@ -50,7 +50,7 @@ public class VisitResource { visitService.saveVisit(visit); } - @GetMapping("/owners/{ownerId}/pets/{petId}/visits") + @GetMapping("/pets/{petId}/visits") public List<Visit> visits(@PathVariable("petId") int petId) { return visitService.findVisitsByPetId(petId); } diff --git a/pom.xml b/pom.xml index fecfcb2a..008eab2c 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ <module>petclinic-visits-service</module> <module>petclinic-config-server</module> <module>petclinic-discovery-server</module> + <module>petclinic-api-gateway</module> </modules> <properties> -- GitLab