Skip to content
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 @@ -4,6 +4,13 @@ spring:
# Configure this Discovery Server
#TODO here you add configurations for server

server:
port: 3000
eureka:
client:
registerWithEureka: false
fetchRegistry: false

logging:
pattern:
console: "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
Expand Down
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,8 @@
import org.springframework.web.bind.annotation.*;

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

@RestController
Expand All @@ -26,7 +29,17 @@ public PersonsController(PersonService personService) {
@ResponseStatus(HttpStatus.OK)
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<Person> list() {
return new ArrayList<>();
List<Person> list = personService.findAll();

//sort by id
list.sort(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getId().compareTo(o2.getId());
}
});

return list;
}

/**
Expand All @@ -36,6 +49,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, "Something went wrong!");
} else{
personService.save(person);
}
}

/**
Expand All @@ -48,7 +66,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 +80,17 @@ public Person show(@PathVariable Long id) {
@ResponseStatus(HttpStatus.NO_CONTENT)
@PutMapping("/{id}")
public void update(@RequestBody Person updatedPerson, @PathVariable Long id) {

Person person = personService.findById(id).orElseThrow(() -> new NotFoundException(Person.class, 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 +102,7 @@ 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: 7 additions & 0 deletions eureka/persons-server/src/main/resources/persons-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ server:
# Discovery Server Access
#TODO here you add configurations for eureka client

eureka:
client:
serviceUrl:
defaultZone: http://192.168.81.11:3000/eureka/
healthcheck:
enabled: true

info:
app:
name: persons-server
Expand Down
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,10 @@

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

@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,34 @@
server:
port: 8080
address: 0.0.0.0

#TODO use eureka to discover the URL for the service1 and service2

eureka:
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka/
register-with-eureka: true
fetch-registry: true

#TODO configure spring cloud gateway to route the request to downstream services (service1 and service2) based on the paths(/api/greeting, /product)

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 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)
9 changes: 6 additions & 3 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,7 +30,10 @@
<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.service1;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

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

@GetMapping("/greeting/{name}")
public String greeting(@PathVariable String name){
System.out.println(name);
return "Hello "+ name;
}
}
14 changes: 13 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,14 @@
server:
port: 8081
port: 8081
address: 0.0.0.0

spring:
application:
name: service1

eureka:
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka/
register-with-eureka: true
fetch-registry: true
17 changes: 14 additions & 3 deletions lab-6-api-gateway/service2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,27 @@
<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.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>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.service2;

import com.sun.istack.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;

@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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.service2;

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.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class Service2Controller {

private List<Product> products = new ArrayList<Product>();

@ResponseStatus(HttpStatus.OK)
@PostMapping()
public void addProduct(@RequestBody Product product, @RequestHeader Map<String, String> headers) {
for(Map.Entry<String,String> header : headers.entrySet()) {
System.out.println(header);
}
products.add(product);
}

@GetMapping()
public List<Product> getProducts() {
return products;
}
}
14 changes: 13 additions & 1 deletion lab-6-api-gateway/service2/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@

server:
port: 8082
port: 8082
address: 0.0.0.0

spring:
application:
name: service2

eureka:
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka/
register-with-eureka: true
fetch-registry: true