基础测试应用
为了方便起见,我们把要测试的项目和相关的沙箱项目放在同一个大项目中,要测试的项目只是其中的一个小模块,整体代码如下
整体pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.deer</groupId>
<artifactId>jvm_sandbox_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>jvm_sandbox_demo</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>jvm_sandbox_base_app</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.deer.base</groupId>
<artifactId>jvm_sandbox_base_app</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
测试应用的pom如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jvm_sandbox_demo</artifactId>
<groupId>com.deer</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.deer.base</groupId>
<artifactId>jvm_sandbox_base_app</artifactId>
<name>jvm_sandbox_base_app</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
代码入口
package com.deer.base;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BaseApp {
public static void main(String[] args) {
SpringApplication.run(BaseApp.class, args);
}
}
web入口
package com.deer.base.controller;
import com.deer.base.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("/sayHi/{userName}")
public String sayHello(@PathVariable(name ="userName") String userName) {
return helloService.sayHello(userName);
}
}
业务代码实现类
package com.deer.base.service;
public interface HelloService {
String sayHello(String userName);
}
package com.deer.base.service.impl;
import com.deer.base.service.HelloService;
import org.springframework.stereotype.Service;
import java.util.concurrent.atomic.AtomicLong;
@Service
public class HelloServiceImpl implements HelloService {
private AtomicLong seq = new AtomicLong(1L);
@Override
public String sayHello(String userName) {
return "hello ," + userName + ",seq = " + seq.incrementAndGet();
}
}
现在启动应用访问
http://localhost:9090/sayHi/zhangfei
返回结果
hello ,zhangfei,seq = 2
目标
如果多次访问这个接口,我们会发现这个seq是不断递增的,我们想要把这个请求记录下来,可以参考java通用的apm工具,skywalking,如果相同的trace ID,无论多少次访问,我们的返回结果不变。