基础测试应用

为了方便起见,我们把要测试的项目和相关的沙箱项目放在同一个大项目中,要测试的项目只是其中的一个小模块,整体代码如下
整体pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.6.4</version>
  8. <relativePath/>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <groupId>com.deer</groupId>
  12. <artifactId>jvm_sandbox_demo</artifactId>
  13. <version>1.0-SNAPSHOT</version>
  14. <packaging>pom</packaging>
  15. <name>jvm_sandbox_demo</name>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <maven.compiler.source>1.8</maven.compiler.source>
  19. <maven.compiler.target>1.8</maven.compiler.target>
  20. </properties>
  21. <modules>
  22. <module>jvm_sandbox_base_app</module>
  23. </modules>
  24. <dependencyManagement>
  25. <dependencies>
  26. <dependency>
  27. <groupId>junit</groupId>
  28. <artifactId>junit</artifactId>
  29. <version>4.11</version>
  30. <scope>test</scope>
  31. </dependency>
  32. <dependency>
  33. <groupId>com.deer.base</groupId>
  34. <artifactId>jvm_sandbox_base_app</artifactId>
  35. <version>${project.version}</version>
  36. </dependency>
  37. </dependencies>
  38. </dependencyManagement>
  39. </project>

测试应用的pom如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <parent>
  5. <artifactId>jvm_sandbox_demo</artifactId>
  6. <groupId>com.deer</groupId>
  7. <version>1.0-SNAPSHOT</version>
  8. <relativePath>../pom.xml</relativePath>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <groupId>com.deer.base</groupId>
  12. <artifactId>jvm_sandbox_base_app</artifactId>
  13. <name>jvm_sandbox_base_app</name>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>4.11</version>
  23. <scope>test</scope>
  24. </dependency>
  25. </dependencies>
  26. <build>
  27. <plugins>
  28. <plugin>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-maven-plugin</artifactId>
  31. </plugin>
  32. <plugin>
  33. <groupId>org.apache.maven.plugins</groupId>
  34. <artifactId>maven-surefire-plugin</artifactId>
  35. <configuration>
  36. <skipTests>true</skipTests>
  37. </configuration>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

代码入口

  1. package com.deer.base;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class BaseApp {
  6. public static void main(String[] args) {
  7. SpringApplication.run(BaseApp.class, args);
  8. }
  9. }

web入口

  1. package com.deer.base.controller;
  2. import com.deer.base.service.HelloService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class HelloController {
  9. @Autowired
  10. private HelloService helloService;
  11. @RequestMapping("/sayHi/{userName}")
  12. public String sayHello(@PathVariable(name ="userName") String userName) {
  13. return helloService.sayHello(userName);
  14. }
  15. }

业务代码实现类

  1. package com.deer.base.service;
  2. public interface HelloService {
  3. String sayHello(String userName);
  4. }
  1. package com.deer.base.service.impl;
  2. import com.deer.base.service.HelloService;
  3. import org.springframework.stereotype.Service;
  4. import java.util.concurrent.atomic.AtomicLong;
  5. @Service
  6. public class HelloServiceImpl implements HelloService {
  7. private AtomicLong seq = new AtomicLong(1L);
  8. @Override
  9. public String sayHello(String userName) {
  10. return "hello ," + userName + ",seq = " + seq.incrementAndGet();
  11. }
  12. }

现在启动应用访问

  1. http://localhost:9090/sayHi/zhangfei

返回结果

  1. hello ,zhangfei,seq = 2

目标

如果多次访问这个接口,我们会发现这个seq是不断递增的,我们想要把这个请求记录下来,可以参考java通用的apm工具,skywalking,如果相同的trace ID,无论多少次访问,我们的返回结果不变。