Skip to content

Commit

Permalink
spring cloud gateway获取response body
Browse files Browse the repository at this point in the history
  • Loading branch information
yefengyu committed Oct 27, 2019
1 parent 85ff2e6 commit 7256332
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yefengyu.gateway.globalFilter;
package com.yefengyu.gateway.global;

import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yefengyu.gateway.globalFilter;
package com.yefengyu.gateway.global;

import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yefengyu.gateway.localFilter;
package com.yefengyu.gateway.local;

import com.yefengyu.gateway.utitls.AuthUtil;
import org.springframework.cloud.gateway.filter.GatewayFilter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yefengyu.gateway.localFilter;
package com.yefengyu.gateway.local;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yefengyu.gateway.localFilter;
package com.yefengyu.gateway.local;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
Expand Down
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;
}
}
}
1 change: 1 addition & 0 deletions gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ spring:
- StripPrefix=1
- Auth
- IPForbid=0:0:0:0:0:0:0:1
- ResponseBody
my-load-balanced-service:
ribbon:
listOfServers: localhost:1001,localhost:1002,localhost:1003
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.yefengyu.cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HealthController
{
@GetMapping("/heath")
@ResponseBody
public String heath()
{
return "ok";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.yefengyu.cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloWorldController
{
@GetMapping("/hello")
@ResponseBody
public String hello()
{
return "hello spring cloud, 1001";
Expand Down
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 + '\'' +
'}';
}
}
}


Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HealthController
{
@GetMapping("/heath")
@ResponseBody
public String heath()
{
return "ok";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloWorldController
{
@GetMapping("/hello")
@ResponseBody
public String hello()
{
return "hello spring cloud, 1002";
Expand Down
65 changes: 65 additions & 0 deletions provider1002/src/main/java/cloud/controller/PersonController.java
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 + '\'' +
'}';
}
}
}


Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HealthController
{
@GetMapping("/heath")
@ResponseBody
public String heath()
{
return "ok";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package cloud.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloWorldController
{
@GetMapping("/hello")
@ResponseBody
public String hello()
{
return "hello spring cloud, 1003";
Expand Down

0 comments on commit 7256332

Please sign in to comment.