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

Tema 5 Eureka + Tema 6 API-Gateway #84

Open
wants to merge 2 commits into
base: lab6
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions eureka/persons-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,48 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Alpha6</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
</dependency>

</dependencies>
<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.eureka.persons;

import com.eureka.persons.base.AbstractEntity;
import com.eureka.persons.ex.NotFoundException;
import com.eureka.persons.person.Person;
import com.eureka.persons.services.PersonService;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/persons")
Expand All @@ -26,7 +29,10 @@ public PersonsController(PersonService personService) {
@ResponseStatus(HttpStatus.OK)
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<Person> list() {
return new ArrayList<>();
List<Person> people = personService.findAll();
people.sort(Comparator.comparing(AbstractEntity::getId));

return people;
}

/**
Expand All @@ -36,6 +42,11 @@ public List<Person> list() {
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public void create(@RequestBody Person person, BindingResult result) {
if (result.hasErrors()) {
throw new PersonsException(HttpStatus.BAD_REQUEST, "Person could not be created!");
}

personService.save(person);
}

/**
Expand All @@ -48,7 +59,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));
}

/**
Expand All @@ -62,6 +73,16 @@ public Person show(@PathVariable Long id) {
@ResponseStatus(HttpStatus.NO_CONTENT)
@PutMapping("/{id}")
public void update(@RequestBody Person updatedPerson, @PathVariable Long id) {
Person person = show(id);

person.setUsername(updatedPerson.getUsername());
person.setFirstName(updatedPerson.getFirstName());
person.setLastName(updatedPerson.getLastName());
person.setPassword(updatedPerson.getPassword());
person.setHiringDate(updatedPerson.getHiringDate());
person.setNewPassword(updatedPerson.getNewPassword());

personService.save(person);
}

/**
Expand All @@ -73,5 +94,7 @@ public void update(@RequestBody Person updatedPerson, @PathVariable Long id) {
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
Person person = show(id);
personService.delete(person);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.eureka.persons.base;

import com.eureka.persons.util.DateProcessor;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import org.springframework.format.annotation.DateTimeFormat;

import com.eureka.persons.util.DateProcessor;
import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Getter;
import lombok.Setter;

@MappedSuperclass
@Getter
@Setter
Expand All @@ -24,6 +32,14 @@ public abstract class AbstractEntity implements Serializable {
@Column(updatable = false)
protected Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@Version
protected int version;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
@Setter
@NoArgsConstructor
public class Person extends AbstractEntity {
interface BasicValidation{}
interface BasicValidation {
}

@NotNull(groups = BasicValidation.class)
@Size(min = 3, max = 30, groups = BasicValidation.class)
Expand Down Expand Up @@ -77,6 +78,54 @@ public String toString() {
return String.format("Person[username='%s', firstName='%s', lastName='%s', hiringDate='%s']\n",
username, firstName, lastName, hiringDate.toString());


}

public String getUsername() {
return username;
}

}
public void setUsername(String username) {
this.username = username;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public LocalDateTime getHiringDate() {
return hiringDate;
}

public void setHiringDate(LocalDateTime hiringDate) {
this.hiringDate = hiringDate;
}

public String getNewPassword() {
return newPassword;
}

public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
}
5 changes: 5 additions & 0 deletions eureka/persons-server/src/main/resources/persons-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
server:
port: 8080
address: 0.0.0.0

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/**

#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)
Expand Down
31 changes: 17 additions & 14 deletions lab-6-api-gateway/service1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<dependencies>
<!-- <dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Expand All @@ -30,24 +30,27 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 Service1Application {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.service1.controller;

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(value = "/greeting/{name}")
public String greeting(@PathVariable String name, @RequestHeader Map<String, String> headers) {
System.out.println("Headers:");
for (Map.Entry<String, String> header : headers.entrySet()) {
System.out.println(header);
}
return String.format("Hello, %s!", name);
}
}
Loading