Skip to content

Commit 022ade7

Browse files
committed
samples update for proxy configuration with spring-cloud-gateway
1 parent 9a8f909 commit 022ade7

File tree

7 files changed

+73
-9
lines changed

7 files changed

+73
-9
lines changed

springdoc-openapi-microservices/config-service/src/main/resources/config/department-service.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
server:
22
port: 8090
3+
forward-headers-strategy: framework
34
eureka:
45
client:
56
serviceUrl:
@@ -9,4 +10,8 @@ eureka:
910

1011
logging:
1112
pattern:
12-
console: "%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} %m%n"
13+
console: "%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} %m%n"
14+
15+
springdoc:
16+
cache:
17+
disabled: true

springdoc-openapi-microservices/config-service/src/main/resources/config/discovery-service.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
server:
22
port: 8761
3+
forward-headers-strategy: framework
34
eureka:
45
client:
56
serviceUrl:

springdoc-openapi-microservices/config-service/src/main/resources/config/employee-service.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
server:
22
port: 8092
3+
forward-headers-strategy: framework
34
eureka:
45
client:
56
serviceUrl:
@@ -10,3 +11,7 @@ eureka:
1011
logging:
1112
pattern:
1213
console: "%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} %m%n"
14+
15+
springdoc:
16+
cache:
17+
disabled: true

springdoc-openapi-microservices/config-service/src/main/resources/config/gateway-service.yml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
server:
22
port: 8060
3+
forward-headers-strategy: framework
34

45
eureka:
56
client:
@@ -11,6 +12,9 @@ eureka:
1112
logging:
1213
pattern:
1314
console: "%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} %m%n"
15+
level:
16+
org.springframework.cloud.gateway: TRACE
17+
org.springframework.http.server.reactive: TRACE
1418

1519
spring:
1620
cloud:
@@ -22,28 +26,38 @@ spring:
2226
- id: employee-service
2327
uri: lb://employee-service
2428
predicates:
25-
- Path=/employee/**
29+
- Path=/employee/**, /demo-microservices/employee/**
2630
filters:
31+
- ContextPathRewritePath=/demo-microservices/employee/(?<path>.*), /$\{path}
2732
- RewritePath=/employee/(?<path>.*), /$\{path}
2833
- id: department-service
2934
uri: lb://department-service
3035
predicates:
31-
- Path=/department/**
36+
- Path=/department/**, /demo-microservices/department/**
3237
filters:
38+
- ContextPathRewritePath=/demo-microservices/department/(?<path>.*), /$\{path}
3339
- RewritePath=/department/(?<path>.*), /$\{path}
3440
- id: organization-service
3541
uri: lb://organization-service
3642
predicates:
37-
- Path=/organization/**
43+
- Path=/organization/**, /demo-microservices/organization/**
3844
filters:
45+
- ContextPathRewritePath=/demo-microservices/organization/(?<path>.*), /$\{path}
3946
- RewritePath=/organization/(?<path>.*), /$\{path}
47+
- id: openapi-proxy
48+
uri: https://demos1.springdoc.org
49+
predicates:
50+
- Path=/demo-microservices/v3/api-docs/**
51+
filters:
52+
- RewritePath=/demo-microservices/v3/api-docs/(?<path>.*), /demo-microservices/$\{path}/v3/api-docs
4053
- id: openapi
4154
uri: http://localhost:${server.port}
4255
predicates:
4356
- Path=/v3/api-docs/**
4457
filters:
4558
- RewritePath=/v3/api-docs/(?<path>.*), /$\{path}/v3/api-docs
4659

60+
4761
springdoc:
4862
swagger-ui:
4963
urls:
@@ -52,4 +66,6 @@ springdoc:
5266
- name: department
5367
url: /v3/api-docs/department
5468
- name: organization
55-
url: /v3/api-docs/organization
69+
url: /v3/api-docs/organization
70+
cache:
71+
disabled: true

springdoc-openapi-microservices/config-service/src/main/resources/config/organization-service.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ eureka:
1111

1212
logging:
1313
pattern:
14-
console: "%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} %m%n"
14+
console: "%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} %m%n"
15+
16+
springdoc:
17+
cache:
18+
disabled: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.springdoc.demo.services.gateway;
2+
3+
import org.springframework.cloud.gateway.filter.GatewayFilter;
4+
import org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory;
5+
import org.springframework.http.server.reactive.ServerHttpRequest;
6+
import org.springframework.stereotype.Component;
7+
8+
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
9+
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl;
10+
11+
/**
12+
* @author bnasslahsen
13+
*/
14+
@Component
15+
public class ContextPathRewritePathGatewayFilterFactory extends RewritePathGatewayFilterFactory {
16+
17+
@Override
18+
public GatewayFilter apply(Config config) {
19+
String replacement = config.getReplacement().replace("$\\", "$");
20+
return (exchange, chain) -> {
21+
ServerHttpRequest req = exchange.getRequest();
22+
23+
addOriginalRequestUrl(exchange, req.getURI());
24+
String path = req.getURI().getRawPath();
25+
26+
String newPath = path.replaceAll(config.getRegexp(), replacement);
27+
ServerHttpRequest request = req.mutate().path(newPath).contextPath("/").build();
28+
29+
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI());
30+
31+
return chain.filter(exchange.mutate().request(request).build());
32+
};
33+
}
34+
35+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
server:
2-
forward-headers-strategy: framework
31
spring:
42
application:
53
name: gateway-service
@@ -8,4 +6,4 @@ spring:
86
springdoc:
97
version: '@springdoc.version@'
108
swagger-ui:
11-
use-root-path: true
9+
use-root-path: true

0 commit comments

Comments
 (0)