Skip to content

Commit

Permalink
Migrate from Netflix Zuul to Spring Cloud Gateway spring-petclinic#117
Browse files Browse the repository at this point in the history
  • Loading branch information
arey committed Apr 3, 2019
1 parent 3682d1b commit 2c739b9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
6 changes: 1 addition & 5 deletions spring-petclinic-api-gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -71,7 +67,7 @@
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@
*/
package org.springframework.samples.petclinic.api;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;


/**
* @author Maciej Szarlinski
*/
@EnableZuulProxy
@EnableDiscoveryClient
@EnableCircuitBreaker
@SpringBootApplication
Expand All @@ -42,4 +49,19 @@ public static void main(String[] args) {
RestTemplate loadBalancedRestTemplate() {
return new RestTemplate();
}

@Value("classpath:/static/index.html")
private Resource indexHtml;

/**
* workaround solution for forwarding to index.html
* @see <a href="https://github.com/spring-projects/spring-boot/issues/9785">#9785</a>
*/
@Bean
RouterFunction<?> routerFunction() {
RouterFunction router = RouterFunctions.resources("/**", new ClassPathResource("static/"))
.andRoute(RequestPredicates.GET("/"),
request -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).syncBody(indexHtml));
return router;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.samples.petclinic.api.dto.VisitDetails;
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;


Expand All @@ -35,6 +36,7 @@
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/gateway")
public class ApiGatewayController {

private final CustomersServiceClient customersServiceClient;
Expand Down
30 changes: 22 additions & 8 deletions spring-petclinic-api-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
zuul:
prefix: /api
ignoredServices: '*'
routes:
vets-service: /vet/**
visits-service: /visit/**
customers-service: /customer/**
api-gateway: /gateway/**
spring:
cloud:
gateway:
routes:
- id: vets-service
uri: lb://vets-service
predicates:
- Path=/api/vet/**
filters:
- StripPrefix=2
- id: visits-service
uri: lb://visits-service
predicates:
- Path=/api/visit/**
filters:
- StripPrefix=2
- id: customers-service
uri: lb://customers-service
predicates:
- Path=/api/customer/**
filters:
- StripPrefix=2

0 comments on commit 2c739b9

Please sign in to comment.