From 62c99d53dd81752976e1995da642978998766ce4 Mon Sep 17 00:00:00 2001 From: GeorgeRosianu <82835306+GeorgeRosianu@users.noreply.github.com> Date: Thu, 5 May 2022 20:42:42 +0300 Subject: [PATCH] Tema 5 + Tema 6 --- .../example/discovery/DiscoveryServer.java | 2 ++ .../src/main/resources/discovery-server.yml | 8 +++++ .../com/eureka/persons/PersonsController.java | 28 +++++++++++++-- .../com/eureka/persons/PersonsServer.java | 2 ++ .../src/main/resources/persons-server.yml | 5 +++ lab-6-api-gateway/api-gateway-project/pom.xml | 4 +++ .../ApiGatewayProjectApplication.java | 2 ++ .../src/main/resources/application.yml | 26 +++++++++++++- lab-6-api-gateway/service1/pom.xml | 26 +++++++------- .../example/service1/Service1Application.java | 2 ++ .../example/service1/Service1Controller.java | 21 +++++++++++ .../src/main/resources/application.yml | 13 ++++++- lab-6-api-gateway/service2/pom.xml | 19 ++++++++-- .../example/service2/Service2Application.java | 2 ++ .../example/service2/Service2Controller.java | 35 +++++++++++++++++++ .../com/example/service2/product/Product.java | 25 +++++++++++++ .../src/main/resources/application.yml | 14 +++++++- 17 files changed, 213 insertions(+), 21 deletions(-) create mode 100644 lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Controller.java create mode 100644 lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Controller.java create mode 100644 lab-6-api-gateway/service2/src/main/java/com/example/service2/product/Product.java diff --git a/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java index 15d783c..8a91eb8 100644 --- a/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java +++ b/eureka/discovery-server/src/main/java/com/example/discovery/DiscoveryServer.java @@ -4,10 +4,12 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import java.io.IOException; @SpringBootApplication +@EnableEurekaServer public class DiscoveryServer { private static Logger logger = LoggerFactory.getLogger(DiscoveryServer.class); diff --git a/eureka/discovery-server/src/main/resources/discovery-server.yml b/eureka/discovery-server/src/main/resources/discovery-server.yml index b55b43b..048af49 100644 --- a/eureka/discovery-server/src/main/resources/discovery-server.yml +++ b/eureka/discovery-server/src/main/resources/discovery-server.yml @@ -11,3 +11,11 @@ logging: root: INFO org.springframework: DEBUG com.apress.cems: DEBUG + +server: + port: 3000 + +eureka: + client: + register-with-eureka: false + fetch-registry: false \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java index ff4acb5..abf23ff 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsController.java @@ -1,5 +1,6 @@ package com.eureka.persons; +import com.eureka.persons.ex.NotFoundException; import com.eureka.persons.person.Person; import com.eureka.persons.services.PersonService; import org.springframework.http.HttpStatus; @@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.*; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; @RestController @@ -26,7 +28,15 @@ public PersonsController(PersonService personService) { @ResponseStatus(HttpStatus.OK) @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) public List list() { - return new ArrayList<>(); + List persons = personService.findAll(); + persons.sort(new Comparator() { + @Override + public int compare(Person person1, Person person2) { + return person1.getId().compareTo(person2.getId()); + } + }); + + return persons; } /** @@ -36,6 +46,10 @@ public List list() { @ResponseStatus(HttpStatus.CREATED) @PostMapping public void create(@RequestBody Person person, BindingResult result) { + if (!result.hasErrors()) + personService.save(person); + else + throw new PersonsException(HttpStatus.BAD_REQUEST, "It can't add a new person!"); } /** @@ -48,7 +62,7 @@ public void create(@RequestBody Person person, BindingResult result) { @ResponseStatus(HttpStatus.OK) @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public Person show(@PathVariable Long id) { - return new Person(); + return personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id)); } /** @@ -62,6 +76,15 @@ public Person show(@PathVariable Long id) { @ResponseStatus(HttpStatus.NO_CONTENT) @PutMapping("/{id}") public void update(@RequestBody Person updatedPerson, @PathVariable Long id) { + Person resultPerson = personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id)); + resultPerson.setFirstName(updatedPerson.getFirstName()); + resultPerson.setLastName(updatedPerson.getLastName()); + resultPerson.setUsername(updatedPerson.getUsername()); + resultPerson.setPassword(updatedPerson.getPassword()); + resultPerson.setHiringDate(updatedPerson.getHiringDate()); + resultPerson.setNewPassword(updatedPerson.getNewPassword()); + + personService.save(resultPerson); } /** @@ -73,5 +96,6 @@ public void update(@RequestBody Person updatedPerson, @PathVariable Long id) { @ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { + personService.delete(personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, id))); } } \ No newline at end of file diff --git a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java index f2098ae..c23be15 100644 --- a/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java +++ b/eureka/persons-server/src/main/java/com/eureka/persons/PersonsServer.java @@ -5,11 +5,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import java.io.IOException; @EntityScan(basePackages = "com.eureka.persons") @SpringBootApplication +@EnableEurekaClient public class PersonsServer { private static Logger logger = LoggerFactory.getLogger(PersonsServer.class); diff --git a/eureka/persons-server/src/main/resources/persons-server.yml b/eureka/persons-server/src/main/resources/persons-server.yml index b2b466d..ae83478 100644 --- a/eureka/persons-server/src/main/resources/persons-server.yml +++ b/eureka/persons-server/src/main/resources/persons-server.yml @@ -25,6 +25,11 @@ server: # Discovery Server Access #TODO here you add configurations for eureka client +eureka: + client: + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + fetchRegistry: true info: app: diff --git a/lab-6-api-gateway/api-gateway-project/pom.xml b/lab-6-api-gateway/api-gateway-project/pom.xml index 871ce8d..a3c1f42 100644 --- a/lab-6-api-gateway/api-gateway-project/pom.xml +++ b/lab-6-api-gateway/api-gateway-project/pom.xml @@ -38,6 +38,10 @@ spring-boot-starter-test test + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + io.projectreactor reactor-test diff --git a/lab-6-api-gateway/api-gateway-project/src/main/java/com/example/apigatewayproject/ApiGatewayProjectApplication.java b/lab-6-api-gateway/api-gateway-project/src/main/java/com/example/apigatewayproject/ApiGatewayProjectApplication.java index e0f6114..16086b5 100644 --- a/lab-6-api-gateway/api-gateway-project/src/main/java/com/example/apigatewayproject/ApiGatewayProjectApplication.java +++ b/lab-6-api-gateway/api-gateway-project/src/main/java/com/example/apigatewayproject/ApiGatewayProjectApplication.java @@ -2,7 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +@EnableEurekaClient @SpringBootApplication public class ApiGatewayProjectApplication { diff --git a/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml b/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml index aaad35d..eb41d45 100644 --- a/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml +++ b/lab-6-api-gateway/api-gateway-project/src/main/resources/application.yml @@ -3,4 +3,28 @@ server: #TODO use eureka to discover the URL for the service1 and service2 #TODO configure spring cloud gateway to route the request to downstream services (service1 and service2) based on the paths(/api/greeting, /product) #TODO for greeting endpoint add a route to accept requests to /greeting but before calling service1 it must append api before the greeting path (HINT: rewrite path filter) -#and method types (GET,POST) \ No newline at end of file +#and method types (GET,POST) + +eureka: + client: + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + register-with-eureka: true + fetch-registry: true + +spring: + application: + name: gateway + main: + web-application-type: reactive + cloud: + gateway: + routes: + - id: service1 + uri: http://localhost:8081 + predicates: + - Path=/api/greeting/** + - id: service2 + uri: http://localhost:8082 + predicates: + - Path=/product/** \ No newline at end of file diff --git a/lab-6-api-gateway/service1/pom.xml b/lab-6-api-gateway/service1/pom.xml index 478fe3f..4458413 100644 --- a/lab-6-api-gateway/service1/pom.xml +++ b/lab-6-api-gateway/service1/pom.xml @@ -18,10 +18,10 @@ 2021.0.1 - + org.springframework.boot spring-boot-starter-actuator @@ -37,17 +37,17 @@ test - - - - org.springframework.cloud - spring-cloud-dependencies - ${spring-cloud.version} - pom - import - - - + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + diff --git a/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java index 27128b5..65b1c23 100644 --- a/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java +++ b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Application.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication +@EnableEurekaClient public class Service1Application { public static void main(String[] args) { diff --git a/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Controller.java b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Controller.java new file mode 100644 index 0000000..df5f31a --- /dev/null +++ b/lab-6-api-gateway/service1/src/main/java/com/example/service1/Service1Controller.java @@ -0,0 +1,21 @@ +package com.example.service1; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; + +@RestController +@RequestMapping("/api") +public class Service1Controller { + + @ResponseStatus(HttpStatus.OK) + @GetMapping("/greeting/{name}") + public String greeting(@PathVariable String name, @RequestHeader Map headers) { + System.out.println("Headers:"); + for (Map.Entry header : headers.entrySet()) { + System.out.println(header); + } + return "Hello " + name + "!"; + } +} diff --git a/lab-6-api-gateway/service1/src/main/resources/application.yml b/lab-6-api-gateway/service1/src/main/resources/application.yml index 54b155f..544bba3 100644 --- a/lab-6-api-gateway/service1/src/main/resources/application.yml +++ b/lab-6-api-gateway/service1/src/main/resources/application.yml @@ -1,2 +1,13 @@ server: - port: 8081 \ No newline at end of file + port: 8081 + +spring: + application: + name: service1 + +eureka: + client: + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + register-with-eureka: true + fetch-registry: true \ No newline at end of file diff --git a/lab-6-api-gateway/service2/pom.xml b/lab-6-api-gateway/service2/pom.xml index be06918..1682b09 100644 --- a/lab-6-api-gateway/service2/pom.xml +++ b/lab-6-api-gateway/service2/pom.xml @@ -26,11 +26,24 @@ org.springframework.boot spring-boot-starter-web - - + + + jakarta.persistence + jakarta.persistence-api + + + org.jetbrains + annotations + RELEASE + compile + + + org.projectlombok + lombok + org.springframework.boot spring-boot-starter-test diff --git a/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Application.java b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Application.java index 3c2fbd2..383d462 100644 --- a/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Application.java +++ b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Application.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication +@EnableEurekaClient public class Service2Application { public static void main(String[] args) { diff --git a/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Controller.java b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Controller.java new file mode 100644 index 0000000..9130aef --- /dev/null +++ b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Controller.java @@ -0,0 +1,35 @@ +package com.example.service2; + +import com.example.service2.product.Product; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/product") +public class Service2Controller { + + private List products = new ArrayList(); + + @ResponseStatus(HttpStatus.OK) + @PostMapping() + public void addProduct(@RequestBody Product product, @RequestHeader Map headers) { + for(Map.Entry header : headers.entrySet()) { + System.out.println(header); + } + products.add(product); + } + + @ResponseStatus(HttpStatus.OK) + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) + public List getProducts(@RequestHeader Map headers) { + for(Map.Entry header : headers.entrySet()) { + System.out.println(header); + } + return products; + } +} diff --git a/lab-6-api-gateway/service2/src/main/java/com/example/service2/product/Product.java b/lab-6-api-gateway/service2/src/main/java/com/example/service2/product/Product.java new file mode 100644 index 0000000..980c919 --- /dev/null +++ b/lab-6-api-gateway/service2/src/main/java/com/example/service2/product/Product.java @@ -0,0 +1,25 @@ +package com.example.service2.product; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.jetbrains.annotations.NotNull; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +@Getter +@Setter +@NoArgsConstructor +public class Product { + + @NotNull + @Column(nullable = false, unique = true) + private String name; + + @NotNull + @Column(nullable = false, unique = true) + private int quantity; +} diff --git a/lab-6-api-gateway/service2/src/main/resources/application.yml b/lab-6-api-gateway/service2/src/main/resources/application.yml index 4772153..530ed6d 100644 --- a/lab-6-api-gateway/service2/src/main/resources/application.yml +++ b/lab-6-api-gateway/service2/src/main/resources/application.yml @@ -1,3 +1,15 @@ server: - port: 8082 \ No newline at end of file + port: 8082 +spring: + application: + name: service2 + main: + allow-bean-definition-overriding: true + +eureka: + client: + serviceUrl: + defaultZone: http://localhost:3000/eureka/ + register-with-eureka: true + fetch-registry: true \ No newline at end of file