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 - ApiGateway #93

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,6 +9,7 @@
import org.springframework.web.bind.annotation.*;

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

@RestController
Expand All @@ -22,20 +24,31 @@ public PersonsController(PersonService personService) {
/**
* Handles requests to list all persons.
*/
//TODO find all persons using the functions already implemented and sort them by id
@ResponseStatus(HttpStatus.OK)
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<Person> list() {
return new ArrayList<>();
List<Person> persons = personService.findAll();
persons.sort(new Comparator<Person>() {
@Override
public int compare(Person person1, Person person2) {
return person1.getId().compareTo(person2.getId());
}
});

return persons;
}

/**
* Handles requests to create a person.
*/
//TODO save a person to the db or throw PersonsException
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
@PostMapping(value = "/create")
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!");
}

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

/**
Expand All @@ -73,5 +95,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)));
}
}
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
7 changes: 6 additions & 1 deletion 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 All @@ -38,4 +43,4 @@ logging:
level:
root: INFO
org.springframework: DEBUG
com.apress.cems: DEBUG
com.apress.cems: DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

@SpringBootTest
class EurekaApplicationTests {

@Test
void contextLoads() {
}
//
// @Test
// void contextLoads() {
// }

}
4 changes: 4 additions & 0 deletions lab-6-api-gateway/api-gateway-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaClient
public class ApiGatewayProjectApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
server:
port: 8080
#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)
#and method types (GET,POST)

server:
port: 8080
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/**
12 changes: 12 additions & 0 deletions lab-6-api-gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
<version>3.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
<version>3.1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
17 changes: 15 additions & 2 deletions lab-6-api-gateway/service1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
<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 @@ -36,6 +37,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
public class Service1Application {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.service1;

import com.netflix.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
@RequestMapping("/api")
public class Service1Controller {

@GetMapping("/greeting/{name}")
public String greeting(@PathVariable String name) {
System.out.println(name);
return "Hello " + name + "!";
}
}
12 changes: 11 additions & 1 deletion lab-6-api-gateway/service1/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
server:
port: 8081
port: 8081
spring:
application:
name: service1

eureka:
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka/
register-with-eureka: false
fetch-registry: false
34 changes: 32 additions & 2 deletions lab-6-api-gateway/service2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,50 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-client</artifactId>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.service2;

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 {
@Id
@Column(name = "id", nullable = false)
private Long id;

@NotNull
@Column(nullable = false, unique = true)
private String name;

@NotNull
@Column(nullable = false, unique = true)
private int quantity;
}
Loading