diff --git a/lab-6-api-gateway/api-gateway-project/bin/.gitignore b/lab-6-api-gateway/api-gateway-project/bin/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/lab-6-api-gateway/api-gateway-project/bin/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/lab-6-api-gateway/api-gateway-project/bin/pom.xml b/lab-6-api-gateway/api-gateway-project/bin/pom.xml new file mode 100644 index 0000000..871ce8d --- /dev/null +++ b/lab-6-api-gateway/api-gateway-project/bin/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.5 + + + com.example + api-gateway-project + 0.0.1-SNAPSHOT + api-gateway-project + api-gateway-project + + 11 + 2021.0.1 + + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + org.springframework.boot + spring-boot-starter-test + test + + + io.projectreactor + reactor-test + test + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/lab-6-api-gateway/api-gateway-project/bin/src/main/resources/application.yml b/lab-6-api-gateway/api-gateway-project/bin/src/main/resources/application.yml new file mode 100644 index 0000000..aaad35d --- /dev/null +++ b/lab-6-api-gateway/api-gateway-project/bin/src/main/resources/application.yml @@ -0,0 +1,6 @@ +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) \ No newline at end of file diff --git a/lab-6-api-gateway/api-gateway-project/pom.xml b/lab-6-api-gateway/api-gateway-project/pom.xml index 871ce8d..4308e3c 100644 --- a/lab-6-api-gateway/api-gateway-project/pom.xml +++ b/lab-6-api-gateway/api-gateway-project/pom.xml @@ -32,7 +32,10 @@ org.springframework.cloud spring-cloud-starter-gateway - + + org.springframework.cloud + spring-cloud-netflix-eureka-server + org.springframework.boot spring-boot-starter-test @@ -43,6 +46,11 @@ reactor-test test + + org.junit.jupiter + junit-jupiter-api + test + @@ -65,4 +73,4 @@ - + \ No newline at end of file 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..23e24df 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 @@ -1,6 +1,33 @@ 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 + +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 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) \ 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..f95d31e 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 @@ -30,7 +30,10 @@ org.springframework.boot spring-boot-starter-web - + + org.springframework.cloud + spring-cloud-netflix-eureka-client + org.springframework.boot spring-boot-starter-test @@ -58,4 +61,4 @@ - + \ No newline at end of file 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..5ffd46c 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,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 { @@ -16,5 +18,4 @@ public static void main(String[] args) { // 3. print request headers // 4. register the service in eureka - } diff --git a/lab-6-api-gateway/service1/src/main/java/com/example/service1/controller/Service1Controller.java b/lab-6-api-gateway/service1/src/main/java/com/example/service1/controller/Service1Controller.java new file mode 100644 index 0000000..b4aadcd --- /dev/null +++ b/lab-6-api-gateway/service1/src/main/java/com/example/service1/controller/Service1Controller.java @@ -0,0 +1,27 @@ +package com.example.service1.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +import org.springframework.http.HttpStatus; + +@RestController +@RequestMapping("/api") +public class Service1Controller { + + @ResponseStatus(HttpStatus.OK) + @GetMapping(value = "/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 String.format("Hello %s.", name); + } +} \ No newline at end of file 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..f7dfe35 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,14 @@ server: - port: 8081 \ No newline at end of file + 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 diff --git a/lab-6-api-gateway/service2/pom.xml b/lab-6-api-gateway/service2/pom.xml index be06918..7a0abcf 100644 --- a/lab-6-api-gateway/service2/pom.xml +++ b/lab-6-api-gateway/service2/pom.xml @@ -26,16 +26,32 @@ org.springframework.boot spring-boot-starter-web - + org.springframework.boot spring-boot-starter-test test + + org.springframework.cloud + spring-cloud-netflix-eureka-client + + + org.hibernate + hibernate-core + + + org.projectlombok + lombok + + + org.projectlombok + lombok + @@ -58,4 +74,4 @@ - + \ No newline at end of file diff --git a/lab-6-api-gateway/service2/src/main/java/com/example/service2/Product.java b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Product.java new file mode 100644 index 0000000..498074a --- /dev/null +++ b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Product.java @@ -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; +} \ No newline at end of file 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..b87df25 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,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 Service2Application { 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..50236e4 --- /dev/null +++ b/lab-6-api-gateway/service2/src/main/java/com/example/service2/Service2Controller.java @@ -0,0 +1,34 @@ +package com.example.service2; + +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; + } +} \ No newline at end of file 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..a666c7f 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,14 @@ - server: - port: 8082 \ No newline at end of file + 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 \ No newline at end of file