-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
303 additions
and
17 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...lter/FluidControlGlobalGatewayFilter.java → ...obal/FluidControlGlobalGatewayFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...obalFilter/GlobalGatewayFilterConfig.java → ...way/global/GlobalGatewayFilterConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ateway/localFilter/AuthGatewayFilter.java → ...ngyu/gateway/local/AuthGatewayFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...localFilter/AuthGatewayFilterFactory.java → ...teway/local/AuthGatewayFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...lFilter/IPForbidGatewayFilterFactory.java → ...y/local/IPForbidGatewayFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
gateway/src/main/java/com/yefengyu/gateway/local/ResponseBodyGatewayFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.yefengyu.gateway.local; | ||
|
||
|
||
import org.reactivestreams.Publisher; | ||
import org.springframework.cloud.gateway.support.CachedBodyOutputMessage; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter; | ||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; | ||
import org.springframework.cloud.gateway.support.BodyInserterContext; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.http.server.reactive.ServerHttpResponseDecorator; | ||
import org.springframework.web.reactive.function.BodyInserter; | ||
import org.springframework.web.reactive.function.BodyInserters; | ||
import org.springframework.web.reactive.function.client.ClientResponse; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR; | ||
|
||
@Component | ||
public class ResponseBodyGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> { | ||
|
||
@Override | ||
public GatewayFilter apply(Object config) { | ||
|
||
return new ModifyResponseGatewayFilter(); | ||
} | ||
|
||
|
||
public class ModifyResponseGatewayFilter implements GatewayFilter, Ordered { | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | ||
return chain.filter(exchange.mutate().response(decorate(exchange)).build()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
ServerHttpResponse decorate(ServerWebExchange exchange) { | ||
return new ServerHttpResponseDecorator(exchange.getResponse()) { | ||
|
||
@Override | ||
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { | ||
|
||
Class inClass = String.class; | ||
Class outClass = String.class; | ||
|
||
String originalResponseContentType = exchange | ||
.getAttribute(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR); | ||
HttpHeaders httpHeaders = new HttpHeaders(); | ||
|
||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, | ||
originalResponseContentType); | ||
|
||
ClientResponse clientResponse = ClientResponse | ||
.create(exchange.getResponse().getStatusCode()) | ||
.headers(headers -> headers.putAll(httpHeaders)) | ||
.body(Flux.from(body)).build(); | ||
|
||
Mono modifiedBody = clientResponse.bodyToMono(inClass) | ||
.flatMap(originalBody -> { | ||
//TODO:此次可以对返回的body进行操作 | ||
System.out.println(originalBody); | ||
return Mono.just(originalBody); | ||
}); | ||
|
||
BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, | ||
outClass); | ||
CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage( | ||
exchange, exchange.getResponse().getHeaders()); | ||
return bodyInserter.insert(outputMessage, new BodyInserterContext()) | ||
.then(Mono.defer(() -> { | ||
Flux<DataBuffer> messageBody = outputMessage.getBody(); | ||
HttpHeaders headers = getDelegate().getHeaders(); | ||
if (!headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) { | ||
messageBody = messageBody.doOnNext(data -> headers | ||
.setContentLength(data.readableByteCount())); | ||
} | ||
return getDelegate().writeWith(messageBody); | ||
})); | ||
} | ||
|
||
@Override | ||
public Mono<Void> writeAndFlushWith( | ||
Publisher<? extends Publisher<? extends DataBuffer>> body) { | ||
return writeWith(Flux.from(body).flatMapSequential(p -> p)); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
provider1001/src/main/java/com/yefengyu/cloud/controller/HealthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
provider1001/src/main/java/com/yefengyu/cloud/controller/HelloWorldController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
provider1001/src/main/java/com/yefengyu/cloud/controller/PersonController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.yefengyu.cloud.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@RestController | ||
public class PersonController { | ||
|
||
@GetMapping("/persons") | ||
public List<Person> listPerson(){ | ||
List<Person> personList = new ArrayList<>(); | ||
for(int i = 0; i < 100; i++){ | ||
Person person = new Person(); | ||
person.setId(i); | ||
person.setName("王五" + i); | ||
person.setAddress("北京" + i); | ||
personList.add(person); | ||
} | ||
return personList; | ||
} | ||
|
||
public static class Person{ | ||
private Integer id; | ||
private String name; | ||
private String address; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(String address) { | ||
this.address = address; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Person{" + | ||
"id=" + id + | ||
", name='" + name + '\'' + | ||
", address='" + address + '\'' + | ||
'}'; | ||
} | ||
} | ||
} | ||
|
||
|
2 changes: 0 additions & 2 deletions
2
provider1002/src/main/java/cloud/controller/HealthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
provider1002/src/main/java/cloud/controller/HelloWorldController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
provider1002/src/main/java/cloud/controller/PersonController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cloud.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@RestController | ||
public class PersonController { | ||
|
||
@GetMapping("/persons") | ||
public List<Person> listPerson(){ | ||
List<Person> personList = new ArrayList<>(); | ||
for(int i = 0; i < 100; i++){ | ||
Person person = new Person(); | ||
person.setId(i); | ||
person.setName("王五" + i); | ||
person.setAddress("北京" + i); | ||
personList.add(person); | ||
} | ||
return personList; | ||
} | ||
|
||
public static class Person{ | ||
private Integer id; | ||
private String name; | ||
private String address; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(String address) { | ||
this.address = address; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Person{" + | ||
"id=" + id + | ||
", name='" + name + '\'' + | ||
", address='" + address + '\'' + | ||
'}'; | ||
} | ||
} | ||
} | ||
|
||
|
2 changes: 0 additions & 2 deletions
2
provider1003/src/main/java/cloud/controller/HealthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
provider1003/src/main/java/cloud/controller/HelloWorldController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.