Skip to content

meazza/spring-boot-request-copy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

This project is help to implement copying Spring mvc controller request to the other address by a given ratio. The Traditional method to achieve this is using tools like gor or tcpcopy. If these tools could not be used in your Spring Boot project for some reason, please read the following context and it can help you.

How to use it

First, add Maven dependencies in your project

<dependencies>
  <dependency>
    <groupId>com.github.meazza</groupId>
    <artifactId>spring-boot-request-copy</artifactId>
    <version>1.0.5.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.54</version>
  </dependency>
</dependencies>

and then, import configuration classes in main class.

@SpringBootApplication
@RestController
@Import({WebAppConfigurer.class, RequestWrapFilter.class})
public class AnnotationTestApplication { 
 
  public static void main(String[] args) { 
    SpringApplication.run(AnnotationTestApplication.class, args); 
  }
}

Now you can add @RequestCopy annotation on the method that you want the requests received by it to be copied.

// This request is copied to url: http://localhost:8080/test by the ratio of 5%
@RequestMapping(value = "/")
@RequestCopy(url = "http://localhost:8080/test", ratio = 0.05F)
public String hello(@RequestParam(defaultValue = "nobody") String name) {
  System.out.println("hello, " + name);
  return "hello, " + name;
}

What can be supported

All GET requests and POST requests whose headers include "Content-Type:application/json"

Testing the effects

You can test the effects in this project, follow the steps:

  1. Clone the whole project
  2. Modify this parameter in pom.xml: <spring-boot-maven-plugin.skip>false</spring-boot-maven-plugin.skip>
  3. Executing command: mvn spring-boot:run
2018-12-10 09:46:51.768  INFO 10196 --- [nio-8080-exec-7] c.g.m.handler.RequestHandlerInterceptor  : Send copied GET request to url: http://localhost:8080/test?name=meazza, and receive response: hello test, meazza
2018-12-10 09:48:15.747  INFO 10196 --- [nio-8080-exec-3] c.g.m.handler.RequestHandlerInterceptor  : Send copied POST request to url: http://localhost:8080/test/post, body: {"name":"meazza","id":1}, and receive response: hello test, PostBody(id=1, name=meazza)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages